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

74 lines
2.0 KiB
Plaintext
Raw Normal View History

2015-04-27 21:18:12 +02:00
<?php
/**
* This example shows settings to use when sending over SMTP with TLS and custom connection options.
*/
2015-11-09 19:09:13 +01:00
namespace PHPMailer\PHPMailer;
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;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = 'smtp.example.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Custom connection options
2016-04-19 17:08:56 +02:00
//Note that these settings are INSECURE
2015-04-27 21:18:12 +02:00
$mail->SMTPOptions = array (
2015-11-10 13:45:13 +01:00
'ssl' => [
2015-04-27 21:18:12 +02:00
'verify_peer' => true,
'verify_depth' => 3,
'allow_self_signed' => true,
'peer_name' => 'smtp.example.com',
'cafile' => '/etc/ssl/ca_cert.pem',
2015-11-10 13:45:13 +01:00
]
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
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
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
}