Skip to content

Commit ab36b53

Browse files
committed
Expose stream context options
1 parent 9bb6a28 commit ab36b53

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Provide detailed errors when individual recipients fail
2929
* Report more errors when connecting
3030
* Add extras classes to composer classmap
31+
* Expose stream_context_create options via new SMTPOptions property
3132

3233
## Version 5.2.9 (Sept 25th 2014)
3334
* **Important: The autoloader is no longer autoloaded by the PHPMailer class**

class.phpmailer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ class PHPMailer
255255
*/
256256
public $SMTPAuth = false;
257257

258+
/**
259+
* Options array passed to stream_context_create when connecting via SMTP.
260+
* @type array
261+
*/
262+
public $SMTPOptions = array();
263+
258264
/**
259265
* SMTP username.
260266
* @type string
@@ -1220,7 +1226,7 @@ public function getSMTPInstance()
12201226
protected function smtpSend($header, $body)
12211227
{
12221228
$bad_rcpt = array();
1223-
if (!$this->smtpConnect()) {
1229+
if (!$this->smtpConnect($this->SMTPOptions)) {
12241230
throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
12251231
}
12261232
if ('' == $this->Sender) {

class.smtp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function connect($host, $port = null, $timeout = 30, $options = array())
265265
}
266266
// Connect to the SMTP server
267267
$this->edebug(
268-
"Connection: opening to $host:$port, t=$timeout, opt=".var_export($options, true),
268+
"Connection: opening to $host:$port, timeout=$timeout, options=".var_export($options, true),
269269
self::DEBUG_CONNECTION
270270
);
271271
$errno = 0;

examples/ssl_options.phps

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* This example shows settings to use when sending over SMTP with TLS and custom connection options.
4+
*/
5+
6+
//SMTP needs accurate times, and the PHP time zone MUST be set
7+
//This should be done in your php.ini, but this is how to do it if you don't have access to that
8+
date_default_timezone_set('Etc/UTC');
9+
10+
require '../PHPMailerAutoload.php';
11+
12+
//Create a new PHPMailer instance
13+
$mail = new PHPMailer;
14+
15+
//Tell PHPMailer to use SMTP
16+
$mail->isSMTP();
17+
18+
//Enable SMTP debugging
19+
// 0 = off (for production use)
20+
// 1 = client messages
21+
// 2 = client and server messages
22+
$mail->SMTPDebug = 2;
23+
24+
//Ask for HTML-friendly debug output
25+
$mail->Debugoutput = 'html';
26+
27+
//Set the hostname of the mail server
28+
$mail->Host = 'smtp.example.com';
29+
30+
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
31+
$mail->Port = 587;
32+
33+
//Set the encryption system to use - ssl (deprecated) or tls
34+
$mail->SMTPSecure = 'tls';
35+
36+
//Custom connection options
37+
$mail->SMTPOptions = array (
38+
'ssl' => array(
39+
'verify_peer' => true,
40+
'verify_depth' => 3,
41+
'allow_self_signed' => true,
42+
'peer_name' => 'smtp.example.com',
43+
'cafile' => '/etc/ssl/ca_cert.pem',
44+
)
45+
);
46+
47+
//Whether to use SMTP authentication
48+
$mail->SMTPAuth = true;
49+
50+
//Username to use for SMTP authentication - use full email address for gmail
51+
$mail->Username = "[email protected]";
52+
53+
//Password to use for SMTP authentication
54+
$mail->Password = "yourpassword";
55+
56+
//Set who the message is to be sent from
57+
$mail->setFrom('[email protected]', 'First Last');
58+
59+
//Set who the message is to be sent to
60+
$mail->addAddress('[email protected]', 'John Doe');
61+
62+
//Set the subject line
63+
$mail->Subject = 'PHPMailer SMTP options test';
64+
65+
//Read an HTML message body from an external file, convert referenced images to embedded,
66+
//convert HTML into a basic plain-text alternative body
67+
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
68+
69+
//send the message, check for errors
70+
if (!$mail->send()) {
71+
echo "Mailer Error: " . $mail->ErrorInfo;
72+
} else {
73+
echo "Message sent!";
74+
}

0 commit comments

Comments
 (0)