我正在尝试使用PHPMailer在我的网站上创建联系表单。我在设置时遇到了一些麻烦。我正在尝试使用G-mail作为我的smtp主机。我想知道是否有人可以帮助解决这个问题?
这是我的邮件代码:
<?php
require("class.phpmailer.php");
require("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 467;
$mail->Username = "validmail@gmail.com"; // SMTP username
$mail->Password = "workingpassword"; // SMTP password
$mail->From = "validmail@gmail.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com"); // name is optional
$mail->AddReplyTo("info@example.com", "Information");
$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";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
错误消息:
Message could not be sent.
Mailer Error: The following From address failed: validmail@gmail.com
答案 0 :(得分:7)
你看过并尝试过这个Q的信息吗?
PHPMailer: SMTP Error: Could not connect to SMTP host
特别是,这是否提供任何其他信息?
$mail->SMTPDebug = 1;
答案 1 :(得分:2)
smtp.gmail.com要求您使用SSL和端口587或465。
请参阅其配置页:http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
答案 2 :(得分:1)
你在Windows上运行PHP吗?那么这可能会有所帮助:
http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html
答案 3 :(得分:1)
得到了同样的错误,问题是我试图从“boy333 @ in **”帐户发送电子邮件假装为“girl333 @ in **”。我刚刚改变了
$mail->From = 'girl333@in**'
到用户名我实际连接。所以我改为:
$mail->From = 'boy333@in**'
这个想法是这两个字段具有相同的用户名。
$mail->Username = "boy333";
$mail->From = 'boy333@in**';
答案 4 :(得分:1)
<?php
require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$phoneField = $_POST['contactno'];
$cityField = $_POST['city'];
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$body .= $nameField;
try {
//$mail->Host = "mail.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username = "xxxxx@gmail.com"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddAddress('sendto@gmail.com', 'abc');
$mail->SetFrom('xxxxxx@gmail.com', 'def');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo "Message Sent OK</p>\n";
header("location: ../test.html");
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
转到Google设置并启用“不太安全”的设置。应用。它对我有用。
答案 5 :(得分:0)
ping smtp.gmail.com
出于某种原因,Google会将SMTP请求重定向到gmail-smtp-msa.l.google.com
PING gmail-smtp-msa.l.google.com (74.125.133.108): 56 data bytes
64 bytes from 74.125.133.108: icmp_seq=0 ttl=43 time=72.636 ms
64 bytes from 74.125.133.108: icmp_seq=1 ttl=43 time=68.841 ms
64 bytes from 74.125.133.108: icmp_seq=2 ttl=43 time=68.500 ms
因此,您应该将最终目标放在配置中,因为PHPMailer不遵循重定向。此外,您可以尝试通过将该字段留空来发送没有SMTPSecure的电子邮件。
$mail->Host = 'gmail-smtp-msa.l.google.com';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = '';
$mail->Port = 25;
答案 6 :(得分:0)
如果您在gsuite中发送电子邮件,请检查以下两个步骤: https://support.google.com/a/answer/6260879?hl=en https://accounts.google.com/DisplayUnlockCaptcha
Php邮件设置; PHPMailer版本5.2.22 检查class.phpmailer.php,
private String getPropertyNameValue(){
return propService.getProperty("prop.name");
}
邮件发送php
var $Host = 'smtp.gmail.com';
var $Port = 465;
var $Helo ='domain.net';
public $SMTPSecure = 'ssl';
public $SMTPAutoTLS = true;
public $SMTPAuth = true;
public $SMTPOptions = array();
public $Username = 'gmail-username';//or domain credentials on google mail
public $Password = 'gmail-password';
public $Timeout = 10;
public $SMTPDebug = true;
这对我有用。希望它有所帮助。