Confused Adding From, Reply-To and Recipients
Posted: Mon Feb 16, 2009 1:06 pm
I am writing a fairly simple (so far) script that will be sending some automatically triggered emails. The emails are pretty straight forward, but can be configured to optionally include additional email headers such as reply-to, cc, bcc, etc. Currently, I having two problems when using Swiftmailer to accomplish this.
First, I was going to simply use the Swift_Message class to load the message, subject, recipients and from address (one object to deal with is much easier IMHO). Unfortunately, when I wrote some test code, I was having a problem including both a name and email address for the recipients. It would seem that all of the setters in Swift_Message convert the input to Swift_Address, but then use Swift_Address::build() which drops the name off and only passes the email address. This means every email we send comes across as from john@mydomain.com instead of John Doe. When I started to investigate the code a little further, I read this in Swift::send() [line 323] What I am inferring from this comment is that Swift does not recommend utilizing the Swift_message::setTo|setCC|setBcc methods at all. Is that correct?
When I went back to the drawing board, I ran into another problem. Using Swift::send(), there is no way to set the Reply-To or Return-Path headers. It only looks at what is based via $message, which works out alright, I guess, but isn't obvious at first glance.
What is the "proper" way to accomplish this? Here's my snipped code (which seems to work, but requires using four Swift objects in a somewhat muddled manner):
Maybe someone that knows more about this class can give me some additional insight? Am I missing or abusing anything?
Thanks,
Brandon
EDIT: I forgot to mention that I am using Swift 3.3.2 from the svn trunk
First, I was going to simply use the Swift_Message class to load the message, subject, recipients and from address (one object to deal with is much easier IMHO). Unfortunately, when I wrote some test code, I was having a problem including both a name and email address for the recipients. It would seem that all of the setters in Swift_Message convert the input to Swift_Address, but then use Swift_Address::build() which drops the name off and only passes the email address. This means every email we send comes across as from john@mydomain.com instead of John Doe. When I started to investigate the code a little further, I read this in Swift::send() [line 323]
Code: Select all
* @param Swift_Message The message to send. This does not need to (and shouldn't really) have any of the recipient headers set.When I went back to the drawing board, I ran into another problem. Using Swift::send(), there is no way to set the Reply-To or Return-Path headers. It only looks at what is based via $message, which works out alright, I guess, but isn't obvious at first glance.
What is the "proper" way to accomplish this? Here's my snipped code (which seems to work, but requires using four Swift objects in a somewhat muddled manner):
Code: Select all
$email_from = new Swift_Address(.@.., ...); //email@address.com, full name
$email_replyto = new Swift_Address(.@.., ...);
$email_recipients = new Swift_RecipientList();
$email_recipients->addTo(.@.., ...);
$email_recipients->addBcc(.@.., ...);
$email_message = new Swift_Message($email->getSubject(), $email->getBody(), 'text/plain');
$email_message->setFrom($email_from); //notice this is being set here and is still required to be passed to Swift::send()
$email_message->setReplyTo($email_replyto);
try {
$emails_sent = $emailer->send($email_message, $email_recipients, $email_from);
echo 'sent ' . $emails_sent;
} catch (Swift_ConnectionException $swift_e) {
//...
}Thanks,
Brandon
EDIT: I forgot to mention that I am using Swift 3.3.2 from the svn trunk