Why is Amazon SES SMTP only working on port 443? - php

I'm setting up a PHP script that uses PHPMail to send email. The "from" address is already verified on Amazon's console, and I have created the IAM user and SMTP credentials. When creating those, Amazon tells you to use ports 25, 465 or 587. Here's a php example from Amazon's documentation, doesn't use PHPMail but the idea should be the same.
This is my script:
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->Timeout = 20;
$mail->CharSet = 'UTF-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-east-1.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my_username'; // SMTP username
$mail->Password = 'my_password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('my#email.com', 'My email name');
$mail->addAddress('test#test.com', 'Test'); // Add a recipient
$mail->addReplyTo('my#email.com', 'My email name');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Amazon SES SMTP test with PHPMailer';
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
That script like that doesn't work, I get a connection timeout error. However, if I change the port to 443, then it works fine. Why is that? That's not a port listed by Amazon so I'm worried that even though it works now it might give some other problems in the future. Am I missing something here? Port 465 doesn't work either, by the way.
EDIT
Just for clarification, I realize this works using port 443 like I mention, however why isn't this working with the ports they suggest? That's what I'm trying to understand. Is there anything missing in this script? I've also teste without pre-fixing the host with "ssl://" (which is how they show it in their example) and using the suggested ports, to no avail.

TCP port 443 is the standard TCP port that is used for websites which use SSL. your address is
ssl://email-smtp.us-east-1.amazonaws.com
so to me this is working as it should.

The problem was that there was a setting in our CSF Firewall called SMTP_BLOCK that was on. We turned that off and now port 587 works fine (I've had also to remove the ssl:// from the host address).
Maybe this helps someone in the future with the same problem.

Related

Troubleshooting PHPMailer with Bluehost

Trying to set up PHPMailer for a site I have hosted on Bluehost and after a full day of researching and troubleshooting I just can not get it working.
I'm a beginner so apologies in advance for the newbie question, but I've been reading everything I can find (including this and this, as well as the PHPMailer docs) to solve this but can't seem to get mine set up correctly. Any guidance, thoughts on what I'm doing wrong or ways to debug this are much appreciated.
This is what I've found regarding Bluehost SMTP.
Secure SSL/TLS Settings (Recommended)
Username Your email address: john#example.com
Password The password for that email account.
Incoming Server mail.example.com*
Incoming Port 993 (IMAP) or 995 (POP3)
Outgoing Server mail.example.com*
Outgoing Port 465 (SMTP)
Authentication Password
*Replace example.com with your domain name.
Below is what I'm using in my file (personal info removed).
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.MYDOMAIN.com';
$mail->SMTPAuth = true;
$mail->Username = 'MYEMAIL#MYDOMAIN.com';
$mail->Password = 'MYEMAILPASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 465;
//Recipients
$mail->setFrom('MYEMAIL#MYDOMAIN.com');
$mail->addAddress('MYEMAIL#MYDOMAIN.com');
//Content
$mail->isHTML(true);
$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;
}
I've installed PHPMailer using Composer so the autoload.php file (and the phpmailer folder) are in a 'vendor' folder located in the same directory as the file that contains the code shown above.
After uploading to my Bluehost server, when I try to display the webpage that has this code in the browser, I get this HTTP ERROR 500 screenshot and of course no email sent.
Something that's covered in many of the examples and the docs is what combinations of encryption and port settings will work.
You have Port = 465 and SMTPSecure = 'tls'; that won't work. Either change Port to 587, or SMTPSecure to 'ssl' (but not both!). As it stands, you're trying to open a connection to a port that expects implicit TLS while using a protocol that expects to need to make it explicit with STARTTLS, and that's not going to work.

Could not connect to SMTP host. Message could not be sent

