inserting data and images in an email message variable

17 posts by 3 authors in: Forums > CMS Builder
Last Post: November 15, 2012   (RSS)

By gkornbluth - October 21, 2012

Hi All,

I’m using the following to send emails to exhibition entrants and it works fine with just HTML in the email $message variable.

However, I need to replace part of the $message variable with some PHP code to show images and other data, and I can’t seem to figure out how to do that.

Here's the plain vanilla version:

<?php foreach($exhibition_email_testRecords as $record) : ?>

<?php $first_name = $record['first_name'] ; ?>
<?php $last_name = $record['last_name'] ; ?>
<?php


$the_to = $record['email'];
$the_from = "exhibition-submissions@artistsofpalmbeachcounty.org";
$to = "$the_to";
$from = "$the_from";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";

$subject = "Your submission to the $where Exhibition has been Juried";
$message = <<<EOF
<html>
<style type="text/css">
.body-text {font-family:Arial; font-size: 1em; }

</style>

<body bgcolor='#7C7164'>
<div align='left'> <img src="http://www.artistsofpalmbeachcounty.org/images/APBC-LOGO.png" width="800" height="183" /></div><br /><br />
<div align='left' class='body-text'>
Hello $first_name $last_name,
<br /><br />
Here is a message about your submission. (Need to insert PHP code here...)
<br /><br />
Best,
<br /><br />
The Exhibition Committee<br />
Artists of Palm Beach County</div>
</body>
</html>
EOF;
//end of message
mail($to,$subject,$message,$headers);

?>

<?php endforeach ?>


Here’s the code that I need to insert:
<?php $instructionInformation = array(); ?>
<?php foreach ($record['uploads'] as $upload): ?>
<?php $recordnumber = $upload["recordNum"] ?>
<?php if($upload['info5'] == "1") : ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" /> <br />
<?php // get the instructions for this particular image
$instructions = mysql_get("email_image_information",null,"file_name = '".$upload['filename']."' AND project_title = '".$record['project_title']."'");

// Get only the instructions we want to show - in this case, the fields we want start with 'complete_'
foreach ( $instructions as $field => $value ) {

if ( preg_match("/^complete_/",$field) ) {

// Replace all the underscores with spaces and remove 'complete_' from the field name
// then capitalize all the individual words so we can use this as a proper label
$field = str_replace("complete_","",$field);
$field = str_replace("_"," ",$field);
$field = ucwords($field);

// Add this extra info to the appropriate upload in the exhibition_email_testRecords array
// in it's own array called 'email_image_information'
$exhibition_email_testRecord['uploads'][$key]['email_image_information'][$field] = $value;
$instructionInformation[$field] = $value;

} }
?>
<?php foreach ( $instructionInformation as $field => $instruction ) : ?>
<?php if ( @$instruction && $field == 'Title') : ?>
<?php // Replace all double quotes with single quotes
$instruction = preg_replace("[\"]", "''", $instruction); ?>
<div ><?php echo $field.': '.$instruction; ?></div>
<?php endif; ?>
<?php endforeach; ?>
Filename: <?php echo $upload['filename'] ?><br />
<?php endif; ?>
<?php endforeach ?>


I've attached a full test page that will pull all the required information from the database (exhibition_email.php) and one that will send the plain vanilla emails to the correct recipients (exhibition_email2.php).

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] inserting data and images in an email message variable

By Dave - October 21, 2012

Hi Jerry,

Here's a simple trick for capturing the output of some PHP code into a variable.

Say you have some content in a page like this that is a mix of HTML and PHP:
<b>Hello World</b>, 1 + 1 = <i><?php echo 1+1; ?></i>

Normally that content is output to the screen, but you can capture it to a variable using PHP output buffering. Just add <?php ob_start(); ?> when you want to turn on output buffering, and <?php $output = ob_get_clean(); ?> when you want to turn it off an assign it to a variable.

Here's an example:
<?php ob_start(); // start capturing output ?>
<b>Hello World</b>, 1 + 1 = <i><?php echo 1+1; ?></i>
<?php $output = ob_get_clean(); // stop capturing output ?>

<?php print "Nothing was output yet, because we captured this: $output<br/>The end.<br/>"; ?>


I think if you run your code before you define the $message variable, you could capture it's output and assign it to another variable such as $instructions and then include that variable in the $message content.

Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] inserting data and images in an email message variable

By gkornbluth - October 22, 2012

Hi Dave,

Thanks for the tips.

I surrounded the code with the ob_start and ob_get_clean tags as you suggested, inserted the $output variables in the $message variable, and all of the images and information now displays in the email.

I've got one more challenge.

Although separate emails are sent to each entrant, and in each email the $first_name and $last name fields match the $the_to field for each of those records, I can’t seem to limit the images and information in the emails to only that record’s information.

I’ve attached my latest attempt in case something jumps out.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

exhibition_email12.php 10K

Re: [greg] inserting data and images in an email message variable

By gkornbluth - October 22, 2012

Well, Greg,

That was pretty careless on my part. Thanks for catching it.

Have you got a trick up your sleeve that can:

Hide line 72 in the revised attached file (CONGRATULATIONS, THESE WORKS...), when there are no images in that record with an info5 field value equal to 0 (zero or empty)?

Or, hide line 116 in the revised attached file (UNFORTUNATELY, THESE WORKS...), when there are no images in that record with an info5 field value not equal to 1?

I’ve tried a number of ‘if’ variants and can’t get the empty ones to hide.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

exhibition_email17.php 9K

Re: [gkornbluth] inserting data and images in an email message variable

