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".
Related
I had successfully setup a web app using WAMPSERVER on a desktop used by a few people internally, this used PHPMailer to an internal SMTP server without encryption or authentication and it worked.
That desktop crashed and I've migrated to a "new" desktop. I had an SVN setup so I was even using most of the same files and config. One difference which might matter is that the old desktop was 64-bit and the new is 32-bit. This means I'm using different versions of WAMPSERVER.
The mailer just hangs. I don't get a PHP error or a PHP timeout. I just never reach the end of my script. The crazy part about this is that it works with authentication, ssl, and gmail. It just won't work with the extra simple case I need.
This works:
<?php
require('class.phpmailer.php');
$mail=new PHPMailer();
$mail->ISSMTP();
$mail->Host='smtp.gmail.com';
$mail->Subject='test subj';
$mail->Body='the body email test';
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$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 for the GMAIL server
$mail->Username = "myemail#gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
$mail->AddAddress('toemail#supersecret.com', 'John Doe');
$mail->SetFrom('something#gmail.com', 'First Last');
$mail->Send();
?>
this used to, but now does not:
<?php
require('class.phpmailer.php');
$mail=new PHPMailer();
$mail->ISSMTP();
$mail->Host='smtp.internal.com';
$mail->Subject='test subj';
$mail->Body='the body email test';
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->AddAddress('myaddress#somewhere.com', 'John Doe');
$mail->SetFrom('someaddress#mightbereal.com', 'First Last');
$mail->Send();
?>
The only thing I get from debug is
CLIENT -> SMTP: EHLO thedesktophostname
No errors display on the page and nothing in the apache log, where I normally get PHP errors, if they don't display.
I can telnet to the host from the desktop on port 25 and even type in the EHLO command and get a good response from the server.
I don't remember having this issue before, although it's possibly I've already solved it once. I couldn't find anything that helped here or on The Google.
Please help. Thanks.
Hijacking the post to say i had the same issue but had set the port to 465 without setting SMTPSecure to 'ssl' in the example its set TLS by default
If you have a server hosted in Hostgator (shared hosting), this is what solved for me:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
(even though the official example in PHPMailer suggests using ENCRYPTION_STARTTLS)
sadly this probably won't ever help anyone else who has this same problem, but I was able to get everything working by just changing the port to 465.
Eventually found solution for my configuration.
Just add ssl:// to smtp.google.com
$mail->Host = 'ssl://smtp.gmail.com';
I had the same issue. Nothing displays after the send method.
I realized that the encryption was wrong, I did use SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// Enable TLS encryption, `PHPMailer::ENCRYPTION_SMTPS` also accepted
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.
In the company I work for we use microsoft exchange server and ofcourse ms outlook to send and recieve email.
on andriod we use exchange application as following to access our mail.
I am trying to use the following code to send emails IN php:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.dom-domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password123'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('username#dom-domain.com', 'Mailer');
$mail->addAddress('recname#yahoo.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('username#dom-domain.com', 'Information');
//$mail->addCC('cc#example.com');
//$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 = 'Here is the subject tls587';
$mail->Body = 'This is the HTML message body tls587 <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients tls587';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
but the page loads alot and I get this error:
2017-04-02 13:13:45 Connection: opening to mail.dom-domain.com:587, timeout=300, options=array ( ) 2017-04-02 13:14:48 Connection failed. Error #2: stream_socket_client(): unable to connect to mail.dom-domain.com:587 (Connection timed out) [/home/xxxxxxx/public_html/ml/PHPMailer/class.smtp.php line 294] 2017-04-02 13:14:48 SMTP ERROR: Failed to connect to server: Connection timed out (110) 2017-04-02 13:14:48 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
Keep noted that on your Android device you are mostly using Microsoft Active Sync via port 443. You do NOT use a native SMTP connection here via port 587 (which would be used via IMAP/POP3). The same is true for a plain Outlook connection where MAPI or MAPI over HTTP/Outlook Anywhere is used. Both do not use a native SMTP connection here and might be the reason why it isn´t working. BUT on your GMail Account you are mostly using IMAP as this isn´t an Exchange Server which support Microsoft Active Sync.
It might therefore possible that your Exchange Administrator didn´t configured the SMTP Connector for external Authenticated connections for Outlook users as this isn´t needed in your case and therefore disabled for security reasons. As you didn´t specify if you are the Exchange Administrator and which version you are using its not easy to give you a how to here. So instead of that, you should follow these howtos here from Microsoft:
Controlling SMTP Relaying with Microsoft Exchange
Configure authenticated SMTP settings for POP3 and IMAP4 clients in Exchange 2016
If you aren´t an Microsoft Exchange Administrator you can try to send emails via the Microsoft Exchange Webservices (see here). However as said above, the Exchange Administrator might run a special configuration here to secure the environment.
My project contains a function to send email, being PHPMailer. It runs well to send email from my localhost server, but it stopped sending email today, and now it shows this error message
SMTP Error: Could not connect to SMTP host.
I added this code $mail->SMTPDebug = 1; to view debug errors and is now showing me this message:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found. (0) SMTP Error: Could not connect to SMTP host
I already have enabled extension=php_openssl.dll in php.ini.
This is my code:
$mail = new PHPMailer();
$mail->SMTPSecure = 'ssl';
$mail->IsSMTP();
$mail->Username = "myemail#gmail.com"; // your GMail user name
$mail->Password = "password";
$mail->AddAddress($email); // recipients email
$mail->FromName = "username"; // readable name
$mail->IsHTML(true);
$mail->Subject = "title";
$mail->Body = " Message";
$mail->SMTPDebug = 1;
$mail->Host = "ssl://smtp.gmail.com"; // GMail
$mail->Port = 465;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if($mail->Send()){
} else {
}
thanks
This is commonly reported as a PHPMailer problem (and there are many duplicates of this question), but it's almost always down to local DNS failure, firewall blocking or other network issue on your local network.
First, make sure you are using the latest PHPMailer.
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead:
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
or more succinctly:
$mail->Host = 'tls://smtp.gmail.com:587';
You can your connectivity this by running some commands on your server (you will need dnsutils and telnet packages installed). First check DNS is working:
dig +short smtp.gmail.com
You will get something like this if your DNS is working:
gmail-smtp-msa.l.google.com.
173.194.67.108
173.194.67.109
Next try to telnet to the host on the port you need:
telnet smtp.gmail.com 587
This should give you something like this:
Trying 173.194.67.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp
(Enter quit to get out of that).
If either of these fail, PHPMailer will not work either. So go fix your network, then try again. If you are not in control of your own firewall or DNS, you probably need to raise a support ticket with your ISP to fix this. If they won't fix it, you need to replace your ISP.
Back in PHPMailer, you can get lower-level feedback on the connection by setting:
$mail->SMTPDebug = 4;
A simple google search revealed this forum - http://forums.devshed.com/php-development-5/unable-to-find-the-socket-transport-ssl-667689.html
I've been using PHPmailer (https://github.com/Synchro/PHPMailer) to send email through amazon SES for a few months. At some time in the last two weeks it has stopped working, and I haven't touched it. I'm getting an error msg:
SMTP Error: Could not connect to SMTP host.
This is my code.
public function sendEmail($to,$subject,$body){
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'amazonaws....'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mySMTPuname'; // SMTP username
$mail->Password = 'smtpPword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'example';
$mail->FromName = 'me';
$mail->AddAddress($to); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
My amazon account is still upto date and active. Is there any way to print out more detailed error msgs for debugging? Has there been any known issues lately?
Try :
$mail->SMTPDebug = 1;
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
This is a very old question but I just had the same problem so it may still be relevant to others.
If it stopped working without you changing anything it is probably connected to your hosting company / isp blocking SMTP traffic from your server to other servers.
There are a few topics on this, as multiple hosting companies using Cpanel and also Godaddy have implemented such measures for combating spam.
Try:
$mail->SMTPDebug = 3;
to get the maximum level of detail on the error.
One solution is to use the mail server in the same hosting account (if it blocks SMTP to the outside it probably has an internal service you can use).
To keep using Amazon SES you need to open SMTP traffic on your server.
IF you have control over Cpanel/WHM "tweak settings" you can do it yourself, otherwise you need to ask your hosting provider.
Check this answer for details "Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer