通过PHP Mailer使用Gmail SMTP服务器发送电子邮件 [英] send email using Gmail SMTP server through PHP Mailer

查看:175
本文介绍了通过PHP Mailer使用Gmail SMTP服务器发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 PHP Mailer 使用 Gmail SMTP 服务器发送电子邮件。

这是我的代码

 <?php 
require_once('class.phpmailer.php');

$ mail = new PHPMailer();
$ mail-> IsSMTP();
$ mail-> CharSet =UTF-8;
$ mail-> SMTPSecure ='tls';
$ mail-> Host ='smtp.gmail.com';
$ mail-> Port = 587;
$ mail->使用者名称='MyUsername@gmail.com';
$ mail->密码='有效密码';
$ mail-> SMTPAuth = true;

$ mail-> From ='MyUsername@gmail.com';
$ mail-> FromName ='Mohammad Masoudian';
$ mail-> AddAddress('anotherValidGmail@gmail.com');
$ mail-> AddReplyTo('phoenixd110@gmail.com','信息');

$ mail-> IsHTML(true);
$ mail-> Subject =PHPMailer通过Sendmail测试主题,基本;
$ mail-> AltBody =要查看邮件,请使用兼容HTML的电子邮件查看器!;
$ mail-> Body =Hello; $!
$ b如果(!$ mail-> Send())
{
echo梅勒错误:。 $ MAIL-> ERRORINFO;
}
else
{
echoMessage sent!;
}
?>

但我收到以下错误

 邮件程序错误:SMTP错误:以下收件人失败:anotherValidGmail@gmail.com 

SMTP服务器错误:SMTP端口587上的邮件提交需要SMTP AUTH

我的域名是 vatandesign.ir

解决方案

  $ mail = new PHPMailer(); //创建一个新对象
$ mail-> IsSMTP(); //启用SMTP
$ mail-> SMTPDebug = 1; //调试:1 =错误和消息,2 =仅消息
$ mail-> SMTPAuth = true; //启用认证
$ mail-> SMTPSecure ='ssl'; //启用安全转接功能需要Gmail
$ mail-> Host =smtp.gmail.com;
$ mail->端口= 465; //或587
$ mail-> IsHTML(true);
$ mail->使用者名称=email@gmail.com;
$ mail->密码=密码;
$ mail-> SetFrom(example@gmail.com);
$ mail-> Subject =Test;
$ mail-> Body =hello;
$ mail-> AddAddress(email@gmail.com);

if(!$ mail-> Send()){
echo邮件错误:。 $ MAIL-> ERRORINFO;
} else {
echoMessage has been sent;
}

上面的这段代码已经过测试和为我工作。



可能需要 $ mail-> SMTPSecure ='ssl';



此外,请确保您没有为该帐户启用两步验证功能,因为这也可能会导致问题。



已更新



您可以尝试将$ mail-> SMTP更改为:

  $ mail-> SMTPSecure ='tls'; 

值得注意的是一些SMTP服务器阻止连接。
某些SMTP服务器不支持 SSL (或 TLS )连接。


I would like to send an email using Gmail SMTP server through PHP Mailer.

this is my code

<?php
require_once('class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername@gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;

$mail->From = 'MyUsername@gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail@gmail.com');
$mail->AddReplyTo('phoenixd110@gmail.com', 'Information');

$mail->IsHTML(true);
$mail->Subject    = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$mail->Body    = "Hello";

if(!$mail->Send())
{
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
  echo "Message sent!";
}
?>

but i recieve this following error

Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail@gmail.com

SMTP server error: SMTP AUTH is required for message submission on port 587

my domain is vatandesign.ir

解决方案

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // 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; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }

This code above has been tested and worked for me.

It could be that you needed $mail->SMTPSecure = 'ssl';

Also make sure you don't have two step verification switched on for that account as that can cause problems also.

UPDATED

You could try changing $mail->SMTP to:

$mail->SMTPSecure = 'tls';

It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

这篇关于通过PHP Mailer使用Gmail SMTP服务器发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