How to add dynamic content to the message part?
Moderators: Chris Corbyn, General Moderators
How to add dynamic content to the message part?
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.
- MichaelGallagher
- Forum Newbie
- Posts: 7
- Joined: Thu Dec 20, 2007 1:09 am
- Location: Gold Coast, QLD, Australia
- Contact:
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!
Thats the basic structure i used.. just recycled one i found somewhere else..
Hope this helped,
Mick
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();
..
..Hope this helped,
Mick
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: How to add dynamic content to the message part?
What are you attaching...?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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Swift will change it if it's wrongMichaelGallagher 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..
~ 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
}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:
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();- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.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();
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.