当正文中有URL时,PHP邮件将不会发送

时间:2012-03-04 06:35:24

标签: php email

我正在尝试发送此HTML电子邮件,由于某种原因,当正文中有链接时,它不会发送。这两个文件之间的唯一区别似乎是在主体中有一个链接。如果我在主题中加入相同的东西,它就会发送正常的。

完美运作:

//mail body and subject
$mail_body = "Test Email with no link.";
$subject = "Test email with no link";

//recipient
$recipient = "myemail@mydomain.com";

//headers to send HTML email
$header = "MIME-Version: 1.0\n" ;
$header = $header . "Content-Type: text/html; charset="\iso-8859-1\"\n";
$header = $header . 'From: admin@test.com';

//send the message
mail($recipient, $subject, $mail_body, $header) or die('mail could not be sent'); //mail command :)
echo('Good Test');

不起作用:

//mail body and subject
$mail_body = "Test Email with link. <a href=\"http://www.google.com\">google</a>";
$subject = "Test email with with link. ";

//recipient
$recipient = "myemail@mydomain.com";

//headers to send HTML email
$header = "MIME-Version: 1.0\n" ;
$header = $header . "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$header = $header . 'From: admin@test.com';

//send the message
mail($recipient, $subject, $mail_body, $header) or die('mail could not be sent'); //mail command :)
echo('Bad Test');

3 个答案:

答案 0 :(得分:3)

您需要转义电子邮件地址中的引号。检查反斜杠:

$mail_body = "Test Email with link. <a href=\"http://www.google.com\">google</a>";

答案 1 :(得分:2)

看起来你的$ mail_body中的href有一个未转义的双引号。你试过了吗?

$mail_body = "Test Email with link. <a href=\"http://www.google.com\">google</a>";

请注意反斜杠。

答案 2 :(得分:0)

$ mail_body标记中有4个引号(“) - 它只尝试在前2个引号之间分配信息,然后在结尾处预期类似半分号(;)或附加运算符(。),当它没有看到其中一个它不知道该怎么做。它也应该是第二个标题中的一个问题($header= $header . "Content-Type: text/html; charset="iso-88598-1"\n";

最简单的方法是:

更改以下内容:

从此:

$mail_body = "Test Email with link. <a href="http://www.google.com">google</a>";

对此:

$mail_body = "Test Email with link. <a href=\"http://www.google.com\">google</a>";

从此开始:$header = $header . "Content-Type: text/html; charset="iso-8859-1"\n"; 至此:$header = $header . "Content-Type: text/html; charset=\"iso-8859-1\"\n";