PHP邮件程序不能以HTML格式运行

时间:2012-08-21 19:02:19

标签: php html email

我使用的是一个非常标准的电子邮件创建功能,我之前使用过,但由于某些原因我无法理解它不起作用。无论我在里面放什么内容,它都会发送一封空白的电子邮件。

function sendToUser($email,$admin_email,$subject,$content){
    $to=$email;

    $random_hash = md5(date('r', time())); 
    $headers = "From: Site Name <$admin_email>";
    $headers .= "\r\nReply-To: $admin_email";
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
    ob_start(); //Turn on output buffering
    ?>
    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/plain; charset="utf-8"
    Content-Transfer-Encoding: 7bit

    <?php echo $content; ?>

    --PHP-alt-<?php echo $random_hash; ?>--
    <?
    //copy current buffer contents into $message variable and delete current output buffer
    $message = ob_get_clean();

    mail($to, $subject, $message, $headers);
}

现在,如果我这样称呼它:

sendToUser("myprivatemail@yahoo.com","admin@site.com","Testing","E-mail content");

它发送电子邮件,但它是空的。有谁看到这里有什么问题?或者它可能是我不熟悉的服务器设置?

1 个答案:

答案 0 :(得分:0)

--PHP-alt-<?php echo $random_hash;之前不能有空格。

试试这个:

function sendToUser($email,$admin_email,$subject,$content){
    $to=$email;

    $random_hash = md5(date('r', time())); 
    $headers = "From: Site Name <$admin_email>";
    $headers .= "\r\nReply-To: $admin_email";
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
    ob_start(); //Turn on output buffering
    ?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

<?php echo $content; ?>

--PHP-alt-<?php echo $random_hash; ?>--
    <?
    //copy current buffer contents into $message variable and delete current output buffer
    $message = ob_get_clean();

    mail($to, $subject, $message, $headers);
}