ItGo.me Focus on IT Recommend

Home > send email using Gmail SMTP server through PHP Mailer

send email using Gmail SMTP server through PHP Mailer

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

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

php smtp gmail phpmailer
edited May 9 '14 at 9:12 asked Apr 16 '13 at 22:34 Mohammad Masoudian 2,132 4 18 36      You may want to have a look at Jorj's answer on this thread: stackoverflow.com/questions/5759419/… –  Human Wannabe May 17 '13 at 9:22

 | 

9 Answers
9

---Accepted---Accepted---Accepted---
$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.


edited Nov 23 '15 at 17:09 answered Apr 16 '13 at 22:45 Anderscc 1,230 1 13 20      i recieve this error when i use your code Mailer Error: The following From address failed: phoenixd110@gmail.com : Called Mail() without being connected –  Mohammad Masoudian Apr 16 '13 at 22:55 1   Updated answer with a further suggestion. The code is definitely fine anyway. You could check these two sources phpmailer.worxware.com/index.php?pg=exampleagmail and github.com/Synchro/PHPMailer/blob/master/examples/… –  Anderscc Apr 16 '13 at 23:03      i am getting the error as: CLIENT -> SMTP: EHLO localhost SMTP -> ERROR: EHLO not ---Accepted---Accepted---Accepted--- from server: CLIENT -> SMTP: HELO localhost Notice: fwrite(): send of 16 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in C:\xampp\htdocs\program\mailsending1\mailsending_v1\PHPMaile‌​r-master\class.smtp.‌​php on line 1023 SMTP -> ERROR: HELO not ---Accepted---Accepted---Accepted--- from server: SMTP -> NOTICE: EOF caught while checking if connectedSMTP Connect() failed. Mailer Error: SMTP Connect() failed. –  Anurag Singh Aug 12 '13 at 7:09 1   Note that if you specify "ssl" (which is warmly recommended), your PHP installation needs to have the "openssl" extension loaded (or compiled in), using your favourite PHP extension mechanism (I happen to use OS X Macports, for which it's sudo port install php5-openssl) –  xgretsch Mar 17 '14 at 15:44      It better to check both returning value and errors if(!$mail->Send() || $mail->ErrorInfo) { –  ymakux Jan 21 '16 at 15:25

 | 

So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.

I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.

All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.

The solution was that I had to remove this line:

$mail->isSMTP();

Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.

Recommend:smtp gmail server php mailer not working

elp me . So hereby I am posting my code: <phprequire("class.phpmailer.php");require("class.smtp.php");$mail = new PHPMailer();$mail->IsSMTP(); $mail->Host="smtp.gmail.com";$mail->SMTPDebug=1; $mail->SMTPAuth=true;$mail->SMTPSecure=


answered Jul 2 '15 at 21:12 Evan Butler 315 2 9 2   This solution works because it turns off SMTP altogether. I DO NOT recommend this method. I never solved this issue, I'm now switching to using Mandrill API instead. –  Evan Butler Jul 23 '15 at 22:15      Just use Mandrill, it's much better and actually has some analytics. –  Evan Butler Oct 10 '15 at 6:50      Why does this work? –  ykay Aug 12 '16 at 10:48      Solved my problem. Thank you so much. –  Cristian Oana Nov 29 '16 at 13:26      Man I love you!!! –  Djomla Jan 7 at 12:47

 | 

Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.

You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

Then set $mail->Password to your application specific password.


answered Jan 22 '14 at 12:05 Tim Carr 509 5 4

 | 

It seems that your server fails to establish a connection to Gmail SMTP server. Here are some hints to troubleshoot this: 1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini). 2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config. I hope you'll sort this out quickly!


answered Jul 26 '13 at 8:26 Pr Shadoko 870 2 6 15

 | 

Open this Link and select follow the instructions google servers blocks any attempts from unknown servers so once you click on captcha check every thing will be fine


answered Oct 7 '13 at 9:56 Yasin Hassanien 2,425 1 11 13      I used the same code as in the ---Accepted---Accepted---Accepted--- answer but still would get SMTP_connect() failures. I tried everything in that link and it ended up working, so +1. –  Johnny Dec 30 '13 at 16:22      I was also having issues with SMTP_connect() failures. I followed the above steps and was still getting the error. I followed the steps twice, on 2 different gmail addresses, and both times it seems that I needed to wait ~1 hour before the changes seemed to kick in. Afterwards everything worked as expected. –  cwal Jan 20 '15 at 21:20

 | 

Can't comment but yes, remove

$mail->isSMTP();

and you will be fine!


answered Sep 25 '16 at 9:53 Simon Ilett 46 7

 | 

Google treat Gmail accounts differently depending on the available user information, probably to curb spammers.

I couldn't use SMTP until I did the phone verification. Made another account to double check and I was able to confirm it.


answered Apr 19 '16 at 15:29 Forgot My Login 12 11 1

 | 

I think it is connection issue you can get code here http://skillrow.com/sending-mail-using-smtp-and-php/

include(“smtpfile.php“);
include(“saslfile.php“); // for SASL authentication $from=”my@website.com“; //from mail id

$smtp=new smtp_class;

$smtp->host_name=”www.abc.com“; // name of host
$smtp->host_port=25;//port of host

$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host=””;
$smtp->ssl=0;
$smtp->start_tls=0;
$smtp->localhost=”localhost“;
$smtp->direct_delivery=0;

$smtp->user=”smtp username”;
$smtp->realm=””;
$smtp->password=”smtp password“;

$smtp->workstation=””;
$smtp->authentication_mechanism=””;

$mail=$smtp->SendMessage($from,array($to),array(“From:$from”,”To: $to”,”Subject: $subject”,”Date: ”.strftime(“%a, %d %b %Y %H:%M:%S %Z”)),”$message”);

if($mail){
   echo “Mail sent“;
}else{
   echo $smtp->error;
}

edited Aug 28 '14 at 10:50 Praxis Ashelin 4,132 12 42 answered Mar 21 '14 at 6:06 user3445130 9      Link-only answers are typically discouraged. Could you expand this into something stand-alone? –  Werner Mar 21 '14 at 6:23

 | 

this code working fine for me

    $mail = new PHPMailer;
    //Enable SMTP debugging. 
    $mail->SMTPDebug = 0;
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();
    //Set SMTP host name                          
    $mail->Host = $hostname;
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth = true;
    //Provide username and password     
    $mail->Username = $sender;
    $mail->Password = $mail_password;
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure = "ssl";
    //Set TCP port to connect to 
    $mail->Port = 465;
    $mail->From = $sender;  
    $mail->FromName = $sender_name;
    $mail->addAddress($to);
    $mail->isHTML(true);
    $mail->Subject = $Subject;
    $mail->Body = $Body;
    $mail->AltBody = "This is the plain text version of the email content";
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else {
           echo 'Mail Sent Successfully';
    }

edited Oct 27 '16 at 5:47 answered Oct 27 '16 at 5:41 Zohaib Hussain 1 1

 | 

Recommend:Send email using the GMail SMTP server from a PHP page

error: authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: mx.google.com at your service, [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)] Can anyone help Here is m

oriUlr:http://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer/

------splitte line----------------------------