I am trying to create an html newsletter. I searched the internet, and Mail_Mime, from what I read, is the best and easiest for sending HTML mail. I was able to create a page along with the mailer script, but not all my images are being sent, and none of the text being grabbed from my database is being sent.

Here is my code for the newsletter:

 <html>
<body style="margin: 0px; padding: 0px; font-family: Arial, Helvetica, sans-serif">
	<table cellpadding="10" cellspacing="0" width="100%" style="background-color: #69C">
		<tr>
			<td style="text-align: center"><img src="images/nb_logo.png" /></td>
			<td style="color: #FFF; text-align: center">
				<h1 style="margin: 0px; padding: 0px">Monthly Newsletter</h1>
			</td>
			<td style="color: #FFF; font-family: Arial, Helvetica, sans-serif; text-align: center">
				<h2 style="margin: 0px; padding: 0px">Volume 1, Issue 1</h2>
			</td>
		</tr>
	</table>
	<h1 style="width: 80%; margin: auto; padding: 10px 0px; text-align: center; border-bottom: 1px solid #69C">
		Latest News from NewsBlog
	</h1>
	<table style="width: 80%; margin: auto; padding: 0px">
	<?php
		include('connect.php');

	$sql = mysql_query("SELECT * FROM article_info ORDER BY sequence DESC")
		or die("Query failed : " . mysql_error());

	while($row = mysql_fetch_array($sql)) {
?>
	<tr>
		<td>
			<h1 style="margin: 0px; padding: 10px 0px"><?php echo $row['title']; ?></h1>
			<img src="<?php echo $row['photo']; ?>" style="float: left; padding: 2px; margin-right: 10px; border: 1px solid #69C" />
			<p style="margin-top: -15px"><?php echo $row['article_info']; ?></p>
		</td>
	</tr>
	<tr>
		<td><hr noshade="noshade" style="border-top: 1px solid #FFF; border-right: 1px solid #FFF; border-bottom: 1px solid #69C; border-left: 1px solid #FFF" /></td>
	</tr>
<?php } ?>
</table>
<table style="width: 80%; margin: auto; padding: 0px">
	<tr>
		<td>Unsubscribe...</td>
	</tr>
</table>
</body>
</html>

Here is the code for my mailer:

<?php
	include "connect.php";

//Using PEAR Mail_Mime
include "Mail.php";
include "Mail/mime.php";

$sql = mysql_query("SELECT * FROM article_info");
$row = mysql_fetch_array($sql);

$mime = new Mail_mime("\r\n");
$mime->setHTMLBody(file_get_contents("mailcontent.php"));

$mime->addHTMLImage("images/nb_logo.png");
$message = $mime->get();

$to = "to.test@email.com";
$hdrs = array(
	'From' => "Email Name <from.test@email.com>",
	'Subject' => "PEAR Mail_Mime HTML Email Test",
	'To' => $to
);
$headers = $mime->headers($hdrs);

$host['host'] = "email-server.satx.rr.com";
$smtp =& Mail::factory('smtp', $host);
$mail = $smtp->send($to, $headers, $message);

if(PEAR::isError($mail)) {
	echo("<p>" . $mail->getMessage() . "</p>");
} else {
	echo("<p>Message succcessfully sent!</p>");
}
?>

Any suggestions? Thanks in advance.

    This:

    $mime->setHTMLBody(file_get_contents("mailcontent.php"));

    is going to take the contents of the 'mailcontent.php' file and add it verbatim as the body of the HTML e-mail... PHP syntax and all. [man]file_get_contents/man doesn't magically evaluate the data it reads in search of PHP code it might be able to evaluate.

    Instead, you might consider building the contents of the e-mail in a (global) variable so that you can [man]include[/man] mailcontent.php and have access to that variable.

      Thanks for the reply. I apologize, but I am totally lost. How would I create global content? The first set of code is what is in mailcontent.php. I understand that the PHP syntax will fall into place, but how would I get records from my database and then output it in order to be "HTML friendly?"

        thelos999;10989216 wrote:

        How would I create global content?

        I didn't say "global content"... I said global variable. I simply mean storing all of the HTML data inside of a variable rather than outputting it. That way, when the mailcontent.php script is executed, you'll be able to reference that variable in the mail script.

        thelos999;10989216 wrote:

        The first set of code is what is in mailcontent.php.

        Yes, I realize that. As it stands out, however, that script is simply outputting all of the HTML. Instead, I'm suggesting you store that HTML in a variable.

        To make things even easier.. why even have the second script at all? Get rid of it and just use the mail script to generate the HTML and then add that HTML to the e-mail message.

        thelos999;10989216 wrote:

        I understand that the PHP syntax will fall into place, but how would I get records from my database and then output it in order to be "HTML friendly?"

        I'm not sure what you mean by "fall into place," but I'm even more confused by "HTML friendly".. so I'm not even sure what you're asking.

          I GOT IT!!! I read your comment over and over, and finally got the whole global variable thing. I went with what you said about completely taking doing away with the mailcontent.php page, and just having the HTML within the mailer script. Tested it with a small piece of HTML and MySQL code, and I was able to have my database entries within my email.

          Now, if I may bug you with one thing.

          $mime->addHTMLImage('images/nb_logo.png');
          

          This is how I am adding my images. Will I have to manually add each image, or is there a way of bringing in an array for the multiple images?

          Thank you for your help. It is greatly appreciated.

            If you have an array of filenames (e.g. if you [man]glob/man'ed an entire images folder), you could just loop through that array (e.g. [man]foreach[/man] loop) and call the addHTMLImage() function for each filename in the array.

              One thing worth considering is whether you really need to include the images directly in the email - this means they will be sent as attachments to the email, adding to the email size.

              Instead all you need to do is include a web url to the image - so upload the images to a web server first, then add the address of the image using html img tag :
              <img src='http://www.mywebsite.com/images/image.jpg'>
              (though note that if users are offline they would not see the images)

                @,

                I would do that, and I know there are risks of making the email size too large, but I was told attachments are best practices if for any reason your server is down, or if the person has recieved your email, but for some reason has lost, or has no, internet connectivity. Also, this was the first time I have ever done anything like this, so I was going with what information I had and could find out there.

                Thanks for the advice, though.

                  Write a Reply...