When I sent email from server then it gives me two error -
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
I found many hints on another answers on Stack overflow but doesn't work. I tried port no 465/587/65. Even below code works proper on my local system If i set port 587. But in server, It doesn't work.
require('class.phpmailer.php');
require('class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Username = "FromEmailId";
$mail->Password = "Password";
$mail->Port = 465;
$mail->From = "FromEmailId";
$mail->AddAddress("ToEmail");
$mail->IsHTML(true);
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
Try phpmailer in debug mode to check the error
$mail->SMTPDebug = 3; // Enable verbose debug output
First logout the your gmail account
then open this url
use this yrl
click the continue button
Next
change the port and SMTPsecure
$mail->SMTPSecure = "tls";
$mail->Port = 587;
The Gmail Help :
Still can't send mail?
If you tried configuring your SMTP server on port 465 (with SSL/TLS) and port 587 (with STARTTLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL/TLS).
This question failed to do a basic search and missed a vital piece of information later mentioned in comments: the OP is using GoDaddy. GoDaddy is well known to block outbound SMTP, so any suggestions relating to switching port numbers or security protocols will not help. You need to read other questions on GoDaddy's mail handling, and read their support documentation, which will push you in the direction of using GoDaddy's mail servers (using securehosting domains) or via localhost as a relay to the same. All of this is also covered in the PHPMailer troubleshooting guide.
In future, search before posting, as per the SO guidelines.

sending mail using Php mailer working in local system , failing in godaddy shared server

I am trying to send an email using gmail SMTP settings.
I tried in my localhost , it worked .
I downloaded the phpMailer , in that folder I wrote a PHP file with following contents.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'mygmail#gmail.com'; // SMTP username
$mail->Password = 'mygmailpassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'myfromadress#gmail.com';
$mail->FromName = 'Mailer';
$mail->addAddress('mytoadress.com', 'My name'); // Add a recipient
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
It worked in local system, But not working in the live server.
I am using Godaddy's Linux shared hosting service.
I live server its throwing the below error.
Message could not be sent.Mailer Error: SMTP connect() failed.
I cannot comment on your question because I don't have enough credentials... So I have to write an answer even though it's not technically an answer... I had similar problems a while ago. The bug was in my code. The short story is, turn on debug mode and you will have the exact reason why gmail is refusing to connect.
I'm with GoDaddy. You can't use Gmail's SMTP server. You need to use our relay server. Take a look at https://support.godaddy.com/help/article/953/what-is-the-name-of-my-hosting-accounts-relay-server for details. Feel free to follow up with me on Twitter #GoDaddyHelp if you have other questions. ^Nate

Mails not sending using PHPMailer and SSL

I configured my mail server as in the following guide:
https://www.digitalocean.com/community/tutorials/how-to-configure-a-mail-server-using-postfix-dovecot-mysql-and-spamassasin
Now I am trying to send mails via PHP using this library:
https://github.com/PHPMailer/PHPMailer
But the problem seems the SSL (cert). I am not sure if the script is not able to establish a connection or if the problem is the unverified SSL cert.
Do you guys have any idea?
Should I use startssl oder starttsl in the script?
SSLaccept error from x.com[x.x.x.x]: 0
Dec 8 17:45:23 x postfix/submission/smtpd[13479]: warning: TLS library problem: 13479:error:14094418:SSL routines:SSL3READBYTES:tlsv1 alert unknown ca:s3pkt.c:1258:SSL alert number 48:
My code so far:
function send_mail($from, $from_name, $to, $to_name, $subject, $content)
{
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.x.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply#x.com'; // SMTP username
$mail->Password = 'so secret'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = $from;
$mail->FromName = $from_name;
$mail->addAddress($to, $to_name); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $content;
$mail->AltBody = 'Please use an html compatible Client to view this mail!';
if(!$mail->send()) {
return false;
} else {
return true;
}
}
I had the same error, and finally figured out that phpmailer wants to use a normal smtp connection and then STARTTLS or STARTSSL to initialize encryption. In other words: it wants to connect to a submission service instead of an smtps service. Submission is on port 587 (usually) and smtps is on port 465. In fact, the smtps protocol was deprecated a long time ago. There are still many ISPs out there using smtps, but apparently they shouldn't.
If your mail provider allows you to use submission (port 586) instead of 465, then change your port number in your config and it will work! If you happen to be the sysadmin on the smtp server, then simply start a submission service, listening on port 587. If your ISP only supports smtps, then probably you are stuck. It seems that phpmailer does not handle SMTPS connections that are encrypted from the beginning. It only support connections that can be switched to encrypted (using STARTTLS).
The difference between the two is also described here:
What is the difference between ports 465 and 587?
UPDATE: before you try sending out any email, please update to the very latest version of phpmailer. Download the latest from github ( https://github.com/PHPMailer/PHPMailer ). Do not try to use any other version! Sometimes a small change in the minor version will decide between a successfully sent email and an "smtp connection error".

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: xxxxx#gmail.com ERROR

In contact form it's working fine in localhost. While hosted it's not working. Showing the error
**"SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: xxxxx#gmail.com ERROR"**
I attached my contact_submit.php code form
include_once('class.phpmailer.php');
$mail->IsSMTP(); //
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxx#123";
$mail->SMTPSecure = "tls";
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$mail->Subject = "Contact - xxx";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("xxx#gmail.com","xxx");
if(!$mail->Send())
{
echo $mail;
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=contact.php?id='.$id.'&send=success">';
exit;
}
I'm using phpmailer 5.2.1.
I contacted the hosting side, but i'm not getting actual response.
I believe you have to connect to smtp.gmail.com on port 465, not port 587. Also, SSL is required. So, you should have:
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
You can increase the time out by prepending your code with:
set_time_limit(3600);
and then specifying the Timeout of the $mail object as such:
$mail->Timeout = 3600;
I had a similar problem, with mail being sent correctly from my local server but not my live one on the internet. It turned out my host (Bluehost) blocked outgoing connections on port 465.
I found a wonderful how-to which fixed it for me:
In your cPanel > Mail, find the MX (MX Entry) section, and select 'remote mail exchanger'.
In the cPanel email accounts section, create the appropriate email address (don't skip this)
Don't use "smtp.live.com" as your smtp host. Use the smtp host of your Shared Linux Hosting smtp. I don't know how you will get yours. Mine is boxXXXX.bluehost.com.
Set your username and password to be the same as the email account you just set-up in cPanel.

Categories