我试图使用PHPmailer通过电子邮件发送一个简单的表单提交(一个简单的联系我们的表单,可以通过电子邮件向我发送填写在表单中的信息)。
我让PHPmailer在之前的项目中完美运行,所以我复制并粘贴了相同的代码。现在出现的问题是php脚本似乎完全停止在addAddress代码。它之前的一切都完好无损地运行,但之后的一切似乎都没有运行。我在页面的开头运行了这个脚本,因此,我得到的只是一个空白页面。我通过在这里和那里放入随机回声来测试上述问题,然后使用虚拟信息重新提交我的表单,并且回声完全运行直到addAddress函数。
我也试过做一个try / catch函数来查看是否有任何错误,我也得到一个空白页面...不确定发生了什么,任何帮助都会非常感激。
以下是我使用的一般代码:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = '[my email host here]';
$mail->From = 'source@email.com';
$mail->FromName = 'Contact Form';
$mail->addReplyTo('actualsource@email.com', 'Contact form');
$mail->WordWrap = 250; // Set word wrap to 250
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Website Inquiry';
$fullname = $_POST['FullName'];
$phone = $_POST['phone'];
$mail = $_POST['mail'];
$message = $_POST['mess'];
$mail->Body = 'Info Request<br><br><b>Name:</b> '.$fullname.'<br><b>Phone Number:</b>'.$phone.'<br><b>Email Address:</b>'.$mail.'<br><b>Message:</b> '.$message;
//Everything after this point seems to never happen
$mail->addAddress('email1@email.com', 'John Doe1');
$mail->addAddress('email2@email.com', 'John Doe2');
$mail->addAddress('email3@email.com', 'John Doe3');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
//Rest of the page's HTML code here.
答案 0 :(得分:0)
(这不是答案)
因为你没有得到任何东西然后使用例外
$mail = new PHPMailer(true);//set here true for exception handling
try {
//your entire Code STARTS
$mail->isSMTP();
$mail->Host = '[my email host here]';
$mail->From = 'source@email.com';
$mail->FromName = 'Contact Form';
$mail->addReplyTo('actualsource@email.com', 'Contact form');
$mail->WordWrap = 250; // Set word wrap to 250
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Website Inquiry';
$fullname = $_POST['FullName'];
$phone = $_POST['phone'];
$mail = $_POST['mail'];
$message = $_POST['mess'];
$mail->Body = 'Info Request<br><br><b>Name:</b> '.$fullname.'<br><b>Phone Number:</b>'.$phone.'<br><b>Email Address:</b>'.$mail.'<br><b>Message:</b> '.$message;
//Everything after this point seems to never happen
$mail->addAddress('email1@email.com', 'John Doe1');
$mail->addAddress('email2@email.com', 'John Doe2');
$mail->addAddress('email3@email.com', 'John Doe3');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
//your CODE ENDS
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
答案 1 :(得分:0)
您正在为邮寄数据重新分配邮件。您已经为PHPMailer分配了$ mail,它现在是一个对象。测试时出现此错误:
捕获致命错误:PHPMailer类的对象无法在第11行转换为字符串
将$ mail = $ _POST ['mail']更改为其他内容。