Send email using the Gmail SMTP server via PHP Mailer

I would like to send an email using the 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 get the 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

+55
php phpmailer smtp gmail
Apr 16 '13 at 22:34
source share
11 answers
 $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.

You may need $mail->SMTPSecure = 'ssl';

Also, make sure you do not have 2-step authentication for this account, as this can also cause problems.

UPDATED

You can try changing $ mail-> SMTP to:

 $mail->SMTPSecure = 'tls'; 

It is worth noting that some SMTP servers block connections. Some SMTP servers do not support SSL (or TLS ) connections.

+98
Apr 16 '13 at 22:45
source share

So, I just resolved my "SMTP Connection Error" error, and I wanted to post a solution just in case it helps someone else.

I used the EXACT code specified in the gmail.phps PHPMailer example file. It worked just when I used MAMP, and then I got an SMTP connection error as soon as I moved it to my personal server.

All the stack overflow answers I read and all the PHPMailer troubleshooting documentation says this is not a problem with PHPMailer. This is a server side configuration issue. I tried different ports (587, 465, 25), I tried "SSL" and "TLS" encryption. I checked that openssl is included in my php.ini file. I checked that the firewall problem was not there. Everything is checked, and still nothing.

The solution was that I had to delete this line:

 $mail->isSMTP(); 

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

+21
Jul 02 '15 at 21:12
source share

It's also worth noting that if you have enabled two-factor authentication, you need to set up a password for a specific application in order to use your email account instead of the password.

You can create a password for a specific application by following these instructions: https://support.google.com/accounts/answer/185833

Then set $mail->Password to your special password.

+7
Jan 22 '14 at 12:05
source share

It seems your server was unable to establish a connection to the Gmail SMTP server. Here are some troubleshooting tips: 1) check that SSL is configured correctly in your PHP (the module that processes it is not installed by default in PHP. You need to check your configuration in phph.ini). 2) check if your firewall allows you to send outgoing calls to the desired port (here 465 or 587). Use telnet for this. If the port does not open, you will need some sysdmin support to configure it. I hope you quickly figured out!

+5
Jul 26 '13 at 8:26
source share

I can not comment, but yes, delete

 $mail->isSMTP(); 

and all will be well!

+5
Sep 25 '16 at 9:53 on
source share

Open the Link and choose to follow the instructions on which Google servers block any attempts from unknown servers, so as soon as you click on the "captcha check" button, everything will be fine

+3
Oct 07 '13 at 9:56
source share

Google treats Gmail accounts differently depending on the user information available, possibly to limit spammers.

I could not use SMTP until I checked the phone. I made another account for double verification, and I was able to confirm it.

+1
Apr 19 '16 at 15:29
source share

I think you can get the connection problem 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; } 
0
Mar 21 '14 at 6:06
source share

this code works 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'; } 
0
Oct 27 '16 at 5:41
source share

Anderscc has it all right. Thank you This worked for me, but not 100%.

I had to install

$ mail-> SMTPDebug = 0; Setting it to 1 can cause errors, especially if you pass some data as json to the next page. Example. Perform verification if mail is sent using json to transfer data via ajax.

I had to lower the security settings of my gmail account in order to get rid of the errors: "SMTP connect () error" and "SMTP error: password failed"

Solution: This problem can be caused either by "less secure" applications trying to use an email account (this is consistent with Google’s prompt, but not sure how they judge what is safe and what is not). Or, if you try to log in several times to the OR line, if you change countries (for example, use a VPN, move the code to another server or try to log in from different parts of the world).

Links that fix the problem (you must be logged into your google account):

Note: You can go to the following stackoverflow answer link for more detailed help.

stack overflow

0
May 4 '17 at
source share

If you are using cPanel, you just need to click the wee field, which allows sending to external SMTP servers.

Go to CPanel> Settings> All> "Limit outgoing SMTP to root, Exim and mailman (FKA SMTP Tweak)"

As indicated here:

"Password not accepted from server: 535 Invalid authentication data" when sending using GMail and phpMailer

0
Oct 26 '17 at 15:32
source share



All Articles