CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Tips & Solutions

 Merge variables within a FCKEditor generated bulk email message (PHP)

Print topic Send  topic

Author Message
jacem

Posts: 87
Posted: 07/06/2007, 6:08 PM

I had a typical e-marketing application where I needed to send every person on a table an email message. The message content needed to be nicely laid out HTML so I created an FCKeditor text area to allow the user to design/write their bulk email message.

The users however needed to use certain fields from the table, such as their customer's name and customer's company name within the HTML textarea message. The solution in PHP was to use PHP's str_replace function. (This is the main tip in this posting!)

I decided which table fields the users would be allowed to merge in their email message. I also 'decided' that '%fieldname' would denote a meragble field. In the below example one merge field was $CustomerName

In my record grid attached to a before show event I generated the email thus as each record was displayed it triggered the code to output an email from the server for that record.

Notes: In the code example below $maillist is the record grid, EmailContent was the blob field that contained the previously saved email message in HTML as produced by FCKeditor, CustomerEmail is the field that contains the customer email address.

The custom code was;

$headers = "From: Billy Blogs <bill@blogs.com.au>;\r\n";
$headers.= "Reply-To: Billy Blogs <bill@blogs.com.au>;\r\n";
$headers.= "MIME-Version: 1.0;\r\n";
$headers .= "Content-type: text/html;\r\n";
//replace any occurance of the merge field $CustomerName with its table field value
$message=str_replace('$CustomerName',$mailinglist->CustomerName-Value,$mailinglist- >EmailContent->Value);
//add more merge field replacements here...
//perform the php email for this table record
mail($mailinglist->CustomerEmail->Value,"Test Bulk
Email","<HTML>\r\n".$message."\r\n</HTML>\r\n",$headers);
//-------------------

When the mail arrives at each customer's mail reader (ie: Outlook), their name replaces any occurrence of $CustomerName set in the email message content field. If you wish to allow the user to use other 'meragble fields' from the customer table then you just need to perform successive str_replace for each merge field you decide to provide your users.

Adding the "<HTML>" and "</HTML>" around the FCKeditor gernetated field seems to assist some mail readers in interpreting the message content as nicely formatted HTML.

Hope this helps anyone doing PHP HTML merge mailings

Cheers...


View profile  Send private message
datadoit.com
Posted: 07/10/2007, 5:20 AM

Nice trick! Thanks for sharing.
jacem

Posts: 87
Posted: 07/10/2007, 2:41 PM

Thanks for the feedback.

Had another client sign up for this same functionality to their web database the other day but had an additional requirement: they wanted to have a database variable (company name) in each subject line. So same trick just do the same str_replace on the $subject variable as well as the $messagetext variable and it works a treat.
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.