6

I am trying to send a simple HTML e-mail from PHP. The code below simply results in a blank e-mail in GMail. It also has an empty attachment called 'noname', which is not at all what I want; though that might just be a symptom of it not working.

The code I am using is:

<?php
//define the receiver of the email
$to = '[email protected]';
//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: [email protected]\r\nReply-To: [email protected]";
//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; ?> 
MIME-Version: 1.0
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; ?> 
MIME-Version: 1.0
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";
0

7 Answers 7

9

If possible use the PHPMailer class. It will greatly simplify your work.

7

It turns out the key is the encoding type. Instead of:

Content-Type: text/plain; charset="iso-8859-1"

I needed to use:

Content-Type: text/plain; charset=us-ascii

It might depend on stuff as detailed as how you save the PHP file in your own text editor. I haven't looked into it, but the iconv function in PHP may have brought me some joy too. So I think this part is really sensitive.

Here is a better snippet of sample code that shows the whole thing end-to-end:

$notice_text = "This is a multi-part message in MIME format.";
$plain_text = "This is a plain text email.\r\nIt is very cool.";
$html_text = "<html><body>This is an <b style='color:purple'>HTML</b> text email.\r\nIt is very cool.</body></html>";

$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$to = "Me <[email protected]>";
$from = "Me.com <[email protected]>";
$subject = "My Email";

$body = "$notice_text

--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$plain_text

--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

$html_text

--$mime_boundary--";

if (@mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/alternative;\n" .
    "     boundary=" . $mime_boundary_header))
    echo "Email sent successfully.";
else
    echo "Email NOT sent successfully!";

exit;

-Kevin

6

You have to specify the mime-type within the headers-parameter of the mail-function. Add this:

$header  .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

It's also shown in the PHP-Documentation for the mail-function. See example 4

1
  • This won't work, because I need a multi-part message with both test and HTML. I tried adding MIME to both, and that doesn't help, though I think you're right that it is required. So I must have an additional problem on top of that.
    – KevinM
    Jun 17, 2010 at 17:00
3

If using a library is not a problem just use Swift Mailer

2
  • I tried Pear mail to no avail. I general I am trying to minimize dependencies and actually understand how this works.
    – KevinM
    Jun 17, 2010 at 18:27
  • There are multiple examples on the Swift Mailer documentation. Unlike PEAR Mail It doesn't have dependencies
    – rmontagud
    Jun 17, 2010 at 19:52
0

There is something important I want to notice about charsets here:

"Content-Type: text/plain; charset = \"UTF-8\";\n"

is right and not only

"Content-Type: text/plain; charset=UTF-8\n"

Hope I could help other people who frustrate while searching the same mistake like me.

Remember also to write only \r for windows and not for Linux-Servers. And at the end of the header should be an extra blank line:

$headers .= "Content-Type: text/plain; charset = \"UTF-8\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "\n";
0

Following code is working for me to remove HTML Tags in mail.

$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";      
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
-1

Hope this helps you in php

$fname=$_POST['FirstName'];
$lname=$_POST['LastName'];
$dob=$_POST['DOB'];
$name=$fname.'  '.$lname;


$HTML="
<style>
<!--
.form_tbl{ border:1px solid #bbbbbb; background-color:#eeeeee;}
.form_tbl td{
    font-size:12px;
    color:#555555;
    padding:5px 5px;
}
h3{ 
    font-family:Georgia, Times New Roman, Times, serif;
    font-size:12px;
    font-weight:bold;
    color:#0099CC;
    margin:0;
    padding:0;
}
.form_tbl td select{ margin:0; padding:0;}
.tbl_brdr{ border-top:1px solid #bbbbbb; border-left:1px solid #bbbbbb;}
.tbl_brdr td{ border-bottom:1px solid #bbbbbb;  border-right:1px solid #bbbbbb; padding:5px 5px 5px 10px; }
-->
</style>

<table class='form_tbl' width='650' border='0' cellspacing='0' cellpadding='3'>

    <tr>
      <td colspan='2'><strong>PERSONAL DETAILS:</strong></td>
    </tr>
    <tr>
      <td width='250' > Name:</td>
      <td >".$name."</td>
    </tr>
    <tr>
      <td  valign='middle'>Date Of Birth:</td>
      <td  valign='top'>".$dob."</td>
    </tr>
  </table>
";

$from=$name;
$subject='Student Registration Form';
$to= '[email protected]';
//$to='[email protected]';
function sendHTMLemail($HTML,$from,$to,$subject)
{
// First we have to build our email headers
// Set out "from" address

    $headers = "From: $from\r\n"; 

// Now we specify our MIME version

    $headers .= "MIME-Version: 1.0\r\n"; 

// 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"; 
// And then send the email ....

    if (mail($to,$subject,$HTML,$headers)) 
{

  $_SESSION['msg']="Details successfully sent!";

} 
else 
{
  $_SESSION['msg']="Sending Failed! Please try again later.";

}
}
sendHTMLemail($HTML,$from,$to,$subject);
?>
1
  • This doesn't include a text part to the e-mail, so it won't work for what I need.
    – KevinM
    Jun 17, 2010 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.