我正在尝试使用gmail smtp服务器通过php邮件从php表单发送电子邮件。 我已经从Windows防火墙打开了465端口,但是当我键入时,该端口没有出现在cmd输出中
Netstat -an
命令。PHP 页中显示的错误是:地址无效: SMTP 错误: 无法连接到 SMTP 主机。在我的搜索结果中,我的问题没有确切的解决方案。我的代码是:
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
define('GUSER', 'from@gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}}
if(smtpmailer('from@gmail.com', 'to@gmail.com', 'name', 'test mail message', 'Hello World!')){
echo "sent";
if (!empty($error)) echo $error;}
?>
注意:我正在为我的网站使用本地主机
尝试使用端口 25 或 587,同时检查您的 apache 日志。