[SOLVED] Personalized emails to multiple recipients

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
desertwriter
Forum Newbie
Posts: 5
Joined: Thu Mar 01, 2007 9:32 pm

[SOLVED] Personalized emails to multiple recipients

Post by desertwriter »

I'm attempting to personalize an email message that will be sent in either html or plain text format (choice made by the recipient) to multiple recipients (already in an array from mysql) on a PHP4 installation. Can you provide example code for what you consider the best solution? As an alternative, sending plain text and html parts would also work. Would this be a better solution?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

http://swiftmailer.org/wikidocs/v3/multirecipients and http://swiftmailer.org/wikidocs/v3/multipart are the most applicable tutorials available on the official site that I can see.
desertwriter
Forum Newbie
Posts: 5
Joined: Thu Mar 01, 2007 9:32 pm

Post by desertwriter »

I've looked at both of those and its my understanding that the first tutorial you cite shows all email addresses to all recipients, which is not acceptable. The second uses the attach, which seems like it should be a better fit, but I'm having trouble getting it to personalize each email. The attach statement is put inside of the loop, but only attaches the first time so that each recipient sees the same email. If that's the solution, it seems like I should be able to "detach" (found that in the documentation also), but received an error message that it couldn't detach using the id number I provided in the "attach" statement.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry, the documentation is still not 100% complete for v3. Bugfixes have come foremost on my list of priorities right now, but there's a thread here which contains a vague example:

viewtopic.php?t=63917

Anyone using < PHP 4.4 may be struggling with multipart and/or attachments until tomorrow's update is released.
desertwriter
Forum Newbie
Posts: 5
Joined: Thu Mar 01, 2007 9:32 pm

Post by desertwriter »

Thanks for the link to that example. :) I had seen that one already and was able to send mail using a similar routine. However, that doesn't address how to either send the message as multipart so the viewer can choose text or html or how to set the message type so that I send either text or html based on the recipient's preferences. Either solution would be fine. Thanks for your help and your responsiveness!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's the same concept, but you modify message parts instead:

Code: Select all

$html_body = "Hello {name}, 

Thanks for joining our mailing list blah blah blob blob...";

$text_body = "Hello {name}, 

Thanks for joining our mailing list blah blah blob blob...";

$message =& new Swift_Message();
$html_part =& new Swift_Message_Part($html_body, "text/html");
$text_part =& new Swift_Message_Part($text_body);
$message->attach($html_part);
$message->attach($text_part);

$subject = "{name} - You are now on our mailing list"; 

foreach ($users as $email => $name) 
{ 
    $message->setSubject(str_replace("{name}", $name, $subject)); 
    $html_part->setBody(str_replace("{name}", $name, $html_body));
    $text_part->setBody(str_replace("{name}", $name, $text_body)); 
    $swift->send($message, $email, $from); 
}
Last edited by Chris Corbyn on Sun Mar 04, 2007 9:41 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

psst.. parse error. :)

")" expected after each of the str_replace() calls.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:psst.. parse error. :)

")" expected after each of the str_replace() calls.
8O What parse error?

:lol: Fixed, thanks.
desertwriter
Forum Newbie
Posts: 5
Joined: Thu Mar 01, 2007 9:32 pm

Post by desertwriter »

Sorry to be a pain, but I still seem to be having a problem. First, in your foreach loop where the setBody function is supposed to change the message content, shouldn't the object references be to $message and not $html_part and $text_part (using those throws an undefined function error).

The second problem is that if those are replaced with $message->setBody, and we use the variables for $html_body and $text_body as the replacement subjects, the message never changes. I tested my replacement function and it was working because it echo'd the correct message body, but the setBody function doesn't seem to want to change the message body. Is this a bug or am I missing something very basic here?
thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Substitute setBody() with setData(). My apologies, I will add a wrapper function for that like I have done with Swift_Message... setBody() in Swift_Message simply passes the call to setData().
desertwriter
Forum Newbie
Posts: 5
Joined: Thu Mar 01, 2007 9:32 pm

Post by desertwriter »

:D
problem solved! Thanks a lot for your hard work on this project and your support.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I've just released 3.0.2 and about 2 minutes before I packaged it up I threw in the setBody() method for Swift_Message_Part to make things more user-friendly. The only reason it's called setData() is because it's the method which lies in the MIME layer and applies to Images, Attachments and any other MIME object.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Post Reply