Page 1 of 1

Two questions for v4: setSender and setBody

Posted: Wed Mar 11, 2009 6:32 am
by GryphonLeon
Hi Chris,

As I wrote in a previous thread, I'm converting my scripts from v3 to v4.

-----
setSender()
-----

In v3, I could use the following:

Code: Select all

$message->headers->set('Sender', 'Domain.com <delivery@domain.com>');
In v4, this doesn't work, the setSender() method only accepts an email address like so:

Code: Select all

$message->setSender('delivery@domain.com');
I've tried $message->setSender('delivery@domain.com' => 'Domain.com'); but that doesn't work. Is there anything I'm missing here or is it not possible to also include a name in the Sender header with v4?

----
setBody()
----

I'm trying to send a personalized email to one or more recipients. In v3, I used this (snipplet):

Code: Select all

 
for($i=0;$i<$nr_recipients;$i++) {
    $e_msg = str_replace(array('#%recpname%#','#%recp_key%#'),array($_POST['recpname'][$i],$recp_key_arr[$i]),$msg_tmpl);
    $message->setBody($e_msg);
    $email_sent = $mailer->send($message,new Swift_Address($_POST['recpemail'][$i],$_POST['recpname'][$i]),new Swift_Address($senderemail,$sendername));
}
 
The $msg_tmpl is a simple email template, which has the variables #%recpname%# and #%recp_key%#. As you can see, for each recipient (email), the template gets called and the variables will be replaced with an actual recipient name and recipient key. Works perfectly.

In v4, I'm trying to do the same, but somehow, it always uses the body for the first recipient..it doesn't replace it. Please note that I've made sure that $e_msg has the correct content. I echo'ed it inside the for loop and it is being personalized correctly. So, it looks like setBody doesn't replace the initial body when you call it again?

snipplet for v4:

Code: Select all

 
$message->setFrom(array($senderemail => $sendername));
for($i=0;$i<$nr_recipients;$i++) {
    $e_msg = str_replace(array('#%recpname%#','#%recp_key%#'),array($_POST['recpname'][$i],$recp_key_arr[$i]),$msg_tmpl);
    echo $e_msg.'<br /><br />';
    $message->setBody($e_msg,'text/plain');
    $message->setTo(array($_POST['recpemail'][$i] => $_POST['recpname'][$i]));
    $email_sent = $mailer->send($message);
}
 
Thanks Chris!

Re: Two questions for v4: setSender and setBody

Posted: Wed Mar 11, 2009 7:18 am
by Chris Corbyn
Looks like you were missing the "array" part in your example for senders. This should work.

Code: Select all

$message->setSender(array('delivery@domain.com' => 'Domain.com'));
With regards to the body not be reset. You appear to have found a bug that nobody picked up during beta 1, 2, 3, 4, 5 or rc1 :oops:

I'll fix this now since that's a pretty major bug. I had planned on adding a couple of other things to 4.0.1 but they can wait.

Re: Two questions for v4: setSender and setBody

Posted: Wed Mar 11, 2009 7:25 am
by GryphonLeon
Woops, sorry about that setSender mistake, I should have seen that one. :banghead:

I'm "glad" the setBody one is a real bug though, sort of makes up for the setSender question :lol:

I'll wait for the new version, thanks again for the quick replies!

Re: Two questions for v4: setSender and setBody

Posted: Wed Mar 11, 2009 7:49 am
by Chris Corbyn
New version in just a few moments... Packaging up now.

Re: Two questions for v4: setSender and setBody

Posted: Wed Mar 11, 2009 7:58 am
by Chris Corbyn

Re: Two questions for v4: setSender and setBody

Posted: Wed Mar 11, 2009 8:38 am
by GryphonLeon
Great, updated and works perfectly now. Thanks again!