Unable to send mail from php - php

We are trying to send mail from a PHP program on bigrock , mail server is on Digital Ocean
But mails are not getting sent
Would there be any thing wrong with the way the mail server is specified ?
<?php
require 'Exception.php';
require 'PHPMailer.php';
//Load Composer's autoloader
require 'SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true); // Passing true enables
exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mandify.in'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'ABC#mandify.in'; // SMTP username
$mail->Password = 'XYZ'; // SMTP password
$mail->SMTPSecure = 'STARTTLS'; // Enable TLS encryption, ssl also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('ABC#mandify.in', 'Mailer');
$mail->addAddress('QWERTY#gmail.com', 'Joe User'); // Add a recipient
//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. Mailer Error: ', $mail->ErrorInfo;
}
?>
Same Code was also run for mail server on bigrock (same server as execution)
but that too did not work
This is the error message:
Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
There seems to be something wrong with the code as per feedback from server admin team.

This is likely a configuration problem or an indication of a missing dependency. One thing to try to avoid failure of certificate verification is to configure the following:
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
]

Related

PHPMailer 6.2 stream_socket_client

I switched over to using PHPMailer 6.2 (which is the master version, and most recent version).
I am using Apache 2.4 on a Windows Server 2019. The version of PHP I am using is 7.4.12.
Following this tutorial: https://www.youtube.com/watch?v=t0zwgJrSHd4
I am not having much luck using the below code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailerAutoload.php';
require 'phpmailer/class.phpmailer.php';
require 'phpmailer/class.smtp.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'xx.x.x.xxx';
$mail->SMTPAuth = false; // <- just changed to false
$mail->Username = 'mycompany#email.com';
$mail->Password = 'mypassword';
$mail->Port = 25;
$mail->SMTPSecure = 'tls';
// update
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
// end update
$mail->setFrom('mycompany#email.com', 'Mailer');
$mail->addAddress('myoutsidefacingemail#gmail.com', 'TEST');
$mail->addReplyTo('mycompany#email.com', 'Information');
$mail->addCC('mycoworker#email.com');
$mail->isHTML(true);
$mail->Subject = 'This is a test subject';
$mail->Body = '<h1>This is the HTML message body <b>in bold!</b></h1>';
$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';
}
?>
I have edited this question as I have made some updates (see above code).
Using the above, I can send email internally, on our network.
The problem is sending external email.
Here is the error message I am receiving when trying to send to my gmail (I did not paste the full error message, just the last few lines):
2021-01-21 21:02:26 CLIENT -> SERVER: QUIT
2021-01-21 21:02:26 SMTP INBOUND: "221 2.0.0 Service closing transmission channel"
2021-01-21 21:02:26 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
2021-01-21 21:02:26 Connection: closed
SMTP Error: The following recipients failed: myoutsidefacingemail#gmail.com:
SMTP; Unable to relay recipient in non-accepted domain
Message could not be sent.Mailer Error: SMTP Error: The following recipients failed: myoutsidefacingemail#gmail.com: SMTP; Unable to relay recipient in non-accepted domain

peer certificate did not match "smtp.office354.com" using phpmailer

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.

PHPMailer - Mails not using ses server(No support from aws)

I have an ec2 server subscription and I am trying to use SES to send emails using PHPMailer. For some reason, Mails are not being sent from SES server. Mails go from some other server. I spoke with an AWS support member and he confirmed that all the code i have is correct and he cant provide any help with my php code.
His reply was
Thank you for your time speaking today.
As discussed, your a PHP Mailer is not relaying on SES, despite your configuration seems to be correct, it's not triggering SES SMTP Servers.
The line below shows that your message was sent from the EC2 directly, without pass through SES.
Received: from ip-xxxxx.ap-southeast-1.compute.internal (ec2-yyyyyy.ap-southeast-1.compute.amazonaws.com. [yyyyyyy])
The sample PHPMailer code i have is
<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
//$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'email-smtp.us-east-1.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'smtp-username'; // SMTP username
$mail->Password = 'smtp-password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('contact#example.com', 'John Doer');
$mail->addAddress('abc#gmail.com', 'Random'); // Add a recipient
$mail->addAddress('xyz#gmail.com'); // Name is optional
$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';
}
Any help is appreciated.

PHP Mailer to outlook: Data not accepted Recipient address reserved by RFC 2606

When i try to send an email to my outlook account I receive an error: SMTP Error: data not accepted. Message could not be sent.Mailer Error: SMTP Error: data not accepted.SMTP server error: DATA END command failed Detail: 501 5.1.5 Recipient address reserved by RFC 2606 SMTP code: 550 Additional SMTP info: 5.3.4. I tried with my Gmail account and it works but not with outlook live. Anyone can help me?
Here's the code:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP();
$mail->Host = 'smtp-mail.outlook.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'andreacivitas#hotmail.it'; // SMTP username
$mail->Password = '***********'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('andreacivitas#hotmail.it');
$mail->addAddress('andreacivitas#hotmail.it', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$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';
}
?>
RFC2606 defines some domains that are designated as being for example use only, and are guaranteed never to exist. in particular these include example.com, example.org and example.net. This means that you can use reasonable-looking addressees in example code without fear of inadvertently sending email or other traffic to random people, which might happen if you use a made-up name like mydomain.com because it could actually exist.
The error you're seeing has recognised that you're using a reserved domain like this and so is refusing to accept your submission. Use real addresses, or remove the lines that use the reserved addresses, and it will work.
You've set the SMTPSecure option incorrectly - it is case-sensitive, so it should be:
$mail->SMTPSecure = 'tls';
You're disabling certificate verification via SMTPOptions - Outlook/Hotmail etc usually serve verifiable certificates, so you should only disable verification to solve a specific problem as it is not a safe way to avoid verification issues, i.e. don't do it unless you really need to for a known, specific reason.

Getting error SMTP connect() failed phpmailer with localhost

I'm getting error SMTP connect() failed phpmailer with localhost:
2015-09-10 09:34:48 Connection: opening to ssl://smtp.gmail.com:587,
timeout=300, options=array ( ) 2015-09-10 09:34:48 SMTP ERROR: Failed
to connect to server: (0) 2015-09-10 09:34: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
Is it phpmailer cannot run at localhost?
If I want really run at localhost how can I change the code?
I tried using mailto function is work for me but I want change whole thing.
can give any suggestion. I really want to learn it.
This is the code from github:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 4; // 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 = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->From = 'ikramlim#gmail.com';
$mail->FromName = 'Mailer';
$mail->addAddress('ikramlim#gmail.com', 'Joe User'); // 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';
}
PHPMailer is normally working on all systems.
I think you have an old openssl extension or your extension is not enabled. Check that with a phpinfo() if there is openssl enabled.
If not then enable it in your php.ini.
And the other way is that it seems that Google has some problems. When i try to send an Email at the moment i have connection problems with Thunderbird, too. I have to try it some times to connect to gmail.
My suggestion is that please try using 'tls' with port 587 instead of 'ssl' with port 587.
Also check whether you are including class.phpmailer.php(ie: require 'class.phpmailer.php';) , If you are doing please comment it and give a try.

Categories