PHP Mailer函数不工作


PHP Mailer Function not working

我是PHP新手,有一个发送邮件的表单。下面是PHP代码,我得到消息"无法发送消息正文为空的消息。

邮件错误:"我一整天都在谷歌上搜索这个问题,但一直没能弄清楚。

我不理解"消息正文为空"这一部分。我检查了$mail.body,它有值。

<?php
require_once("class.PHPMailer.php");
 $mail = new phpmailer();
 $mail->issmtp();      // set mailer to use smtp
 $mail->host = "smtp.office365.com";  // specify main and backup server
 $mail->smtpauth = true;     // turn on smtp authentication
 $mail->username = "noreply@test.com";  // smtp username
 $mail->password = "test123"; // smtp password
 $mail->from = "noreply@test.com";
 $mail->fromname = "no reply";
 $mail->addaddress("johndoe@test.com", "John Doe");
 $mail->wordwrap = 50;                                 // set word wrap to 50 characters
 //$mail->addattachment("/var/tmp/file.tar.gz");         // add attachments
 //$mail->addattachment("/tmp/image.jpg", "new.jpg");    // optional name
 $mail->ishtml(true);                                  // set email format to html
 $mail->subject = "here is the subject";
 $mail->body    = "this is the html message body <b>in bold!</b>";
 $mail->altbody = "this is the body in plain text for non-html mail clients";
echo $mail->subject;
 try{
 if(!$mail->send())
 {
  echo "message could not be sent. <p>";
  echo "mailer error: " . $mail->errorinfo;
  exit;
 }
 echo 'after mail send';
 }
 catch(exception $e) {
  echo 'caught exception: ',  $e->getmessage(), "'n";
}
echo "message has been sent";
?>

我还部署在windows服务器中。提前谢谢。

<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
  ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  From: <input type="text" name="from"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="Submit Feedback">
  </form>
  <?php
} else {    // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    $from = $_POST["from"]; // sender
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    // message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
    // send mail
    mail("webmaster@example.com",$subject,$message,"From: $from'n");
    echo "Thank you for sending us feedback";
  }
}
?>