Page 1 of 1
How to add dynamic content to the message part?
Posted: Tue Jan 08, 2008 10:15 am
by Cinner
I've been working succesfully with Swift for a while now, but now that I want to add dynamic content to the message part of an email, I run into trouble. I want to loop the MySQL database and list a number of items in my message. How do you do this without breaking up the code? I put my loop in the $message->attach(new Swift_Message_Part(, but that doesn't work obviously.
Posted: Tue Jan 08, 2008 5:42 pm
by MichaelGallagher
I don't know if this helps, but it works for me!
It is text based email only, i suppose if you want HTML you can add the appropriate tags..
I know exactly what client OS, browser and email clients I am coding for, since it is in-house solution, so I know the \n will create a carriage return, however, you may need to use a combination of \n \r to ensure it works on both PC and Mac, etc..
unless of course you use html!
Code: Select all
//Grab all data from database etc...
...
...
//Declare body of message..
$body=" COMPANY CONTACT INFORMATION:-\n".
" ==================================\n".
" Company Name: ".$CompanyName."\n".
" ClickPosID: ".$ClickPosID."\n".
" Cust Class: ".$CustClass."\n".
" Date Created: ".$DateCreated."\n".
" ----------------------------------\n".
" Referral Emailed By: ".$nameSalesRep."\n".
" Actual Sales Rep Name: ".$UserName."\n".
" Actual Sales Rep Code: ".$SalesRep."\n".
" Exec: ".$Exec."\n".
" ----------------------------------\n".
" Contact Name: ".$ContactTitle." ".$ContactFName." ".$ContactLName."\n".
" Job Title: ".$ContactJobTitle."\n".
" Phone: ".$ContactPhone."\n".
" Fax: ".$ContactFax."\n".
" Mobile: ".$ContactMobile."\n".
" Email: ".$ContactEmail."\n".
" Site Address: ".$SiteAddress." ".$SiteSuburb." ".$SiteState." ".$SitePC."\n".
" Postal Address: ".$PostalAddress." ".$PostalSuburb." ".$PostalState." ".$PostalPC."\n".
" CIDN: ".$CIDN."\n".
" ==================================\n\n\n".
" OASIS OPPORTUNITY INFORMATION:-\n".
" ==================================\n".
" Sale Status: ".$SaleStatus."\n".
" Sale Type: ".$SaleType."\n".
" Service Type: ".$ServiceType."\n".
" Opportunity: ".$Opportunity."\n".
" Number Services: ".$NumberServices."\n".
" Exp. Close Date: ".$ExpCloseDate."\n".
" Sale Probability: ".$SaleProbability."\n".
" Expected GP: ".$ExpectedGP."\n".
" Opportunity Date Created: ".$PortDateCreated."\n".
" MAE Resource: ".$MAEResource."\n".
" Other Resource: ".$OtherResource."\n".
" ----------------------------------\n".
" Notes: ".$Notes."\n".
" ==================================\n\n\n".
// add all other stuff, attachments, subject, sender, etc..
..
..
//Create the message to send
$message = new Swift_Message($subject);
$message->attach(new Swift_Message_Part($body));
..
..
//Try sending the email
$sent = $swift->send($message, $recipients, $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
..
..
Thats the basic structure i used.. just recycled one i found somewhere else..
Hope this helped,
Mick

Re: How to add dynamic content to the message part?
Posted: Tue Jan 08, 2008 5:46 pm
by superdezign
Cinner wrote:I've been working succesfully with Swift for a while now, but now that I want to add dynamic content to the message part of an email, I run into trouble. I want to loop the MySQL database and list a number of items in my message. How do you do this without breaking up the code? I put my loop in the $message->attach(new Swift_Message_Part(, but that doesn't work obviously.
What are you attaching...?
Do you want this data to be inside of the message or in a separate file attachment?
It sounds like you just want to generate the message contents before creating the message object.
Posted: Tue Jan 08, 2008 6:19 pm
by Chris Corbyn
MichaelGallagher wrote:I know exactly what client OS, browser and email clients I am coding for, since it is in-house solution, so I know the \n will create a carriage return, however, you may need to use a combination of \n \r to ensure it works on both PC and Mac, etc..
Swift will change it if it's wrong

There are rules for when \r\n shoud be used rather than just \n, but swift takes care of it for you.
~ Cinner, you can change the value of a sub-part at runtime easily
Code: Select all
$message = new Swift_Message( .. );
$part = new Swift_Message_Part(); //Create the part early without specifying a body
$message->attach($part); //attach it (this is just a reference to $part)
foreach ($recipients as $address) {
$recipientSpecificBody = 'Hello ' . $address;
$part->setBody($recipientSpecificBody); //Add content to the body in the loop
$swift->send($message, .... ); // .. and send
}
This is actually very inexpensive too due to the way Swift caches its messages.
Posted: Wed Jan 09, 2008 10:11 am
by Cinner
Thanks guys, I will try out your suggestions.
Any idea why I can't sent multipart messages though? With the code below, only the second part shows up in the email:
Code: Select all
//Create the message to send
$message =& new Swift_Message($title);
//Add some "parts"
$message->attach(new Swift_Message_Part("Part 1 of message"));
$message->attach(new Swift_Message_Part("Part <strong>2</strong> of message", "text/html"));
//Try sending the email
$sent = $swift->send($message, "(my_email)", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
Posted: Wed Jan 09, 2008 2:43 pm
by Chris Corbyn
Cinner wrote:Thanks guys, I will try out your suggestions.
Any idea why I can't sent multipart messages though? With the code below, only the second part shows up in the email:
Code: Select all
//Create the message to send
$message =& new Swift_Message($title);
//Add some "parts"
$message->attach(new Swift_Message_Part("Part 1 of message"));
$message->attach(new Swift_Message_Part("Part <strong>2</strong> of message", "text/html"));
//Try sending the email
$sent = $swift->send($message, "(my_email)", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
That will definitely have sent a multipart message. More likely your email client doesn't make it obvious how to switch to the plain text part.
EDIT | Try setting your mail client preferences as "View messages in plain text" or equivalent and try opening the email again if you can't find an option to switch.