Page 1 of 1

Confused Adding From, Reply-To and Recipients

Posted: Mon Feb 16, 2009 1:06 pm
by BrandonMUS
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]

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.
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):

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) {
//...
}
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

Re: Confused Adding From, Reply-To and Recipients

Posted: Mon Feb 16, 2009 7:46 pm
by Chris Corbyn
I think you'll find version 4 much more intuitive. In version 4 everything is set on the message object... no RecipientList type stuff.

Look for the "Version 4.0.0 Beta" thread stickied at the top of this forum.

Re: Confused Adding From, Reply-To and Recipients

Posted: Tue Feb 17, 2009 9:31 am
by BrandonMUS
I saw the sticky (and I've been following the progress via svn for a few months now), but I wasn't sure how stable it was. This is something that needed to go into production 2 months ago, so I didn't want to have to wait for a stable package to be released. Also, since this is my first experience with the project, the lack of documentation for version 4 makes it a little harder to learn (though I see in the sticky the basics are covered).

How far out is a release candidate for 4.0?

Re: Confused Adding From, Reply-To and Recipients

Posted: Tue Feb 17, 2009 6:41 pm
by Chris Corbyn
BrandonMUS wrote:I saw the sticky (and I've been following the progress via svn for a few months now), but I wasn't sure how stable it was. This is something that needed to go into production 2 months ago, so I didn't want to have to wait for a stable package to be released. Also, since this is my first experience with the project, the lack of documentation for version 4 makes it a little harder to learn (though I see in the sticky the basics are covered).

How far out is a release candidate for 4.0?
There's a lot of documentation for v4. I've got more coming too.

Also note, I'm not using svn anymore (I moved to git) so that svn repository on sourceforge is dead.

The official 4.0.0 release will be out in 3 days time (21st Feb). It will be the same as 4.0.0-b5 with a couple of additional methods in the message headers.

I'm uploading some new documentation tonight (docs for plugins and docs for working with message headers).

The details are in the sticky :)

NOTE: 4.0.0-b5 is stable. I have had no bug reports and I know it's being used in production.