Jump to content

Send HTML email


Recommended Posts

Hi guys


I am trying to send an HTML email using the following code, adapting some code I found on a tutorial site:-


//define the receiver of the email
$to = $email;
//define the subject of the email
$subject = 'Trade-Bidz Registration'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: registrations@trade-bidz.co.uk\r\nReply-To: registrations@trade-bidz.co.uk";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>--
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";


The code processes, but no email is received, any idea what I am doing wrong/missed out?

Link to comment
Share on other sites

  • 4 weeks later...

Hi Guys


Sorry about the delay in posting the code


//declare variables from registration form
//add user to database
$add_user_sql=mysql_query("INSERT into `tbl_users` (`emailaddress`,`password`,`trader_name`,`address1`,`address2`,`address3`,`address4`,`address5`,`post_code`,`phone_number`,`mobile_number`,`vat_num`,`contact_name`,`account_status`) 
values ('$email',md5('$password'),'$trader','$address1','$address2','$address3','$address4','$address5','$post_code','$phone_number','$mobile','$vat_num','$contact_name','Active')");
$message="Thank you for registering with Trade-Bidz.co.uk";
//add to newsletter table if required
$add_email_newsletter=mysql_query("INSERT into `tbl_subscribers` (`email`,`contact_name`,`trader`) values ('$email','$contact_name','$trader')");
$message=$message." You have been added to the distribution list, to receive Trade-Bidz updates.";


below is the code for the email


$subject = "Trade-Bidz Registration Confirmation";
$hash = md5(rand().time().rand());

$txt_message = "Older email clients will receive this message as default";
$html_message = "<h2>Hello from Wizecho!</h2><p>This is the actual email you will receive with <b>HTML</b> <i>formatting.</i></p>";

$headers = <<<WIZ
From: Trade-Bidz <registrations@trade-bidz.co.uk>
Reply-To: noreply@trade-bidz.co.uk
MIME-Version: 1.0
Content-Type: multipart/alternative;

$body = <<<WIZ
MIME-Version: 1.0
Content-Type: multipart/alternative;

This is a multi-part message in MIME format.

Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable

Content-Type: text/html;
Content-Transfer-Encoding: quoted-printable




    echo "Email Sent";


    echo "Mail not sent";




I am not getting any email, is there anything wrong with the code?





Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.