Merge remote-tracking branch 'remotes/upstream/master' into autotls

This commit is contained in:
Synchro 2015-04-29 13:16:43 +02:00
commit 1befef0970
7 changed files with 281 additions and 12 deletions

View File

@ -1,5 +1,7 @@
# ChangeLog
## Version 5.2.10 (April 29th 2015)
* Add custom header getter
* Use `application/javascript` for .js attachments
* Improve RFC2821 compliance for timelimits, especially for end-of-data
* Add Azerbaijani translations (Thanks to @mirjalal)
@ -27,6 +29,9 @@
* Enable TLS encryption automatically if the server offers it
* Provide detailed errors when individual recipients fail
* Report more errors when connecting
* Add extras classes to composer classmap
* Expose stream_context_create options via new SMTPOptions property
* Automatic encoding switch to quoted-printable if message lines are too long
## Version 5.2.9 (Sept 25th 2014)
* **Important: The autoloader is no longer autoloaded by the PHPMailer class**

View File

@ -263,6 +263,12 @@ class PHPMailer
*/
public $SMTPAuth = false;
/**
* Options array passed to stream_context_create when connecting via SMTP.
* @type array
*/
public $SMTPOptions = array();
/**
* SMTP username.
* @type string
@ -566,6 +572,13 @@ class PHPMailer
*/
protected $exceptions = false;
/**
* Unique ID used for message ID and boundaries.
* @type string
* @access protected
*/
protected $uniqueid = '';
/**
* Error severity: message only, continue processing.
*/
@ -586,6 +599,12 @@ class PHPMailer
*/
const CRLF = "\r\n";
/**
* The maximum line length allowed by RFC 2822 section 2.1.1
* @type integer
*/
const MAX_LINE_LENGTH = 998;
/**
* Constructor.
* @param boolean $exceptions Should we throw external exceptions?
@ -1018,8 +1037,9 @@ class PHPMailer
throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
}
$this->MIMEHeader = $this->createHeader();
//Create body before headers in case body makes changes to headers (e.g. altering transfer encoding)
$this->MIMEBody = $this->createBody();
$this->MIMEHeader = $this->createHeader();
// To capture the complete message when using mail(), create
// an extra header list which createHeader() doesn't fold in
@ -1228,7 +1248,7 @@ class PHPMailer
protected function smtpSend($header, $body)
{
$bad_rcpt = array();
if (!$this->smtpConnect()) {
if (!$this->smtpConnect($this->SMTPOptions)) {
throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
}
if ('' == $this->Sender) {
@ -1693,12 +1713,6 @@ class PHPMailer
{
$result = '';
// Set the boundaries
$uniq_id = md5(uniqid(time()));
$this->boundary[1] = 'b1_' . $uniq_id;
$this->boundary[2] = 'b2_' . $uniq_id;
$this->boundary[3] = 'b3_' . $uniq_id;
if ($this->MessageDate == '') {
$this->MessageDate = self::rfcDate();
}
@ -1750,7 +1764,7 @@ class PHPMailer
if ($this->MessageID != '') {
$this->lastMessageID = $this->MessageID;
} else {
$this->lastMessageID = sprintf('<%s@%s>', $uniq_id, $this->ServerHostname());
$this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->ServerHostname());
}
$result .= $this->headerLine('Message-ID', $this->lastMessageID);
$result .= $this->headerLine('X-Priority', $this->Priority);
@ -1860,6 +1874,11 @@ class PHPMailer
public function createBody()
{
$body = '';
//Create unique IDs and preset boundaries
$this->uniqueid = md5(uniqid(time()));
$this->boundary[1] = 'b1_' . $this->uniqueid;
$this->boundary[2] = 'b2_' . $this->uniqueid;
$this->boundary[3] = 'b3_' . $this->uniqueid;
if ($this->sign_key_file) {
$body .= $this->getMailMIME() . $this->LE;
@ -1869,16 +1888,30 @@ class PHPMailer
$bodyEncoding = $this->Encoding;
$bodyCharSet = $this->CharSet;
//Can we do a 7-bit downgrade?
if ($bodyEncoding == '8bit' and !$this->has8bitChars($this->Body)) {
$bodyEncoding = '7bit';
$bodyCharSet = 'us-ascii';
}
//If lines are too long, change to quoted-printable transfer encoding
if (self::hasLineLongerThanMax($this->Body)) {
$this->Encoding = 'quoted-printable';
$bodyEncoding = 'quoted-printable';
$bodyCharSet = 'us-ascii'; //qp always fits into ascii
}
$altBodyEncoding = $this->Encoding;
$altBodyCharSet = $this->CharSet;
//Can we do a 7-bit downgrade?
if ($altBodyEncoding == '8bit' and !$this->has8bitChars($this->AltBody)) {
$altBodyEncoding = '7bit';
$altBodyCharSet = 'us-ascii';
}
//If lines are too long, change to quoted-printable transfer encoding
if (self::hasLineLongerThanMax($this->AltBody)) {
$altBodyEncoding = 'quoted-printable';
$altBodyCharSet = 'us-ascii';
}
//Use this as a preamble in all multipart message types
$mimepre = "This is a multi-part message in MIME format." . $this->LE . $this->LE;
switch ($this->message_type) {
@ -2968,6 +3001,16 @@ class PHPMailer
}
}
/**
* Returns all custom headers
*
* @return array
*/
public function getCustomHeaders()
{
return $this->CustomHeader;
}
/**
* Create a message from an HTML string.
* Automatically makes modifications for inline images and backgrounds
@ -3471,6 +3514,18 @@ class PHPMailer
return $dkimhdrs . $signed . "\r\n";
}
/**
* Detect if a string contains a line longer than the maximum line length allowed.
* @param string $str
* @return boolean
* @static
*/
public static function hasLineLongerThanMax($str)
{
//+2 to include CRLF line break for a 1000 total
return (boolean)preg_match('/^(.{'.(self::MAX_LINE_LENGTH + 2).',})/m', $str);
}
/**
* Allows for public read access to 'to' property.
* @access public

View File

@ -265,7 +265,7 @@ class SMTP
}
// Connect to the SMTP server
$this->edebug(
"Connection: opening to $host:$port, t=$timeout, opt=".var_export($options, true),
"Connection: opening to $host:$port, timeout=$timeout, options=".var_export($options, true),
self::DEBUG_CONNECTION
);
$errno = 0;

View File

@ -27,7 +27,13 @@
"phpunit/phpunit": "4.3.*"
},
"autoload": {
"classmap": ["class.phpmailer.php", "class.pop3.php", "class.smtp.php"]
"classmap": [
"class.phpmailer.php",
"class.smtp.php",
"class.pop3.php",
"extras/EasyPeasyICS.php",
"extras/ntlm_sasl_client.php"
]
},
"license": "LGPL-2.1"
}

74
examples/ssl_options.phps Normal file
View File

@ -0,0 +1,74 @@
<?php
/**
* This example shows settings to use when sending over SMTP with TLS and custom connection options.
*/
//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');
require '../PHPMailerAutoload.php';
//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;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//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
$mail->SMTPOptions = array (
'ssl' => array(
'verify_peer' => true,
'verify_depth' => 3,
'allow_self_signed' => true,
'peer_name' => 'smtp.example.com',
'cafile' => '/etc/ssl/ca_cert.pem',
)
);
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//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__));
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

