如果我在标题中使用MIME类型和版本,则PHP表单不会发送邮件。如何解决这一问题?

时间:2014-09-02 作者:Riffaz Starr

如果我使用以下代码,那么表单工作正常,我会将表单提交到我的电子邮件中。

<?php

if($_POST["submit"]) {
    $recipient="[email protected]";
    $subject="New request";
    $headers  = \'MIME-Version: 1.0\' . "\\r\\n";
    $headers .= \'Content-type: text/html; charset=iso-8859-1\' . "\\r\\n";
    $sender=$_POST["sendername"];
    $senderEmail=$_POST["senderEmail"];
    $message=$_POST["message"];
    $name_title=$_POST["name_title"];   

    $mailBody = "<b>Name:</b> $name_title $sender\\n
                 <b>Email:</b> $senderEmail\\n\\n
                 <b>Message:</b> $message";

    $mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
}

if ($mail_sent) {
?>
    <p>Mail sent</p>
<?php } ?>
如果我更改$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"); TO $mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);, 然后表单停止工作。问题是什么?

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

From: 只是另一个邮件头。您需要将其与当前$headers 并将其作为一个参数传递给mail():

$headers  = "From: $sender <$senderEmail>\\r\\n";
$headers .= "MIME-Version: 1.0\\r\\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\\r\\n";

// la de da

mail( $recipient, $subject, $mailBody, $headers );

结束

相关推荐

恢复CForms II表单预置

我有个客户不得不重新激活cforms II contact forms plugin. 它拒绝并给出以下错误:*Fatal error*: Cannot use string offset as an array in */var/www/vhosts/site.com/httpdocs/wp-content/plugins/cforms/lib_activate.php* <http://site.com/httpdocs/wp-content/plugins/cform