PHPMailer 6.2 stream_socket_client - php

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

Related

Unable to send mail from 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,
]

SMTP Error: Could not connect to SMTP host. i tried all result found on stackoverflow and githubs

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.

How can I get PHPMailer to send an email using Gmail's SMTP?

I've installed PHPMailer onto Windows Server 2012 R2 (using IIS) and I'm trying to use it to send an email, here is my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mailer->Host = 'tls://smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemailaddress#gmail.com'; // SMTP username
$mail->Password = 'MyPassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('myemailaddress#gmail.com', 'Martin');
$mail->addAddress('anotheraddress#emaildomain.co.uk', 'Hello'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('anotheraddress#midlandairexpress.co.uk', 'Hello');
//$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
As you can see I've enabled debug output andwhen I try to run this I get:
2017-05-07 20:36:28 Connection: opening to localhost:587, timeout=300, options=array ( ) 2017-05-07 20:36:30 Connection failed. Error #2: stream_socket_client(): unable to connect to localhost:587 (No connection could be made because the target machine actively refused it. ) [C:\inetpub\wwwroot\email\PHPMailer\class.smtp.php line 292] 2017-05-07 20:36:30 SMTP ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it. (10061) 2017-05-07 20:36:30 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
I've installed PHP screenshot of info
I've tried:
1. Looking through the GitHub documentation
2. Specifying the protocol before the name of the SMTP server (line 10)
3. Checking the SMTP info against Google's Support page
4. Checking the credentials of my email address (send from)
The PHPMailer folder is located within a folder on the server called email
(Path to PHPMailerAutoload.php)
C:\inetpub\wwwroot\email\PHPMailer\PHPMailerAutoLoad.php
I'm fairly new to PHP but I have a basic understanding of how it works.
Does anyone have any suggestions?

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.

SMTP ERROR: Failed to connect to server: Connection timed out (110)

Connection: opening to smtp.mandrillapp.com:587, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Message could not be sent.Mailer Error: SMTP connect() failed.
I used mandrillAPI to send email. It works on my localhost. I can send mails from my localhost. But when I used this on the test server it is not connecting to the mandrill server. It shows the above error. I checked the port and it is open. I can use gmail smtp from my server. It works fine. But I need to use mandrillAPI. I also added DKIM and SPF record on domain. But still not connecting to the smtp.mandrillapi.com
here is the code I used.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPDebug = 4;
$mail->Debugoutput='html';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'apikey'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'example#gmail.com';
$mail->FromName = 'example';
$mail->AddAddress('example#gmail.com', 'Nidheesh'); // Add a recipient
// Name is optional
$mail->addReplyTo('example#gmail.com');
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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;
exit;
}
echo 'Message has been sent';
?>
`

Categories