View File

@ -0,0 +1,26 @@
<?php
/**
* Korean PHPMailer language file: refer to English translation for definitive list
* @package PHPMailer
* @author ChalkPE <amato0617@gmail.com>
*/
$PHPMAILER_LANG['authenticate'] = 'SMTP 오류: 인증할 수 없습니다.';
$PHPMAILER_LANG['connect_host'] = 'SMTP 오류: SMTP 호스트에 접속할 수 없습니다.';
$PHPMAILER_LANG['data_not_accepted'] = 'SMTP 오류: 데이터가 받아들여지지 않았습니다.';
$PHPMAILER_LANG['empty_message'] = '메세지 내용이 없습니다';
$PHPMAILER_LANG['encoding'] = '알 수 없는 인코딩: ';
$PHPMAILER_LANG['execute'] = '실행 불가: ';
$PHPMAILER_LANG['file_access'] = '파일 접근 불가: ';
$PHPMAILER_LANG['file_open'] = '파일 오류: 파일을 열 수 없습니다: ';
$PHPMAILER_LANG['from_failed'] = '다음 From 주소에서 오류가 발생했습니다: ';
$PHPMAILER_LANG['instantiate'] = 'mail 함수를 인스턴스화할 수 없습니다';
$PHPMAILER_LANG['invalid_address'] = '잘못된 주소';
$PHPMAILER_LANG['mailer_not_supported'] = ' 메일러는 지원되지 않습니다.';
$PHPMAILER_LANG['provide_address'] = '적어도 한 개 이상의 수신자 메일 주소를 제공해야 합니다.';
$PHPMAILER_LANG['recipients_failed'] = 'SMTP 오류: 다음 수신자에서 오류가 발생했습니다: ';
$PHPMAILER_LANG['signing'] = '서명 오류: ';
$PHPMAILER_LANG['smtp_connect_failed'] = 'SMTP 연결을 실패하였습니다.';
$PHPMAILER_LANG['smtp_error'] = 'SMTP 서버 오류: ';
$PHPMAILER_LANG['variable_set'] = '변수 설정 및 초기화 불가: ';
$PHPMAILER_LANG['extension_missing'] = '확장자 없음: ';

View File

