0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-20 01:52:15 +02:00
PHPMailer/examples/ssl_options.phps

82 lines
2.3 KiB
Plaintext
Raw Normal View History

2015-04-27 21:18:12 +02:00
<?php
2015-04-27 21:18:12 +02:00
/**
* This example shows settings to use when sending over SMTP with TLS and custom connection options.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
2019-09-27 18:26:00 +02:00
use PHPMailer\PHPMailer\SMTP;
2015-04-27 21:18:12 +02:00
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
2015-11-09 19:09:13 +01:00
require '../vendor/autoload.php';
2015-04-27 21:18:12 +02:00
//Create a new PHPMailer instance
$mail = new PHPMailer();
2015-04-27 21:18:12 +02:00
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
2021-02-19 13:42:01 +01:00
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
2015-04-27 21:18:12 +02:00
//Set the hostname of the mail server
$mail->Host = 'smtp.example.com';
//Set the SMTP port number:
// - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or
// - 587 for SMTP+STARTTLS
$mail->Port = 465;
//Set the encryption mechanism to use:
// - SMTPS (implicit TLS on port 465) or
// - STARTTLS (explicit TLS on port 587)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
2015-04-27 21:18:12 +02:00
//Custom connection options
2016-04-19 17:08:56 +02:00
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
2015-11-10 13:45:13 +01:00
'ssl' => [
'verify_peer' => true,
2015-04-27 21:18:12 +02:00
'verify_depth' => 3,
'allow_self_signed' => true,
'peer_name' => 'smtp.example.com',
'cafile' => '/etc/ssl/ca_cert.pem',
],
2015-04-27 21:18:12 +02:00
);
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
2016-04-19 17:08:56 +02:00
$mail->Username = 'username@example.com';
2015-04-27 21:18:12 +02:00
//Password to use for SMTP authentication
2016-04-19 17:08:56 +02:00
$mail->Password = 'yourpassword';
2015-04-27 21:18:12 +02:00
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP options test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
2017-09-29 16:07:42 +02:00
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
2015-04-27 21:18:12 +02:00
2016-04-19 17:08:56 +02:00
//Send the message, check for errors
2015-04-27 21:18:12 +02:00
if (!$mail->send()) {
2016-04-19 17:08:56 +02:00
echo 'Mailer Error: ' . $mail->ErrorInfo;
2015-04-27 21:18:12 +02:00
} else {
2016-04-19 17:08:56 +02:00
echo 'Message sent!';
2015-04-27 21:18:12 +02:00
}