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.
Related
First, I already read this stack post, and it doesn't seem to answer my question. I'm unclear how to apply those findings to my situation.
The following code works fine on my LAMP server (Bluehost), and I receive the email:
require_once("PHPMailer/src/Exception.php");
require_once("PHPMailer/src/PHPMailer.php");
require_once("PHPMailer/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->SMTPDebug = 2; // verbose debug output
//$mail->SMTPDebug = 0; // no output
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = $mailSender;
$mail->Password = $mailSenderPassword;
$mail->SetFrom($mailSender);
$mail->addAddress($mailTo);
$mail->isHTML(true);
$mail->Subject = "email test";
$mail->Body = "testing an email";
$mail->send();
But on my local WIMP (Windows-IIS-MySQL-PHP) PC, I always get the following error when running it:
Failed to connect to server: (0)
Note: I run php pages successfully all the time on my WIMP pc. The only thing that doesn't work locally is PHPMailer.
I tried turning my windows firewall completely off and that made no change.
How can I run this successfully on my Windows 10 IIS PC?
This was the solution:
My cafile set in php.ini had the wrong path to my cert:
openssl.cafile=wrongpath\cacert.pem
I fixed the path and everything worked.
As a stop-gap, failing everything else, the following will work:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
Taken from here:
PHPMailer: SMTP Error: Could not connect to SMTP host
It sounds like your PHP is running just fine, as is PHPMailer — but you have some network problem that means that PHP can't talk to your mail server. PHP scripts that don't talk to mail servers obviously don't have any problem with that. Have a read of the PHPMailer troubleshooting guide which has some techniques for diagnosing network problems like this, though you may need to adapt them for Windows.
Since you're sending via gmail, I recommend that you base your code on the gmail example provided too — your code lacks any debug output or error handling, so you don't have any feedback on what's going wrong.
From first view I saw these, that you have written some props witout uppercases. That should be like:
IsSMTP()
AddAddress()
IsHTML()
Send()
Also I don't remember about this block much now, but before I've implemented something like this, and it worked. If it can help you, I'll share that code here (sending confirmation email):
use PHPMailer\PHPMailer\Exception as PhpMailerException;
use PHPMailer\PHPMailer\PHPMailer;
// ...
public function sendEmail_PhpMailer($to_email, $from_email, $name, $confirm_token): array
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
// $mail->SMTPDebug = 2; // Enable verbose debug output
// $mail->isSMTP(); // Set mailer to use SMTP // https://github.com/PHPMailer/PHPMailer/issues/1209#issuecomment-338898794
$mail->Host = config('custom.phpmailer.host'); // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Username = config('custom.phpmailer.username'); // SMTP username
$mail->Password = config('custom.phpmailer.password'); // SMTP password
$mail->SMTPSecure = config('custom.phpmailer.secure'); // Enable TLS encryption, `ssl` also accepted
$mail->Port = config('custom.phpmailer.port'); // TCP port to connect to
$mail->setFrom($from_email, config('app.name'));
$mail->addAddress($to_email, $name);
// $mail->addReplyTo($from_email, config('app.name'));
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Confirm Your Registration!';
$mail->Body = "<b>Click here to confirm your " . config('app.name') . " account!</b>";
$mail->AltBody = "Confirm your account with this link: " . route('web.email_confirm', $confirm_token);
$mail->send();
return [
'error' => false,
'message' => 'Message successfully sent!',
];
}
catch (PhpMailerException $e) {
return [
'error' => true,
'message' => 'Something went wrong: ', $mail->ErrorInfo,
];
}
}
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,
]
im new in php im trying to use phpmailer to send email using gmail
here is the code i wrote
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sendermail#gmail.com';
$mail->Password = '<some password>';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('anadresse#gmail.com', 'GestionStock');
$mail->addAddress('anadresse#gmail.com', 'hamza');
$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.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} ?>
i got as results SMTP Error: Could not connect to SMTP host.
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
can someone tell me what im doing wrong !!
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 the matteobin user contribution of stackoverflow
i figured out that the problem not from code , its from the account gmail , idk exactly what is but i have different accounts , some works and other not working , i tried creating new gmail account and enabling lesssecureapps but not worked
it works only in a specific gmail with a disabled lesssecureapps
The following code has worked for me.
require('./phpmailer/PHPMailerAutoload.php');
require('./phpmailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->CharSet = "utf-8";
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPAuth=true;
$mail->isSMTP();
$mail->Username="myemail#gmail.com";
$mail->Password="mypassword";
$mail->setFrom('myemail#gmail.com','Some text here');
$mail->addAddress($email);
$mail->addReplyTo('myemail#gmail.com');
$mail->isHTML(true);
$mail->Subject="Confirmation email";
$mail->Body="<h2 style='text-align='center';'>Confirmation email</h2></br>
<p>Your message has been received. One of our team members will contact you shortly.</br></br>Thank you for contacting us.</p>";
if(!$mail->send())
{
echo "Message could not be sent!";
echo $mail->ErrorInfo;
}
else
{
echo "Message sent successfully!";
}
I was having the same problem. I added the following code:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
It is because, in the newer version, PHP has implemented stricter SSL behaviour which has caused this problem. Hope this solves your problem as well.
Im messing around with phpmailer and i've got everything working.
But what im trying to do now is after submitting a form send a mail.
Not anything too difficult just a basic email that acknowledges the form has been submitted (no form data).
problem : email is not sending after submitting form (email code is working 100% tested)
Hope someone can help me out :)
mail.php code :
<?php
//ini_set(‘display_errors’, 1);
include '/var/www/includes/mailer.php';
//require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.nicetrygoyim.nl'; //Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'secret#secret.com'; // SMTP username
$mail->Password = 'nicetry'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('secret#secret.com', 'Mailer');
$mail->addAddress('secret#secret.com', 'secret'); // Add a recipient
$mail->addAddress('secret#secret.com', 'secret'); // Name is optional
//$mail->addReplyTo('secret#secret.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';
$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->smtpConnect([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
my Form :
<form action="mail.php" method="post">
Leerlingnummer:<br>
<input type="text" name="leerlingnummer"required placeholder="Voer hier het leerlingnummer in" /><br>
E-mailadres:<br>
<input type="submit" name="submit" class="groottext" value="Reparatie indienen"/>
edit : typo
edit : forgot to mention problem
If this code is running, you should be seeing a ton of debug output, even if it is working correctly. You don't actually say what the problem is, but you're doing a few things wrong that I can see. It would really help if you based your code on the examples provided and read the docs instead of just guessing.
$mail->SMTPSecure = 'TLS';
should be:
$mail->SMTPSecure = 'tls';
Don't call smtpConnect() yourself, you'll mess up the tracking of SMTP transaction state. If you want to set SSL params, set them the expected way and then just call send(), which will deal with the connection:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
The next question is why are you doing that? If you can't provide an explicit, specific reason for doing that, you're doing something wrong.
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.