whitedragon101 Posted January 22, 2011 Share Posted January 22, 2011 I have tried the top 4 or 5 scripts from googling "send html email using php." Every single one results in the html markup itself being displayed as text, not as a styled page. I have many emails from companies where no matter what client I view it on I see a fully styled webpage with links and pics. How can this be achieved with php? Quote Link to comment https://forums.phpfreaks.com/topic/225274-sending-html-email/ Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Why not show us some of your problematic code and we'll go from there? Quote Link to comment https://forums.phpfreaks.com/topic/225274-sending-html-email/#findComment-1163398 Share on other sites More sharing options...
whitedragon101 Posted January 22, 2011 Author Share Posted January 22, 2011 I am looping through the emails so there is a do loop at the bottom of each getting a new address for each MySQL row. All three codes send the email, and all three only display the html markup itself but not styled text. Code 1 //define the receiver of the email //define the subject of the email $subject = 'Test HTML email'; //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: $from\r\nReply-To: $from"; //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/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --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(); do{ $email= $row_email['email']; $to=$email; $sentmail = @mail($to,$subject,$message,$headers); } while($row_email = mysql_fetch_assoc($email_result)); Code 2 $from = $row_company_details['email']; $name=$_POST["name"]; $subject="hello"; //$message = file_get_contents('http://www.holtrecruitment.com/TEST_RECRUITMENT/webpages/management_area/newsletter.php'); $message = "<p>Hello</p>"; $headers="From:$name <$from>\r\n"; $headers .= "Reply-To: $from\r\n"; $headers .= "Date: " . date("r") . "\r\n"; $headers .= "Return-Path: $from\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Message-ID: " . date("r") . $_SERVER["name"]."\r\n"; $headers .= "Content-Type: text/html; charset=utf-8\r\n"; $headers .= "X-Priority: 1\r\n"; $headers .= "Importance: High\r\n"; $headers .= "X-MXMail-Priority: High\r\n"; $headers .= "X-Mailer: PHP Mailer 1.0\r\n"; //////////////////// //message do{ $email= $row_email['email']; $to=$email; $sentmail = mail($to,$subject,$message,$header); } while($row_email = mysql_fetch_assoc($email_result)); Code 3 $from = $row_company_details['email']; // $HTML = file_get_contents('http://www.holtrecruitment.com/TEST_RECRUITMENT/webpages/management_area/newsletter.php'); $HTML = "<p>THis is a test</p>"; $headers = "From: $from\r\n"; // Now we specify our MIME version $headers .= "MIME-Version: 1.0\r\n"; // Create a boundary so we know where to look for // the start of the data $boundary = uniqid("HTMLEMAIL"); // First we be nice and send a non-html version of our email $headers .= "Content-Type: multipart/alternative;". "boundary = $boundary\r\n\r\n"; $headers .= "This is a MIME encoded message.\r\n\r\n"; $headers .= "--$boundary\r\n". "Content-Type: text/plain; charset=ISO-8859-1\r\n". "Content-Transfer-Encoding: base64\r\n\r\n"; $headers .= chunk_split(base64_encode(strip_tags($HTML))); // Now we attach the HTML version $headers .= "--$boundary\r\n". "Content-Type: text/html; charset=ISO-8859-1\r\n". "Content-Transfer-Encoding: base64\r\n\r\n"; $headers .= chunk_split(base64_encode($HTML)); do{ $email= $row_email['email']; $to=$email; $sentmail = mail($to,$subject,"",$header); } while($row_email = mysql_fetch_assoc($email_result)); Quote Link to comment https://forums.phpfreaks.com/topic/225274-sending-html-email/#findComment-1163400 Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Code 2 would work except you passing the variable $header to the mail functions instead of $headers. You'll also want a while {} no a do {} while, because your first loop won't have $row_email['email'] defined otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/225274-sending-html-email/#findComment-1163407 Share on other sites More sharing options...
whitedragon101 Posted January 22, 2011 Author Share Posted January 22, 2011 Code 2 would work except you passing the variable $header to the mail functions instead of $headers. Bingo it works. Its always the stupid stuff that gets you Is this how these styled corporate emails are generally done. Just send out html in the email itself? Or do they use an iFrame or other such container linked to an html page on their servers? You'll also want a while {} no a do {} while, because your first loop won't have $row_email['email'] defined otherwise. I have already got the first row out when I wrote the MySQL query. I only do it that way so when I do my loops down the page I can just copy and paste the row name $query_email = "SELECT email FROM customer WHERE email_yes_or_no != 'no' "; $email_result = mysql_query($query_email, $jobconnect) or die(mysql_error()); $row_email = mysql_fetch_assoc($email_result); $totalRows_email_result = mysql_num_rows($email_result); Thanks thorpe Quote Link to comment https://forums.phpfreaks.com/topic/225274-sending-html-email/#findComment-1163411 Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Is this how these styled corporate emails are generally done. Just send out html in the email itself? Or do they use an iFrame or other such container linked to an html page on their servers? This is how we send me where I work. Your images will need to be hosted online somewhere obviously. You can also use images to keep track of how many people are opening the emails. Quote Link to comment https://forums.phpfreaks.com/topic/225274-sending-html-email/#findComment-1163415 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.