Jump to content

Sending html email


whitedragon101

Recommended Posts

 

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?

Link to comment
Share on other sites

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));

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

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.

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.

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