Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding to send html email 1

Status
Not open for further replies.

sinbadly

Technical User
Mar 16, 2006
126
GB
The following code sends out my weekly newsletters as text emails. I'd like to send the newsletters in html.

Be v grateful if someone could show me what code is necessary to send html newsletters, please.

$query = 'SELECT * FROM reg3';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result)) // was _array tried _assoc
{
$subject = "Willowfield newsletter - Sunday January 7 2007.";
$message = "Dear $row[username],\n


...newsletter here ...

\r\n";
$headers = 'From: Willowfield School website';
$message.= "(Thank you, $row[fname] $row[sname], for being part of Willowfield’s email newsletter list.)";
mail($row, $subject, $message, $headers);
$subject.="copies from " . $row[email];
mail("my_email_address", $subject, $message, $headers);
}
 
you could use phpmailer or try this code. you'll need to enclose most of it in a loop for you database recordset. overall i'd recommend phpmailer which is far more comprehensive than the below.

Code:
//stub some basic parameters
$to = "          ";
$from = "        ";
$subject = "test subject";

//set up the message text
$defaultmessage = "Your mail client is too old to read html. Get a newer one";

$plaintextmessage = "Hello world";

$htmlmessage = '<div><span style="color:blue;">Hello</span><span style="color:red"> World</span></div>';

//set up the boundaries
$boundary = md5(uniqid("",true));
$b2 = "b2---$boundary";

//separator
$sep = "\n";
//set up the mail header

$headers  = "From: $from$sep";
$headers .= "To: $to$sep";
$headers .= "Return-Path: $from$sep";
$headers .= "MIME-Version: 1.0$sep";
$headers .= "Content-Type: multipart/alternative; boundary=\"$b2\"$sep";
$headers .= "$sep";    

//now set up the message   

//default message
$message .= $defaultmessage.$sep;
$message .= "$sep";    

//plaintext message

$message .= "--$b2$sep";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"$sep";
$message .= "Content-Transfer-Encoding: 8bit$sep";
$message .= "$sep";
$message .= $plaintextmessage;
$message .= "$sep";

//html message
$message .= "--$b2$sep";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"$sep";
$message .= "Content-Transfer-Encoding: 8bit$sep";
$message .= "$sep";
$message .= $htmlmessage;
$message .= "$sep";

//finish the mail

$message .= "--$b2--$sep";

$result = mail ($to, $subject, $message, $headers);

if ($result === FALSE) {
    echo "email was not sent";
} else {
    echo "email was sent";
}
?>
 
Looks very good, Justin. Will study it tonight. I am most grateful - again. All the best. paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top