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
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
I have the same problem i tried with SSL with port 465 then it said services is unavailable then i try this thing it solve my problem hopefully it solve also yours 1) Make sure you use $mail->IsSMTP();
for the authentication. Ismail is only working for the same server with mail function
2) This thing fix my problem
$mail->SMTPSecure = "tsl";
$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
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 = 'your_email_here@gmail.com';
$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('recipient_address@gmail.com', 'Recipient Name');
// Name is optional
//$mail->addAddress('another_email@example.com');
$mail->addReplyTo('info@your_domain.com', 'Information Team');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//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;
}
?>
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
)
);