【问题标题】:PHP mailer (message body empty)PHP 邮件程序(邮件正文为空)
【发布时间】:2013-03-27 12:20:53
【问题描述】:

我是 PHPMailer 的新手,我想用这个类发送 HTML 邮件。但我收到消息说正文是空的。

这是我的代码:

<?php        

$bericht .= 'my html and php code that format the mail';

require_once('class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = preg_replace('/[\]/','',$bericht);

$mail->SetFrom('email@adres', 'Name');

$address = "email@adres";
$mail->AddAddress($address, "");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);


if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

我做错了什么?

【问题讨论】:

  • 您是否尝试显示您的$body 以确保它确实包含某些内容?也许之前有问题(例如,在填充$bericht 的部分中)
  • 考虑使用&lt;?php 而不是&lt;?

标签: php phpmailer


【解决方案1】:

我认为您的preg_replace() 有问题。如果我尝试在我的服务器上运行它,我会收到以下警告:

警告:preg_replace():编译失败:偏移 3 处的字符类缺少终止]

您是否尝试过不带preg_replace() 的代码,即仅将$bericht 传递给MsgHTML()

【讨论】:

  • 就是这样,奇怪,因为它直接来自 phpmailer 示例文件:)
  • 转义 [ 字符,如 preg_replace('/\[\]/','',$bericht)
【解决方案2】:

我认为你的代码有错误

$bericht .= (my html and php code that format the mail);

应该是

$bericht = 'my html and php code that format the mail';

然后代替这个

$body             = $bericht;
$body             = preg_replace('/[\]/','',$body);

这样做更容易

$body             = preg_replace('/\[\]/','',$bericht);

【讨论】:

  • 尝试打印或回显正文以查看其内容
  • 我对我的代码进行了更改,检查一下,你需要转义 [ 字符,复制你使用 preg_replace 的行,现在应该可以工作了
【解决方案3】:

我误解了如果不需要变量,为什么需要对变量执行此 INCREMENT。

$bericht .= ...;

是的,引号中的字符串值在哪里?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 2013-12-19
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    相关资源
    最近更新 更多