inserting data and images in an email message variable

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

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

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: [gkornbluth] inserting data and images in an email message variable

Hi Jerry,

On line 73 you have:

<?php if($the_to = $record['email']) : ?>

Which I think would always default to true as it can always set $the_to variable to be $record['email']. Should it actually be:

<?php if($the_to == $record['email']) : ?>

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

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

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

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

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

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

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