By gregThomas - October 23, 2012

Hi Jerry,

It's a very easy mistake to make!

Something like this should work:

<?php if(@$record['uploads']): ?>
<span class='heading-text-yellow'>UNFORTUNATELY THESE WORKS WERE NOT ACCEPTED BY THE JURORS FOR INCLUSION INTO THE <?php echo strtoupper($where) ?> EXHIBITION (17).</span><br />
<?php endif; ?>


In a PHP if statement an empty array is equal to false and an array with something in it is equal to true.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] inserting data and images in an email message variable

By gkornbluth - October 23, 2012

Greg,

Thanks, your suggestion got me thinking in the right direction.

I ended up having to add another set of foreach loops, the correct limiting parameters as if statements, and a <?php break ?> into the mix, but it's now working as planned.

Again, thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Dave] inserting data and images in an email message variable

By gkornbluth - November 13, 2012

Hi Dave, Greg, All,

I’ve encountered a strange situation that I hope someone can shed some light on.

I’m using the suggested technique to generate acceptance emails for entrants to an art exhibition. The emails include thumbnails of all of the entrant's accepted and rejected works.

Everything works perfectly when I test the system (with one entrant) and all of the images always displayed correctly.

This morning I sent emails to all (75) exhibition entrants and some of the images in the received emails did not display.

When I looked at the source code of one of the emails where the images had problems, I saw this for one image that did not display:<img src="http://www.ar!
tistsofpalmbeachcounty.org/cmsAdmin/uploads/customers/thumb/Mirage.jpg" height="100" width="150">
This for another (with no <img tag):src="http://www.artistsofpalmbeachco!
unty.org/cmsAdmin/uploads/customers/thumb/Gilded-Age.jpg" height="150" width="113">
and this for the one that displayed correctly: <img src="http://www.artistsofpalmbeachcounty.org/cmsAdmin/uploads/customers/thumb/Voyagers.jpg" height="100" width="150">

I also noticed a few sporadic exclamation points (!) in the email source code.

I’ve attached a copy of both the email source code and the page code.

Thanks for any insights.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] inserting data and images in an email message variable

By gregThomas - November 13, 2012

Hi Jerry,

I've done a bit of reseach and the problem is most likely to be when using the mail function you shouldn't have a line with more than 955 characters on or you get the issue your describing. I've found a good thread on how to get around the issue here:

http://www.daniweb.com/web-development/php/threads/75331/exclamation-points-at-odd-places-in-php-email

If you don't want to encode your mail in base64, you could also try adding \r\n to the end of every line on your $message variable, and breaking up the content onto more lines.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] inserting data and images in an email message variable

By gkornbluth - November 13, 2012 - edited: November 13, 2012

Hi Greg,

Thanks for looking into this.

I created some test records (4) with the same data that was giving errors when sent emails to the large group and could not duplicate the email errors that I was experiencing.

So... Since I really can't send emails to the entire group as a test, and to cover as many bases as I could think of, I followed your suggestion and added an \r\n to the end of some of the lines in the message body.

And... Based on a suggestion at the end of the article you found, I added the following to the HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />

Also... On a hunch that there might be too much data being submitted too quickly, after:
mail($to,$subject,$message,$headers);
I added the following:
// pause script for .1 sec
usleep(100000);
// Reset max_execution_time to 30 sec
set_time_limit(30);


Although these do not seem to break anything in the test emails, I’m curious about:

A) Your thoughts on the approach I used, and
B) The correctness of the code changes I made and whether you think anything else should be added.

The complete revised code is:
<?php
if ($FORM['where'] && $_REQUEST['send_this'] == '1' && $allow_send == '1'):?>
<?php $email_count++ ; ?>
<?php
$the_from = "exhibition-submissions@artistsofpalmbeachcounty.org";
$to = "$the_to";
$from = "$the_from";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$subject = "Your submission to the $where Exhibition has been Juried";
$message = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.body-text {font-family:Arial; font-size: 1.0em;}
.body-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.0em;}
.heading-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.3em;}
</style>
</head>
<body bgcolor="#7C7164">
<table style='background-color: #7C7164;' width='100%' align='center' border='0' >
<tr>
<td align='left' >
<table style='background-color: #7C7164;' width='70%' align='center' border='0' >
<tr>
<td align='left' >
<div align='left'><img src='http://www.artistsofpalmbeachcounty.org/exhibition_email_open.php?num=$recnum' height='1' width='1' alt='' /><img src='http://www.artistsofpalmbeachcounty.org/images/APBC-LOGO.png' width='800' height='183' style='border:hidden'/></div><br /><br />
<div align='left' class='body-text-yellow'>
Hello $first_name $last_name,
<br /><br />
$output \r\n<br /><br />$output2 \r\n
<br /><br />
Any artwork that has been accepted into the $where exhibition can only be delivered to the exhibition site on $art_drop_off_date between $art_drop_off_start_time and $art_drop_off_time_end.\r\n
<br /><br />You can review the submission specifics for this exhibition <a href='http://www.artistsofpalmbeachcounty.org/exhibitionsubmissiondetail.php?num=$ehib_recnum' /><span class='body-text-yellow'><u>Here</u></span></a>.\r\n<br /><br />
Thank you for submitting your work to this exhibition,
<br /><br />
The Exhibition Committee<br />
Artists of Palm Beach County\r\n</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
EOF;
//end of message
mail($to,$subject,$message,$headers);
// pause script by .1 sec
usleep(100000);
// reset max_execution_time to 30 sec
set_time_limit(30);
?>
<?php endif; ?>


Thanks again.

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php