@ -946,7 +946,7 @@ EOT;
$this->Mail->Subject .= ': HTML + Attachment';
$this->Mail->isHTML(true);
if (!$this->Mail->addAttachment(__FILE__, 'test_attach.txt')) {
if (!$this->Mail->addAttachment('../examples/images/phpmailer_mini.png', 'phpmailer_mini.png')) {
$this->assertTrue(false, $this->Mail->ErrorInfo);
return;
}
@ -1212,6 +1212,57 @@ EOT;
$this->assertFalse($this->Mail->send(), $this->Mail->ErrorInfo);
}
/**
* Test constructing a message that contains lines that are too long for RFC compliance.
*/
public function testLongBody()
{
$oklen = str_repeat(str_repeat('0', PHPMailer::MAX_LINE_LENGTH) . PHPMailer::CRLF, 10);
$badlen = str_repeat(str_repeat('1', PHPMailer::MAX_LINE_LENGTH + 1) . PHPMailer::CRLF, 2);
$this->Mail->Body = "This message contains lines that are too long.".
PHPMailer::CRLF . PHPMailer::CRLF . $oklen . $badlen . $oklen;
$this->assertTrue(
PHPMailer::hasLineLongerThanMax($this->Mail->Body),
'Test content does not contain long lines!'
);
$this->buildBody();
$this->Mail->Encoding = '8bit';
$this->Mail->preSend();
$message = $this->Mail->getSentMIMEMessage();
$this->assertFalse(PHPMailer::hasLineLongerThanMax($message), 'Long line not corrected.');
$this->assertContains(
'Content-Transfer-Encoding: quoted-printable',
$message,
'Long line did not cause transfer encoding switch.'
);
}
/**
* Test constructing a message that does NOT contain lines that are too long for RFC compliance.
*/
public function testShortBody()
{
$oklen = str_repeat(str_repeat('0', PHPMailer::MAX_LINE_LENGTH) . PHPMailer::CRLF, 10);
$this->Mail->Body = "This message does not contain lines that are too long.".
PHPMailer::CRLF . PHPMailer::CRLF . $oklen;
$this->assertFalse(
PHPMailer::hasLineLongerThanMax($this->Mail->Body),
'Test content contains long lines!'
);
$this->buildBody();
$this->Mail->Encoding = '8bit';
$this->Mail->preSend();
$message = $this->Mail->getSentMIMEMessage();
$this->assertFalse(PHPMailer::hasLineLongerThanMax($message), 'Long line not corrected.');
$this->assertNotContains(
'Content-Transfer-Encoding: quoted-printable',
$message,
'Short line caused transfer encoding switch.'
);
}
/**
* Test keepalive (sending multiple messages in a single connection).
*/
@ -1557,6 +1608,31 @@ EOT;
$this->assertEquals($target, PHPMailer::normalizeBreaks($mixedsrc), 'Mixed break reformatting failed');
}
/**
* Test line length detection
*/
public function testLineLength()
{
$oklen = str_repeat(str_repeat('0', PHPMailer::MAX_LINE_LENGTH)."\r\n", 10);
$badlen = str_repeat(str_repeat('1', PHPMailer::MAX_LINE_LENGTH + 1) . "\r\n", 2);
$this->assertTrue(PHPMailer::hasLineLongerThanMax($badlen), 'Long line not detected (only)');
$this->assertTrue(PHPMailer::hasLineLongerThanMax($oklen . $badlen), 'Long line not detected (first)');
$this->assertTrue(PHPMailer::hasLineLongerThanMax($badlen . $oklen), 'Long line not detected (last)');
$this->assertTrue(
PHPMailer::hasLineLongerThanMax($oklen . $badlen . $oklen),
'Long line not detected (middle)'
);
$this->assertFalse(PHPMailer::hasLineLongerThanMax($oklen), 'Long line false positive');
$this->Mail->isHTML(false);
$this->Mail->Subject .= ": Line length test";
$this->Mail->CharSet = 'UTF-8';
$this->Mail->Encoding = '8bit';
$this->Mail->Body = $oklen . $badlen . $oklen . $badlen;
$this->buildBody();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
$this->assertEquals('quoted-printable', $this->Mail->Encoding, 'Long line did not override transfer encoding');
}
/**
* Test setting and retrieving message ID.
*/
@ -1680,6 +1756,33 @@ EOT;
'SMTP connect with options failed'
);
}
/**
* Tests the Custom header getter
*/
public function testCustomHeaderGetter()
{
$this->Mail->addCustomHeader('foo', 'bar');
$this->assertEquals(array(array('foo', 'bar')), $this->Mail->getCustomHeaders());
$this->Mail->addCustomHeader('foo', 'baz');
$this->assertEquals(array(
array('foo', 'bar'),
array('foo', 'baz')
), $this->Mail->getCustomHeaders());
$this->Mail->clearCustomHeaders();
$this->assertEmpty($this->Mail->getCustomHeaders());
$this->Mail->addCustomHeader('yux');
$this->assertEquals(array(array('yux')), $this->Mail->getCustomHeaders());
$this->Mail->addCustomHeader('Content-Type: application/json');
$this->assertEquals(array(
array('yux'),
array('Content-Type', ' application/json')
), $this->Mail->getCustomHeaders());
}
}
/**