[SOLVED] Personalized emails to multiple recipients
Moderators: Chris Corbyn, General Moderators
-
desertwriter
- Forum Newbie
- Posts: 5
- Joined: Thu Mar 01, 2007 9:32 pm
[SOLVED] Personalized emails to multiple recipients
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?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
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
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!
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
desertwriter
- Forum Newbie
- Posts: 5
- Joined: Thu Mar 01, 2007 9:32 pm
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
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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
desertwriter
- Forum Newbie
- Posts: 5
- Joined: Thu Mar 01, 2007 9:32 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia