我是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服务器中部署。 提前谢谢。
答案 0 :(得分:-1)
<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";
}
}
?>