I'm using PHPMailer, which works fine on my server, but not on my localhost. The function I'm using to send is:
function sendEmail($mail, $toEmail, $toName, $subject, $message, $noHtmlMessage) {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'christopherpickardco.netfirms.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'webmaster#christopherpickard.com'; // SMTP username
$mail->Password = 'myPassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('webmaster#christopherpickard.com', 'ChrisComposes.com'); //
$mail->addAddress($toEmail, $toName); // Add a recipient. Right now this goes to me, in the end it will go to Chris
$mail->addReplyTo('webmaster#christopherpickard.com', 'ChrisComposes.com');
$mail->Subject = "ChrisComposes.com: " . $subject;
$mail->Body = $message;
$mail->AltBody = $noHtmlMessage;
$mail->send();
}
This works fine on my server but on my localhost I get an error: Warning: stream_socket_enable_crypto(): Peer certificate CN=smtp.eigbox.net' did not match expected CN=christopherpickardco.netfirms.com' in /Users/ChristopherPickard/Web_Development/chriscomposes/includes/phpmailer/class.smtp.php on line 344
How can I fix this?
From docs:
The correct fix for this is to replace the invalid, misconfigured or
self-signed certificate with a good one. Failing that, you can allow
insecure connections via the SMTPOptions property introduced in
PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP
class in earlier versions), though this is not recommended:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
This works.
Related
I'm trying to send emails using PHPMailer and it works just fine on dev environment (locally - my private leptop) but doesn't work at all on production environment(Azure Host).
This is the code I'm using:
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true); // 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 465-for gmail
$mail->CharSet = 'UTF-8';
$mail->IsHTML(false);
$mail->Username = '***#gmail.com';
$mail->Password = '*****';
$mail->SetFrom('****#gmail.com');
$mail->Subject = 'test phpmailer';
$mail->Body = 'body without html';
$mail->AddAddress('****#gmail.com');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
I was trying to check for solutions on their website as well as general online but I found nothing helpful: Troubleshooting PHPMailer Problems
What I tried and what I see:
On CMD:
ping smtp.gmail.com
Pinging gmail-smtp-msa.l.google.com [108.177.126.108] with 32 bytes of data:
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Ping statistics for 108.177.126.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 5ms, Maximum = 5ms, Average = 5ms
On CMD 2:
telnet smtp.gmail.com 587
220 smtp.gmail.com ESMTP b11sm6804305edc.8 - gsmtp
Those results don't point on problem so I don't know how to solve it and I'm pretty stuck now.
When I run the code on production this is what I get:
2018-02-25 11:51:48 SMTP ERROR: Failed to connect to server: (0) 2018-02-25 11:51:48 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Does someone knows how to solve this problem?
After a lot of work and searching all over the web, I recommend to those who use Azure to use third-party service such as SendGrid to send emails.
Work for me.
If secure domain to SSL
Add
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
Full code
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Username = "username#gmail.com";
$mail->Password = "password";
$mail->SetFrom('setFrom#gmail.com', 'xxx');
$mail->addReplyTo('replyTo#gmail.com', 'yyy');
$mail->addAddress('addAddress#gmail.com', 'addressName');
$mail->Subject = 'Subject Test';
$mail->msgHTML("Welcome to bamossza.");
$mail->AltBody = 'bamossza.com';
Update for 2021
I found that my host was blocking traffic on ports 465 and 587. Thereby blocking SMPT on SSL or TLS.
Luckily for me I'm using a host that allows me to enable this port. Not all hosts will allow it (Go Daddy). It did require that I contact support and request they open up the port for me. If the system ever does have a major update it might get disabled again though.
Link to further reading
I'm using phpmailer in a selfmade template/mail function:
<?php
require_once(__DIR__.'/../../vendor/phpmailer/phpmailer/PHPMailerAutoload.php');
function sendTemplateMail($body, $data, $subject, $receiver, $sender){
$template = Timber::compile($body, $data);
sendMail($template, $subject, $receiver, $sender);
}
function sendMail($template, $subject, $receiver, $sender){
$mail = new PHPMailer(true); // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP // TCP port to connect to
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "**";
$mail->Password = "**";
$mail->setFrom($sender);
$mail->addAddress($receiver); // Add a recipient
$mail->addReplyTo($sender);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $template;
$mail->AltBody = $template; //$mail->AltBody = $template; //
return $mail->send();
}
In my project the username and password are correct because they worked before. But now I get an error that the smtp is not connected.
fatal error: Uncaught phpmailerException: SMTP Error: Could not connect to SMTP host.
Try this, add the sentence to sendMail function:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended as it defeats much of the point of using a secure transport at all.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure
This should be a temporary solution.
Good Day,
I was having an error on my phpmailer smtp configuration to smtp.office365.com
here my script
require __DIR__ .'/vendor/phpmailer/phpmailer/src/Exception.php';
require __DIR__ .'/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require __DIR__ .'/vendor/phpmailer/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = gethostbyname('smtp.office365.com'); // Specify main and backup SMTP servers
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'office365 username'; // SMTP username
$mail->Password = 'office365 password'; // SMTP password
$mail->SMTPOptions = array (
'ssl' => array(
// 'verify_peer' => false,
// 'verify_peer_name' => false,
// 'allow_self_signed' => true
));
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->From = $mail->Username;
$mail->addAddress('recipient#gmail.com'); // Name is optional
//Attachments
//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.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
I got this error
stream_socket_enable_crypto(): Peer certificate CN=`servername.com' did not match expected CN=`smtp.office365.com'
if I uncomment
$mail->SMTPOptions = array (
'ssl' => array(
// 'verify_peer' => false,
'peer_name' => 'smtp.office365.com',
// 'verify_peer_name' => false,
// 'allow_self_signed' => true
));
I still get authentication failure..
Im not really good with server configuration as someone is doing the server set up. but my site is on https.
Think about what this means. You’re asking to connect to a named host, but the name on the certificate does not match. That means that either the server is misconfigured (unlikely for office365), or you are being redirected to a different server that’s using a different name. This is extremely likely, as it’s very common in large hosting providers. All will be revealed if you set SMTPDebug = 2, as the troubleshooting guide the error message links to suggests.
That this has happened is a good thing - it’s one of the main reasons for using TLS - it not only encrypts your traffic in transit, but provided assurance that the server you connected to is the one you expected, i.e. it is doing its job properly.
we are using SMTP advanced authentication to send a test mail using PHPMailer.
We are using 1and1.com server with SMTP and SSL for E-mail Exchange.
We need to run this php page from a third party server. we have taken a example from the downloaded PHPMailer package. we have tried with "test_pop_before_smtp_advanced" example and " SMTP advanced test with authentication" example. In both cases we are getting same Error.
SMTP Error: Could not connect to SMTP host.
Here is the php file we have written for sending Mail.
<html>
<head>
<title>PHPMailer - SMTP advanced test with authentication</title>
</head>
<body>
<?php
echo "hai";
include('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
echo "hai1";
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "smtp.1and1.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.1and1.com"; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxx#abc.com"; // SMTP account username
$mail->Password = "xxxxx"; // SMTP account password
$mail->AddReplyTo('mno#abc.com', 'First Last');
$mail->AddAddress('mno#abc.com', 'John Doe');
$mail->SetFrom('xxx#abc.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = 'hello';
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
} catch (phpmailerException $e) {
echo "error";
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo "err";
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
</body>
</html>
Can anyone help us in this regard, please suggest any other easy approach for the same purpose. Thank you...
I had the same problem, and was walked through a bunch of different port, domain, and security configuration attempts. Try to use these settings, I found them on another help site and they worked for me.
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = '74.208.5.2'; // Specify main and backup SMTP server (server name)
$mail->Port = 25; // TCP port to connect to 587 or 465, 425, 25
$mail->Username = 'xxxxx#xxx.com'; // SMTP username
$mail->Password = 'xxxxxx'; // SMTP password
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
I had to set SMTPSecutre to tls or remove this line completely, use smtp.1and1.net's ip for the host (you can verify with a ping or whois), and set port to 25. Even though 1and1 suggested other ports more often. Hopefully this saves someone some time.
PHP suddenly changed in version 5.6 to be much more strict about SSL connections. This causes many web contact forms to break, often without any error message being displayed. If you want to continue using a self-signed certificate, or allowing a non-SSL (insecure) connection, a quick workaround for PHPMailer is to use:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if you are using an SSL connection - try changing your port to default SSL port 465
If you are not using SSL, try removing these lines:
$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
and change the port to default port 25.
As you are getting a could not connect, take a moment to check your login details are correct, even the best of us can fall foul of a miss-typed password.
I have this code, and all works well in my local server. The email is sent without any problem.
But now I pass the content to webserver, and I get this error...
SMTP Error: Could not connect to SMTP host.
SSL is enable in the server..correct? so, what is the problem?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "dnteiro"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
It sounds like your web host is blocking outbound connections to smtp.gmail.com:465. Suggestions:
Verify: If you have shell/terminal access to your web hosting server, try a telnet test to verify that they are in fact blocking this. Run telnet smtp.gmail.com 465
Contact: Call or email your hosting provider and find out what SMTP server they provide for outbound relay. Make sure they know you want to use your #gmail.com address as the From/Reply-to address.
Update code: Once your host provides you with a different mail server, update your code and try again.
If your web host doesn't allow outbound relay from its servers at all, then you need to look at switching hosts, if this is a requirement for your application.
AJ is right in regards to #2, it is specified in:
http://support.google.com/mail/bin/answer.py?hl=en&answer=78775
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.abc.co.in';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Username = 'user name';
$mail->Password = 'password';
$mail->setFrom("from email id", "name");
$mail->addAddress("receipiant email id");
$mail->isHTML(true);
$bodycontent = 'body';
$mail->Body = $bodycontent;
if (!$mail->send()) {
echo "message has been not send", $mail->ErrorInfo;
}
else{
echo "message has been send";
}