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
Related
I am working on a website with a form that is used to send email with PHPMailer. i have a GoDaddy hosting plan linux. I have tried multiple ways without any success, some time ago it worked and now does not work.
Configuration 1 with Gmail
include_once('phpmailer/class.phpmailer.php');
include_once('phpmailer/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->SMTPSecure = false; (I've tried the 2 options)
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->Username ='xxxx#gmail.com';
$mail->Password = 'xxxxxxx';
$mail->Subject = 'Form from website';
$mail->AddAddress("xxxxx#xxxx.com");
$mail->FromName = "formsite";
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
Log
Connection: opening to relay-hosting.secureserver.net:25, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Configuration 2 email from the same domain
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = true; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Host = 'p3plcpnxxx.prod.phx3.secureserver.net';
$mail->Port = 465;
$mail->Username ='noreply#samedomain.com';
$mail->Password = 'xxxxxxxx';
$mail->Subject = 'Form from website';
$mail->AddAddress("xxxxx#xxxx.com");
$mail->FromName = "formsite";
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
Log
SERVER -> CLIENT: 220-p3plcpnxxxx.prod.phx3.secureserver.net ESMTP Exim 4.89 #1 Thu, 14 Dec 2017 21:11:11 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO www.xxxxxx.com
SERVER -> CLIENT: 250-p3plcpnxxxx.prod.phx3.secureserver.net Hello
p3plcpnxxxx.prod.phx3.secureserver.net [180.168.200.196]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-CHUNKING250 HELP
To help others struggling with GoDaddy, these two variations worked for me:
Hosting: GoDaddy shared/cPanel
Sending "From:" a GMail address
PHPMailer version 6.0.5
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$send_using_config = 1; // set to 1, 2, etc. to test different settings
switch ($send_using_config):
case 1:
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->SMTPSecure = FALSE;
$mail->SMTPAuth = FALSE;
$mail->SMTPAutoTLS = FALSE;
break;
case 2:
# Host amnd Port info obtained from:
# Godaddy > cPanel Home > Email > cPanel Email > Mail Configuration > "Secure SSL/TLS Settings" > Outgoing Server
$mail->Host = 'a2plcpnxyzw.prod.iad2.secureserver.net';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = FALSE;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed' => TRUE
)
);
break;
endswitch;
$mail->Username = 'you#gmail.com';
$mail->Password = 'your_gmail_password';
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->send();
These settings are based on the question posted here and modified with the advice from #Synchro on StackOverflow and GitHub. Thank you #Synchro!
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 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.
Have a basic contact form using https://github.com/PHPMailer/PHPMailer on a Network Solutions shared hosting package.
// start the mail request basics
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->Debugoutput = 'html';
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = 'my_gmail_account#gmail.com';
$mail->Password = '*******';
Everything works find locally (on my computer) and form sends email, but when I upload this php file to our network solutions hosting I get the following Error.
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Is there something wrong with my Mail Settings? Anyone else had success with PHP mailing on Network Solutions Shared Hosting?
Network Solutions blocks several ports required to send mail including those needed for gmail.
If anyone finds an alternate, please let me know.
For now, used a dummy account and used standard PHP mailer then forwarded mail.
You could change your config options like this.
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "my_gmail_account#gmail.com";
$mail->Password = "********";
$mail->SetFrom("somemail#gmail.com");
$mail->Subject = "Testing the New Config";
$mail->Body = "Hey there !";
I got it to work with these settings:
$mail->SMTPOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
)
);
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->Host = "email-out-priv.myregisteredsite.com";
$mail->Port = 25;
$mail->Username = "noreply#mydomain.com";
$mail->Password = "********";
You have to pay for an email address with Network Solutions for a domain you own. You can see the host and port by logging into your mailbox (mail.mydomain.com) --> Top right Menu --> Settings --> Mail --> Mail and Social Accounts --> E-Mail (Edit) --> Outgoing server (SMTP). I used this PHP Mailer Documentation as a clue to this solution.
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";
}