Jump to content

Send HTML email


bravo14
 Share

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:-

 

<?php
//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

 

<?php
//connection
include_once('includes/connect_inc.php');
//declare variables from registration form
if(isset($_POST['Submit']))
{
$email=$_POST['user_name'];
$password=$_POST['password'];
$trader=$_POST['trader'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$address3=$_POST['address3'];
$address4=$_POST['address4'];
$address5=$_POST['address5'];
$postcode=$_POST['post_code'];
$contact_name=$_POST['contact_name'];
$vat_num='GB'.$_POST['vat_num'];
$mobile=$_POST['mobile'];
$phone_number=$_POST['phone'];
$subscribe=$_POST['subscribe'];
//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
if($subscribe=="subscribe"){
$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.";
include_once('includes/emails/user_reg_inc.php');
}
}
?>

 

below is the code for the email

 

<?php
$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;
boundary="{$hash}"
WIZ;

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

This is a multi-part message in MIME format.

--$hash
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$txt_message
--$hash
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$html_message

--$hash--
WIZ;

if(mail($email,$subject,$body,$headers)){

    echo "Email Sent";

}else{

    echo "Mail not sent";

} 

?>

 

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

 

Cheers

 

Mark

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

Guest
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.

 Share

×
×
  • 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.