PHPMailer + smtp.gmail.com : Could not connect to SMTP host - phpmailer

I was trying to use PHPMailer library to send email.
I installed PHPMailer using composer And i just use their tutorial example.
Before that I also set the gmail account settings to allow less secure apps.
Here is my code:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'phpmailer/vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com:587'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'hidden#gmail.com'; // SMTP username
$mail->Password = 'hidden'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('hidden#gmail.com', 'Mr. From');
$mail->addAddress('to.#gmail.com', 'Mr. To'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
And Here is the Error:
2018-03-30 05:44:49 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 5sm16638786pfh.133 - gsmtp
2018-03-30 05:44:49 CLIENT -> SERVER: EHLO localhost
2018-03-30 05:44:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [123.244.104.122]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-03-30 05:44:50 CLIENT -> SERVER: STARTTLS
2018-03-30 05:44:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-03-30 05:44:50 CLIENT -> SERVER: QUIT
2018-03-30 05:44:50
2018-03-30 05:44:50
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
Please help me.

Just Add the following codes before line code $mail->SMTPDebug saves the day
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

Related

Mail sending using PHP Mailer in Laravel 7

I am using PHPMailer in Laravel 7. Mail works fine. Send Email successful. But after click 'send' button it shows some line about server after this, showing the success message.
This lines are show -
2021-01-04 19:27:49 SERVER -> CLIENT: xxxx
2021-01-04 19:27:49 CLIENT -> SERVER:
2021-01-04 19:27:49 SERVER -> CLIENT:
2021-01-04 19:27:49 SERVER -> CLIENT: xxxxx
2021-01-04 19:27:49 CLIENT -> SERVER: xxxx
2021-01-04 19:27:49 SERVER -> CLIENT: xxxx
My Controller Code :
public function jobApply(Request $request)
{
$resume = Resume::where('candidate_id', Auth::guard('candidate')->user()->id)->first();
$compEmail = $request->txt_compEmail;
$cadEmail = $request->txt_candEmail;
$subject = $request->txt_subject;
$body = $request->txt_candSalary;
$file = $resume->resume;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
// Send using SMTP
$mail->Host = env('MAIL_HOST');
// Set the SMTP server to send through
$mail->SMTPAuth = true;
// Enable SMTP authentication
$mail->Username = env('MAIL_USERNAME');
// SMTP username
$mail->Password = env('MAIL_PASSWORD');
// SMTP password
$mail->SMTPSecure = env('MAIL_ENCRYPTION');
// Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = env('MAIL_PORT');
Log::info("Check:".$mail->Host. ' '.$mail->Username.' '.$mail->Password.' '.$mail->SMTPSecure.' '.$mail->Port);// TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom($cadEmail);
$mail->addAddress($compEmail);
// Add a recipient
$mail->AltBody ='';
$mail->addAttachment($file );
// Content
$mail->isHTML(true);
// Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'Expected Salary : ' . $body ;
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// dd($e);
}
if ($mail){
return redirect()->back()->with('success','Mail Send Successfully..!!');
}
else{
return redirect()->back()->with('error','Something Went Wrong')->withInput();
}
}
How Can I hide this ? Anyone suggest me any solution. Advanced thanks.
You've asked it to show debug output, so it is producing it. If you don't want that, don't turn it on – comment out this line:
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

EOF caught while checking if connected

