Reputation: 31
For the past 2 days I've been trying to get a PHP script to send an e-mail and it doesn't seem to work. First I've tried the normal php mail function, then PHPMailer and then Swiftmailer.
I've tried to use both gmail's accounts and SMTP and the email account and SMTP of my internet provider, none worked. I tried to ping both SMTP servers and they worked fine and telnet replied correctly as well. On telnet smtp.gmail.com 587 it said 220 smtp.gmail.com ESMTP q125sm9630397wmd.19 - gsmtp. I've tried both port 465 and 587 and combinations with tls and ssl. For gmail I also edited my gmail account to make sure it's allowed for less secure devices and to allow imap etc.
I'm running apache on my own pc on windows 7. I've forwarded ports 465 and 587. I've already gone through the troubleshooting page of PHPMailer. I also disabled the firewall for testing, but that didn't help.
I have edited the php.ini settings correctly and also made sure the ; was removed from extension=php_openssl.dll.
This is my code for PHPMailer based on the example PHPMailer provided for gmail (I edited out the SMTP, accounts and passwords):
require '/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.x.x'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]'); // 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()) {
$checkup = 'Something went wrong';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$checkup = 'Success';
}
When I use that on my internet provider's SMTP with my internet provider email address I get this error:
2016-05-06 12:27:04 Connection: opening to smtp.x.x:587, timeout=300, options=array ( ) 2016-05-06 12:27:04
Connection: opened 2016-05-06 12:27:04
SERVER -> CLIENT: 220-smtp6.mnd.mail.iss.as9143.net ESMTP Fri, 06 May 2016 14:27:07 +0200 220-Sending spam or unsolicited commercial e-mail to this server is strictly 220-prohibited by our NO UBE / NO UCE policy. Abuse will be prosecuted and/or 220 charged per attempted recipient at international postal rates. 2016-05-06 12:27:04
CLIENT -> SERVER: EHLO localhost 2016-05-06 12:27:04
SERVER -> CLIENT: 250-smtp6.mnd.mail.iss.as9143.net Hello (MY IP ADDRESS, edited out for security) 250-SIZE 31457280 250-8BITMIME 250-PIPELINING 250-STARTTLS 250 HELP 2016-05-06 12:27:04
CLIENT -> SERVER: STARTTLS 2016-05-06 12:27:04 SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 12:27:04 SMTP Error: Could not connect to SMTP host. 2016-05-06 12:27:04 CLIENT -> SERVER: QUIT 2016-05-06 12:27:04
SERVER -> CLIENT: MIA�x�����dqi7��=���&J��3ʚ���ă�S�_��Z�$��NH�؏�|��m>[z@�f�g��G����.pAGD�8Q�:��J���'L�a8%��v�(*YC��u�ФW�s����l�#6F�w���� ���9�|�\%�!�����B=:��/U��m��bc\�cڊ��4��d�-U��seEZ�PC��Q�Ai`JㅇK�U�]h���H�v���i���cF M�x ���}���V�HR�c�!��,`� *)�$Oȕ�EMƍ�&B�+���u9A�R��j'[7��y����a ����N���� 2016-05-06 12:27:04
SMTP ERROR: QUIT command failed: MIA�x�����dqi7��=���&J��3ʚ���ă�S�_��Z�$��NH�؏�|��m>[z@�f�g��G����.pAGD�8Q�:��J���'L�a8%��v�(*YC��u�ФW�s����l�#6F�w���� ���9�|�\%�!�����B=:��/U��m��bc\�cڊ��4��d�-U��seEZ�PC��Q�Ai`JㅇK�U�]h���H�v���i���cF M�x ���}���V�HR�c�!��,`� *)�$Oȕ�EMƍ�&B�+���u9A�R��j'[7��y����a ����N���� 2016-05-06 12:27:04
Connection: closed 2016-05-06 12:27:04
SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
As you can see it connects, the SMTP server seems to recognize me by IP and says 'go ahead' and then it suddenly fails. I've been looking everywhere and I'm completely clueless as to how I can fix it.
When I use gmail's SMTP + the gmail account it says this:
2016-05-06 17:42:40 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( ) 2016-05-06 17:42:40
Connection: opened 2016-05-06 17:42:40
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 8sm9564071wms.14 - gsmtp 2016-05-06 17:42:40 CLIENT -> SERVER: EHLO localhost 2016-05-06 17:42:40
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [MY IP ADDRESS] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250 SMTPUTF8 2016-05-06 17:42:40
CLIENT -> SERVER: STARTTLS 2016-05-06 17:42:40
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2016-05-06 17:42:40 SMTP Error: Could not connect to SMTP host. 2016-05-06 17:42:40
CLIENT -> SERVER: QUIT 2016-05-06 17:42:40
SERVER -> CLIENT: MIA��b8 ��4}�5KO�q0$&��B.r��Q���P��=���r$�%➽,��X�"�c�5ȭ�D�C�b���B��@��T�?bڌ_�*܋����X1`T%_��� �QQ����y}]OO�����8d�R|�$�����b�����[� ,��5�[*�H����5�l���B����a����f��f���Pj��c@>����vk+Cq�r*��-�4���`W勱A``A�S ,�w:�q�1�����.����*�',ȁ��iH�T0�E�/1W�� |�i4�{�Gk�J�n,US�F�|���|L����n}Qp� 2016-05-06 17:42:40
SMTP ERROR: QUIT command failed: MIA��b8 ��4}�5KO�q0$&��B.r��Q���P��=���r$�%➽,��X�"�c�5ȭ�D�C�b���B��@��T�?bڌ_�*܋����X1`T%_��� �QQ����y}]OO�����8d�R|�$�����b�����[� ,��5�[*�H����5�l���B����a����f��f���Pj��c@>����vk+Cq�r*��-�4���`W勱A``A�S ,�w:�q�1�����.����*�',ȁ��iH�T0�E�/1W�� |�i4�{�Gk�J�n,US�F�|���|L����n}Qp� 2016-05-06 17:42:40
Connection: closed 2016-05-06 17:42:40 SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
On debug mode 4 it says the same (this shows only a part of the huge text wall you get then):
CLIENT -> SERVER: STARTTLS 2016-05-06 17:55:10
SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10
SMTP -> get_lines(): $str is "220 TLS go ahead " 2016-05-06 17:55:10
SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 17:55:10 SMTP Error: Could not connect to SMTP host. 2016-05-06 17:55:10
CLIENT -> SERVER: QUIT 2016-05-06 17:55:10
SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10
SMTP -> get_lines(): $str is "MIA " 2016-05-06 17:55:10
SMTP -> get_lines(): $data is "MIA " 2016-05-06 17:55:10
It says Ready to start TLS and then it says connection failed again. I've read a lot of topics about this on stackoverflow, but none of the solutions and options offered there has worked so far. Does anyone have any idea?
Upvotes: 3
Views: 12464
Reputation: 1
I have the same problem, I tried with SSL & port 465 then it said service is unavailable then I tried this thing, it solved my problem. Hopefully, it would solve yours:
Make sure you use $mail->IsSMTP();
for the authentication. Ismail is only working for the same server with the mail function
This thing fix my problem
$mail->SMTPSecure = "tls"; $mail->Port = 587;
i add these two lines now email sending is perfectly working earlier i face this error SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting but above thing solve my problem hopefully it work for you thank you
Upvotes: 0
Reputation: 645
The SMTPOptions line in @David Kariuki's solution fixed my issue. Was getting a SMTP connect failure due to the failure in the TLS handshake.
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
Upvotes: 7
Reputation: 1756
Here is the PHP Mailer Example code. Ensure to download the PHPMailer Classes from https://github.com/PHPMailer/PHPMailer
To find your mail server,
If you are using windows operating system:
If you are using Linux
Mostly, smtp servers name are something like smtp.yourdomain.com or mail.yourdomain.com.
Open command prompt try to run following two commands:
You will most probably get response from any one from the above two commands and that might be your smtp server.
If this doesn't work open your cpanel --> go to your mailing accounts -- > click on configure mail account settings-- > there somewhere in the page you will get the information about your smtp server
It will be written like this way may be :
Here's the 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;
require 'your_directory/php_mailer/Exception.php';
require 'your_directory/php_mailer/PHPMailer.php';
require 'your_directory/php_mailer/SMTP.php';
$to = '[email protected]';
$name = 'my first name';
// Passing `true` enables exceptions
//$mail = new PHPMailer(TRUE);
$mail = new PHPMailer();
try {
//Set SMTP Options
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Server settings
// Enable verbose debug output
$mail->SMTPDebug = 2;
// Set mailer to use SMTP
$mail->isSMTP();
// Specify main and backup SMTP servers
$mail->Host = 'mail.your_domain.com;your_backup_smtp.your_domain.com.co.ke';
// Enable SMTP authentication
$mail->SMTPAuth = true;
// SMTP username (This is smtp sender email. Create one on cpanel e.g noreply@your_domain.com)
$mail->Username = 'sender_email@your_domain.com';
// SMTP password (This is that emails' password (The email you created earlier) )
$mail->Password = 'your_password';
// Enable TLS encryption, `ssl` also accepted
$mail->SMTPSecure = 'tls';
// TCP port to connect to. the port for TLS is 587, for SSL is 465 and non-secure is 25
$mail->Port = 25;
//Recipients
$mail->setFrom('sender_email@your_domain.com', 'Company Name');
// Add a recipient
$mail->addAddress('[email protected]', 'Recipient Name');
// Name is optional
//$mail->addAddress('[email protected]');
$mail->addReplyTo('info@your_domain.com', 'Information Team');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
//Attachments (Ensure you link to available attachments on your server to avoid errors)
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'some_imaje.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b><i>in bold and italicized!</i></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;
}
?>
Upvotes: 3
Reputation: 21
The same problem I had using Gmail and I solved it by downloading the cacert.pem certificate from the site https://curl.haxx.se/docs/caextract.html You should also write the php.ini file as follows:
extension = php_openssl.dll openssl.cafile = C: \ xampp \ php \ extras \ ssl \ cacert.pem
It must be activated in the GMail account in the label: Access and security of the option: Allow less secure apps access option: ON
This solution is thanks to matteobin user contribution, of stackoverflow
Upvotes: 1