php PHPMailer“$mail->MsgHTML($msg)”问题与“$msg”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10373603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPMailer "$mail->MsgHTML($msg)" issue with "$msg"
提问by unK
I'm asking here because I did not got an answer from OVH (my hosting). Here is the problem : If I replace $mail->MsgHTML($msg) with $mail->MsgHTML($_POST['message']), I receive the mail instantly with headers, name, email, subject and the message. But when I put $msg instead, I receive no mail.
我在这里问是因为我没有从 OVH(我的托管)那里得到答案。问题是:如果我用 $mail->MsgHTML($_POST['message']) 替换 $mail->MsgHTML($msg),我会立即收到带有标题、姓名、电子邮件、主题和消息的邮件。但是当我输入 $msg 时,我没有收到邮件。
$msg='Name:'.$_POST['name'].'<br />
Email:'.$_POST['email'].'<br />
Subject: '.$_POST['subject'].'<br />
IP:'.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
entire PHP (from FancyAJAXForm):
整个 PHP(来自 FancyAJAXForm):
<?php
/* config start */
$emailAddress = 'my mail address';
/* config end */
require "class.phpmailer.php";
foreach($_POST as $k=>$v)
{
if(ini_get('magic_quotes_gpc'))
$_POST[$k]=stripslashes($_POST[$k]);
$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}
$msg='Name:'.$_POST['name'].'<br />
Email:'.$_POST['email'].'<br />
Subject: '.$_POST['subject'].'<br />
IP:'.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "Contact Form: ".mb_strtolower($_POST['subject'])." from ".$_POST['name']."";
$mail->MsgHTML($msg);
$mail->Send();
?>
回答by unK
Ok I got the problem solved.
好的,我解决了问题。
I've replace $mail->msgHTML($msg) with the body function :
我已经用 body 函数替换了 $mail->msgHTML($msg) :
$mail->IsHTML(true);
$mail->Body='Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Sujet: '.$_POST['subject'].'
<br /><br />
'.nl2br($_POST['message']).'
<br /><br />
Browser: '.$_SERVER['HTTP_USER_AGENT'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br />
';
$mail->Send();
It works perfectly! Thanks to all participants!
它完美地工作!感谢所有参与者!
回答by Grenoble
I know this isn't exactly timely, but I found an alternative solution:
我知道这并不完全及时,但我找到了一个替代解决方案:
I had a similar problem, but I had some pages that worked and some that didn't. I tried your solution, but it gave me the same results.
我有一个类似的问题,但我有一些页面可以工作,有些页面没有。我尝试了您的解决方案,但它给了我相同的结果。
Then I looked at the html source of the emails from the working pages, and noticed that I had included the opening and closing html and body tags, and I hadn't included them in the non-working pages. That's all it took, and $mail->msgHTML($msg) worked for me.
然后我查看了来自工作页面的电子邮件的 html 源代码,并注意到我已经包含了开始和结束 html 以及正文标签,而我没有将它们包含在非工作页面中。这就是全部, $mail->msgHTML($msg) 为我工作。
Hope this helps.
希望这可以帮助。

