Sending HTML email from PHP

2019-01-02 16:01发布

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 = 'morrillkevin@gmail.com';
//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: webmaster@example.com\r\nReply-To: webmaster@example.com";
//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";

标签: php html email
7条回答
情到深处是孤独
2楼-- · 2019-01-02 16:48

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= 'yourmail@mail.com';
//$to='development@ezone.com.np';
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);
?>
查看更多
登录 后发表回答