I can't send Email from my gmail account by php
Here is code. I am trying to send email from gmail using smtp.gmail.com, but it's giving error
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com"; //gmail SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'mymail#gmail.com'; //username
$mail->Password = 'mypassword'; //password
$mail->SMTPSecure = 'ssl';
$mail->SMTPDebug = 2;
$mail->Port = 465; //smtp port
$mail->setFrom('mymail#gmail.com','Artisans Web');
$mail->addAddress('mymail#gmail.com', 'User Name');
// $mail->addAttachment(__DIR__ . '/attachment1.png');
// $mail->addAttachment(__DIR__ . '/attachment2.jpg');
$mail->IsHTML(true);
$mail->Subject = 'Email Subject';
$mail->Body = 'Email Body';
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
This is the error:
2018-02-13 06:12:41 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Have you enabled less secure app access on your Gmail account? https://support.google.com/accounts/answer/6010255?hl=en
Related
i'm trying to send an email using PHPMailer. I've seen some topics about sending an email by Gmail servers using this package but i couldn't succeed.
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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 = 587; // or 587
$mail->Username = "myEmailAddress#gmail.com";
$mail->Password = "MyGmailPassword";
$mail->setFrom('myEmailAddress#gmail.com', 'First Last');
$mail->addAddress('MyTargetEmail#example.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'Text to be sent';
if(!$mail->send()){
echo 'message was not sent: ' . $mail->ErrorInfo;
}
else{
echo 'Successfully sent';
}
my response :
2018-11-29 14:56:37 SMTP ERROR: Failed to connect to server: (0)
<br>
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
<br>
message was not sent: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
You are sending the email to the wrong port/configuration. In your code you have it sent to Port 587 which handles stmp connections but you are sending it configured over ssl (which would need to be sent over port 465).
You ONLY need to change where it says ssl to "tls" and it will work.
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; // or 587
Try this with composer
require phpmailer/phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;enter code here
$mail->Username = '#gmail.com';
$mail->Password = '';
$mail->setFrom('#gmail.com', '');
$mail->addReplyTo('#gmail.com', '');
$mail->addAddress('#gmail.com', ' Name');
$mail->Subject = 'This for Testing PHPMailer';
//$mail->msgHTML(file_get_contents('message.html'), __DIR__);
$mail->Body = 'This is a plain text message body';
//$mail->addAttachment('test.txt');
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'The email message was sent.';
}
?>
I'm facing this issue from last three days, before this script worked perfectly. Now getting error:
SMTP ERROR: Failed to connect to server: (0) 2017-10-06 21:05:34 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
ahsanazhar12#gmail.com
Here is my script:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->setFrom('example#gmail.com', 'Your Name');
$mail->addAddress('example#gmail.com', 'My Friend');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if (!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
I think Gmail may have changed settings for sending emails using SMTP, or something like that.
Finally i'm able to send emails from localhost. Here is my code.
To install:
Download PHPMailer
Add it to your project (i put it on root)
Add Autoload class to your Script.
Rest of code is below
require "PHPMailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'securepass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('example#gmail.com', "Mailer");
$mail->addAddress("example#gmail.com","receiver Name");
$mail->isHTML(true);
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if( $mail->send()){
return array("msg"=>msg("success","Email has been sent.<br>"));
} else {
return array("msg"=>msg("error","Email can't send.Try Again<br>"));
}
I want to send mail as outlook mail... but it shows the connection error and authentication error if some port numbers are changed... what wrong in my code....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "outlook.office365.com";
$mail->Port = 993;
$mail->Username = "harish.reddy#skoopview.com";
$mail->Password = "XXXXXXX";
$mail->From = $from;
$mail->FromName= $FromName;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->addAddress('harish.reddy#skoopview.com','harish');
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
It shows error like this ... What i do ??
SERVER -> CLIENT: * OK The Microsoft Exchange IMAP4 service is ready.
[SABLAE4AUABSADAANgBDAEEAMAAwADUAMwAuAGEAcABjAHAAcgBkADAANgAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A]
* BYE Connection is closed. 13 2016-09-12 10:50:13 SMTP NOTICE: EOF caught while checking if connected 2016-09-12 10:50:13 SMTP Error:
Could not authenticate. 2016-09-12 10:50:13 SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer
Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Thankyou....,
As George stated, the port should be 587. Make sure you are using TLS:
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp-mail.outlook.com;smtp.office365.com'; // Specify main and backup SMTP servers
But Outlook self-signs its own SSL/TLS certificate. Hence you need to add this piece of code as per https://github.com/PHPMailer/PHPMailer/issues/914:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
I would try port 587 for SMTP - more details here: https://support.office.com/en-gb/article/POP-and-IMAP-settings-for-Outlook-Office-365-for-business-7fc677eb-2491-4cbc-8153-8e7113525f6c
I'm having trouble to send mail using phpmailer. Error appear is,
2016-02-16 13:20:14 Connection: opening to 103.8.25.18:465,
timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false,
'verify_peer_name' => false, 'allow_self_signed' => true, ), )
2016-02-16 13:20:35 SMTP ERROR: Failed to connect to server: A
connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed
because connected host has failed to respond. (10060) 2016-02-16
13:20:35 SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message
could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
If I test using direct from my localhost PC, it can send the mail but when I transfer the system to our office server, it won't send the mail. Below are the phpmailer codes;
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mydomain.com'; // Specify main and backup SMTP servers
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->AddAddress('kamran#ktmparking.com.my');
$mail->addReplyTo('info#ktmparking.com.my', 'HR-KTMBCP');
$mail->setFrom('info#ktmparking.com.my', 'HR-KTMBCP');
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Leave Application';
$mail->Body = 'example of message ';
$mail->AltBody = 'example of message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent';
}
i am using xampp version 1.8.1 and i have also removed semicolon from php.ini file in extension=php_openssl.dll but still I am getting this error stream_socket_enable_crypto(): this stream does not support SSL/crypto and Message could not be sent.Mailer Error: SMTP connect() failed.
My php mailer code is:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'urusername#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->From = 'urusername#gmail.com';
$mail->FromName = 'Example';
$mail->addAddress('receivermail#gmail.com', 'xxxxx');
$mail->addReplyTo('receivermail#gmail.com', 'xxxxxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Using PHPMailer';
$mail->Body = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
kindly help me
Set the port to:
$mail->Port = 587;
Google SMTP uses port 587.