PHPMailer “$mail->MsgHTML($msg)” issue with “$msg”

2019-08-23 04:24发布

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.

$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
/* 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();

?>

2条回答
我只想做你的唯一
2楼-- · 2019-08-23 05:00

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.

Hope this helps.

查看更多
手持菜刀,她持情操
3楼-- · 2019-08-23 05:06

Ok I got the problem solved.

I've replace $mail->msgHTML($msg) with the body function :

$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!

查看更多
登录 后发表回答