2016-11-24 131 views
0

我有PHP代码来发送邮件是这样的:发送HTML邮件用PHP

<?php 
    $to='[email protected]'; 
    $subject = 'testing'; 
    //create a boundary string. It must be unique 
    $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/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?> 
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit 

    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit 

    <?php 
//html content 
    include ("http://10.*.*.*/maps/tes.php"); 
    ?> 

    --PHP-alt-<?php echo $random_hash; ?>-- 

    --PHP-mixed-<?php echo $random_hash; ?> 
    #Content-Type: application/html; name="racing_mkios_data_wok_sbt_201403.html" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 

    --PHP-mixed-<?php echo $random_hash; ?>-- 

    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    $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"; 
    ?> 

它给我的HTML代码而不是内容。我的代码有什么不对? 我一直在谷歌搜索一小时仍然无法找到答案 .................................. ..........................

+0

使你的服务器肯定sendmail的作品。如果不使用SMTP。 –

回答

1

尝试将您的标题内容类型更改为text/html。 转换此行:

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

到:

$headers .= "\r\nContent-Type: text/html; boundary=\"PHP-mixed-".$random_hash."\""; 
+0

感谢@kerv的工作。你能解释我吗? –

+0

PHP邮件功能只需将其内容类型设置为text/html即可发送HTML邮件。更多可以在这里找到:http://php.net/manual/en/function.mail.php – kerv