I would like to send an email from localhost by using PHP Mailer.
i have already look for others question and that solve some of the error that i got.
Except for this last error.i did not find the answer that fit to solve the error. I am really hope for help to solve this error.
This is what being diplayed after try to run the code.
2018-08-03 04:57:44 SERVER -> CLIENT:
2018-08-03 04:57:44 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
Email could not be sent. Mailer Error: SMTP Error: Could not authenticate.
This is my code
<html>
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('test#gmail.com', 'Mailer');
$mail->addAddress('test#gmail.com', 'Naura'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email testing';
$mail->Body = 'Hi. This is a test email.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Email has been sent';
} catch (Exception $e) {
echo 'Email could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
</html>

PHPMailer can't send via postfix

I already setup postfix in my debian linux VPS.
I can send email via ssh console using postfix.
I can send email using PHPMailer using my gmail account.
I can receive email in my debian linux VPS account.
Then I want to send email using PHPMailer via Postfix using my debian linux vps account. But it's failed with log below.
SERVER -> CLIENT: <br>
CLIENT -> SERVER: EHLO android<br>
SERVER -> CLIENT: <br>
SMTP ERROR: EHLO command failed: <br>
SMTP NOTICE: EOF caught while checking if connected<br>
SMTP Error: Could not connect to SMTP host.<br>
How to send email using PHPMailer using postfix ? should i use sasl ?
My sendmail.php
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Subject = 'hello postfix phpmailer';
$mail->msgHTML(file_get_contents('contentemail.html'), dirname(__FILE__));
$mail->Host = 'yyyy.zzzz.com';
$mail->Username = "xxxx";
$mail->setFrom('xxxx#yyyy.zzzz.com', 'Ceramah Islam');
$mail->addReplyTo('xxxx#yyyy.zzzz.com', 'Ceramah Islam');
$mail->Password = "aaaaaa";
$mail->addAddress('bbbb#gmail.com', 'bbbb');
$mail->send();
I think the problem is caused by the "snakeoil" certificate and private key that come with Postfix. The trick is not to verify them. Here is what I found worked:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "localhost";
$mail->Port = 25;
$mail->SMTPSecure = "tls";
$mail->SMTPOptions = array
(
'ssl' => array
(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('server#example.org', 'My Server');
$mail->addAddress('user#example.com', 'My User');
$mail->Subject = 'Message from PHPMailer and Postfix';
$mail->Body = 'Whatever';
if ($mail->send())
// SMTP message send success
{
// Put success logic here
}
else
// SMTP message send failure
{
// Put failure logic here
}

phpmailer No Relay Access Allowed Error

My website is hosted on godaddy, and I just figured out that my website is no more sending emails through phpmailer since last few months. I uploaded the latest version of phpmailer but still no success. The online web mail of my website runs fine. If I use php's "mail" function, it does send emails to gmail, but not to yahoo accounts.
I tried all the three ports 25, 465, and 587, but no luck
I am getting the following error from phpmailer:
SERVER -> CLIENT: 554 p3plsmtpa07-10.prod.phx3.secureserver.net ESMTP No Relay Access Allowed From 50.63.196.51
CLIENT -> SERVER: EHLO lostandfound.pakproject.com
SERVER -> CLIENT:
SMTP ERROR: EHLO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Following is my code that I am trying to test. (User name, passwords, emails are changed...)
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtpout.... my_server";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "here_i_used_my_website_email";
$mail->Password = "here_password";
$mail->setFrom('website_email', 'From name');
$mail->addReplyTo('website_email', 'From name');
$mail->addAddress('another_email', 'name_used_here');
$mail->Subject = 'About the task';
$mail->Body = 'This is email body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
It worked for me.
Keep in mind
https://co.godaddy.com/help/mail-server-addresses-and-ports-for-business-email-24071**
I was able to resolve my issue with the following code/settings of phpmailer
<?php
$recipient = "abc#def.com"
$subject = "Subject here";
$emailBody = "This is body";
// PHP MAILER CODE STARTS FROM HERE //
require '../phpmailermaster/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpout.secureserver.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#yyyy.com'; // SMTP username
$mail->Password = '3344123'; // SMTP password
//$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 465;
$mail->Port = 80; // TCP port to connect to [THIS PORT ALLOWING EMAILS]
$mail->setFrom('xxx#yyyy.com', 'hello');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress($recipient); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $emailBody;
$mail->AltBody = $emailBody;
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent';
}
// PHP MAILER CODE ENDS HERE ==
?>

how to send mail using phpmailer with smtp.office365.com

I am using below code to send mail but this is not working for me. Please help me to resolve this problem.
require 'class.phpmailer.php';
require 'class.smtp.php';
$subject="Test PHP mailer";
$message="Hi";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier - TLS/SSL
$mail->Host = "smtp.office365.com"; // sets GMAIL as the SMTP server - smtp.gmail.com
$mail->Port = 587; // set the SMTP port for the GMAIL server - Gmail SMTP port (SSL): 465 **** Gmail SMTP port (TLS): 587
$mail->Username = ""; // other email address
$mail->Password = ""; // password
$mail->SetFrom("customercare#test.com", "John");
$mail->addReplyTo('customercare#test.com', 'John');
$mail->Subject = $subject;
//$mail->IsHTML(true);
$mail->MsgHTML($message);
$mail->AddAddress('abc#example.com');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
It display error message:
Mailer Error: SMTP Error: data not accepted.SMTP server error: DATA END command failed Detail: 5.7.60 SMTP; Client does not have permissions to send as this sender SMTP code: 550
Looking at the error message, it's not a problem with the code, but a problem with the permissions for the user your are trying to send as.
You will either need to get the user you're trying to send as to allow their account to be used in such a way, or you'll have to find a different smtp service to use.

Resources