Page 1 of 1

Easy Mailing Methods

Posted: Wed Mar 15, 2006 2:52 pm
by icesolid
I send out weekly e-mails to customers through my site using data stored in a database. The table is small containing the data is small right now, about 20 rows of data (more than posted in the code below). Soon I am going to need to send a huge HTML table in the e-mail, it is going to take hours for me to add backslashes to the 1000 rows of HTML code I am going to be sending out in an email. Also I want to embed $row["field1"]; right into the format, defining each field like this $field1 = $row["field1"] is going to be a pain in the ass.

Basically I am looking for an easier way to format the message_body I am sending out (maybe not setting it as an variable? I'm not sure). I am wondering if there is any easier way of accomplishing this task?

Here is my code (this code is obviously put through a loop in my live code to send to each user):

Code: Select all

<?php
$result = mysql_query("SELECT * FROM table_name WHERE id='1'");\
$row = mysql_fetch_array($result);

$subject = $row["field1"];
$headers = "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers.= "From: Company Name(weekly@companyname.com)";

$field2 = $row["field2"];
$field3 = $row["field3"];

$message_body = "
<html>
<head>
<title>Page Title</title>
</head>

<body>

<table width=\"400\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">
  <tr>
    <td align=\"center\"><font face=\"Verdana\" size=\"1\">$field2</td>
  </tr>
  <tr>
    <td align=\"center\"><font face=\"Verdana\" size=\"1\">$field3</td>
  </tr>
</table>

</body>
</html>
";

mail("emailaddr@company.com", $subject, $message_body, $headers);
?>

Posted: Wed Mar 15, 2006 3:13 pm
by matthijs
Use single quotes or heredoc?

Posted: Wed Mar 15, 2006 5:22 pm
by mickd
"defining each field like this $field1 = $row["field1"] is going to be a pain in the ass"

Variable Variables may be of help

Posted: Wed Mar 15, 2006 6:44 pm
by icesolid
None of those suggestions really help me out.

I was however looking into PEAR to send mail, is that any easier as far as formatting goes?

From what I have looked at, PEAR can get pretty complicated and in my situation all I am looking for is something simple.

I just want an easier way of formatting the BODY of an E-mail being sent.

Posted: Thu Mar 16, 2006 12:35 am
by matthijs
Why don't the solutions work for you?

Otherwise, if you're looking for a good mailing class: check out phpmailer. Very easy to use and supposed to very solid. Example use:

Code: Select all

<?php
// Grab our config settings
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');

// Grab the FreakMailer class
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc');

// Setup body
$textBody = "Dear {MEMBER_NAME},\n\nCheck out PHP Freaks: http://www.phpfreaks.com\n\nSincerely,\nAdmin";
$htmlBody = "Dear {MEMBER_NAME},<br /><br />Check out PHP Freaks: http://www.phpfreaks.com<br /><br />Sincerely,<br />Admin";


// instantiate the class
$mailer = new FreakMailer();

// Get the user's Email
$sql = mysql_query("SELECT FirstName,LastName,EmailAddress,MailType FROM users WHERE 1");

while($row = mysql_fetch_object($sql))
{
  // Send the emails in this loop.
  $member_name = $row->FirstName;
  if(!empty($row->LastName))
  {
    $member_name .= ' '.$row->LastName;
  }
  
  if($row->MailType == 'html')
  {
    $mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $htmlBody);
    $mailer->IsHTML(true);
    $mailer->AltBody = str_replace('{MEMBER_NAME}', $member_name, $textBody);
  }
  else
  {
    $mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $textBody);
    $mailer->isHTML(false);
  }
  $mailer->Send();
  $mailer->ClearAddresses();
  $mailer->ClearAttachments();
  $mailer->IsHTML(false);
  echo "Mail sent to: $member_name<br />";
}

?>
http://www.phpfreaks.com/tutorials/130/8.php