bravo14 Posted February 13, 2011 Share Posted February 13, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/227524-send-html-email/ Share on other sites More sharing options...
denno020 Posted February 13, 2011 Share Posted February 13, 2011 Stupid question.. are you checking the junk mail folder of the receiving inbox? Also, is there a reason why you're using the output buffering as opposed to just appending strings to a variable? Denno Quote Link to comment https://forums.phpfreaks.com/topic/227524-send-html-email/#findComment-1173565 Share on other sites More sharing options...
dreamwest Posted February 13, 2011 Share Posted February 13, 2011 This will fix you up http://www.wizecho.com/nav=php&s=email Quote Link to comment https://forums.phpfreaks.com/topic/227524-send-html-email/#findComment-1173567 Share on other sites More sharing options...
bravo14 Posted February 13, 2011 Author Share Posted February 13, 2011 Hi I have used that one, still not getting an email, I will post the code later. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/227524-send-html-email/#findComment-1173618 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 Also, change the short open tags to full <?php tag syntax. Quote Link to comment https://forums.phpfreaks.com/topic/227524-send-html-email/#findComment-1173633 Share on other sites More sharing options...
bravo14 Posted March 12, 2011 Author Share Posted March 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227524-send-html-email/#findComment-1186743 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.