PHP邮件程序(邮件正文为空)

PHP邮件程序(邮件正文为空),php,phpmailer,Php,Phpmailer,我是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

我是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!";
}

我想你的代码有错误

$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\u replace()
有问题。如果我尝试在我的服务器上运行此操作,我会收到以下警告:

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


您是否在不使用
preg_replace()
的情况下尝试了该代码,即仅通过将
$bericht
传递到
MsgHTML()

我误解了为什么您需要在不需要变量的情况下使用该变量进行增量操作

$bericht .= ...;

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

您是否尝试显示
$body
以确保它确实包含某些内容?之前可能有问题(例如,在填充
$bericht
的部分中),请考虑使用
尝试打印或回显正文以查看其内容。我对我的代码进行了更改,请检查,您需要退出[character,复制使用preg\u replace的行,现在应该可以使用了,就是这样,奇怪的是它直接来自phpmailer示例文件:)转义[character,比如preg\u replace('/\[\]/',''$bericht)