PHPMailer 联系表单在服务器上不起作用,但在本地主机上可以吗?
PHPMailer Contact form doesn't work on server, but does on localhost?
我有一个 PHPMailer 联系表,可以将数据发送到 GMail 帐户。当网站在本地主机上运行时,它成功地做到了这一点,但是当我将网页上传到服务器并尝试使用联系表时,我收到以下错误:消息不能 sent.Mailer 错误:SMTP 连接( ) 失败。
我正在通过 Plesk 将文件上传到服务器,但我怀疑这是否相关。这是代码:
<form action="currentFile.php" method="POST">
Full Name:<input required type="text" name="fullname"/>
<input type="submit"/>
</form>
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'mail@gmail.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->addAddress('mail@gmail.com');
$mail->Subject = 'Contact Form';
$mail->Body = "Test";
$mail->AltBody = "Test";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
我认为将 $mail->isSMTP();
更改为 $mail->isMAIL();
没有任何区别。当网站在线(在我的服务器上)时,我该怎么做才能将表单数据发送到电子邮件?
注意:使用的gmail帐号和密码在下面的代码中是"fake"。我打算以 GMail 帐户为目标,并且我已成功输入 Google 邮件地址和密码,因此脚本 运行 在本地主机上成功。
谢谢。
"@Fred-ii- Thanks this helped resolve my issue. Please post as an answer so I can give credit. – H3ll0 10 mins ago"
操作 action="currentFile.php"
属于 <form>
,未输入并使用 GET/POST 方法。
您还需要使用任一方法和assign/use输入数组。
即:
$var = $_POST['var']; // or $_GET depending on the preferred method.
header 中还应该有一个 From:
,因为这可能会将您的邮件发送到垃圾邮件或完全被拒绝。
即:
$mail->setFrom('from@example.com', 'First Last');
故障排除参考链接:
- https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
- http://php.net/manual/en/function.error-reporting.php
这是 PHPMailer 的 github:
如果您没有 SPF 记录(发件人策略框架),创建一个会很有用。
我有一个 PHPMailer 联系表,可以将数据发送到 GMail 帐户。当网站在本地主机上运行时,它成功地做到了这一点,但是当我将网页上传到服务器并尝试使用联系表时,我收到以下错误:消息不能 sent.Mailer 错误:SMTP 连接( ) 失败。
我正在通过 Plesk 将文件上传到服务器,但我怀疑这是否相关。这是代码:
<form action="currentFile.php" method="POST">
Full Name:<input required type="text" name="fullname"/>
<input type="submit"/>
</form>
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'mail@gmail.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->addAddress('mail@gmail.com');
$mail->Subject = 'Contact Form';
$mail->Body = "Test";
$mail->AltBody = "Test";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
我认为将 $mail->isSMTP();
更改为 $mail->isMAIL();
没有任何区别。当网站在线(在我的服务器上)时,我该怎么做才能将表单数据发送到电子邮件?
注意:使用的gmail帐号和密码在下面的代码中是"fake"。我打算以 GMail 帐户为目标,并且我已成功输入 Google 邮件地址和密码,因此脚本 运行 在本地主机上成功。
谢谢。
"@Fred-ii- Thanks this helped resolve my issue. Please post as an answer so I can give credit. – H3ll0 10 mins ago"
操作 action="currentFile.php"
属于 <form>
,未输入并使用 GET/POST 方法。
您还需要使用任一方法和assign/use输入数组。
即:
$var = $_POST['var']; // or $_GET depending on the preferred method.
header 中还应该有一个 From:
,因为这可能会将您的邮件发送到垃圾邮件或完全被拒绝。
即:
$mail->setFrom('from@example.com', 'First Last');
故障排除参考链接:
- https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
- http://php.net/manual/en/function.error-reporting.php
这是 PHPMailer 的 github:
如果您没有 SPF 记录(发件人策略框架),创建一个会很有用。