Skip to content

Commit d93be92

Browse files
author
mcramer
committed
Updated: added maximum mail count to send per smtp connection
1 parent b0191fc commit d93be92

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

interface/lib/classes/ispcmail.inc.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ispcmail {
5454
private $mime_boundary;
5555
private $body = '';
5656
private $_mail_sender = '';
57+
private $_sent_mails = 0;
5758
/**#@-*/
5859

5960
/**
@@ -95,6 +96,10 @@ class ispcmail {
9596
* If you want to use tls/ssl specify it here
9697
*/
9798
private $smtp_crypt = ''; // tls or ssl
99+
/**
100+
* How many mails should be sent via one single smtp connection
101+
*/
102+
private $smtp_max_mails = 20;
98103
/**#@-*/
99104

100105
public function __construct($options = array()) {
@@ -105,7 +110,7 @@ public function __construct($options = array()) {
105110
$this->attachments = array();
106111

107112
$this->headers['MIME-Version'] = '1.0';
108-
$this->setOptions($options);
113+
if(is_array($options) && count($options) > 0) $this->setOptions($options);
109114
}
110115

111116
public function __destruct() {
@@ -138,6 +143,10 @@ public function setOption($key, $value) {
138143
case 'smtp_pass':
139144
$this->smtp_pass = $value;
140145
break;
146+
case 'smtp_max_mails':
147+
$this->smtp_max_mails = intval($value);
148+
if($this->smtp_max_mails < 1) $this->smtp_max_mails = 1;
149+
break;
141150
case 'use_smtp':
142151
$this->use_smtp = ($value == true ? true : false);
143152
if($value == true) $this->_crlf = "\r\n";
@@ -152,8 +161,12 @@ public function setOption($key, $value) {
152161
}
153162
}
154163

164+
/** Detect the helo string if none given
165+
*
166+
*/
155167
private function detectHelo() {
156-
if(isset($_SERVER['SERVER_NAME'])) $this->smtp_helo = $_SERVER['SERVER_NAME'];
168+
if(isset($_SERVER['HTTP_HOST'])) $this->smtp_helo = $_SERVER['HTTP_HOST'];
169+
elseif(isset($_SERVER['SERVER_NAME'])) $this->smtp_helo = $_SERVER['SERVER_NAME'];
157170
else $this->smtp_helo = php_uname('n');
158171
if($this->smtp_helo == '') $this->smtp_helo = 'localhost';
159172
}
@@ -211,6 +224,9 @@ public function setSMTPEncryption($mode = '') {
211224
* @param string $value value to set in header field
212225
*/
213226
public function setHeader($header, $value) {
227+
if(strtolower($header) == 'bcc') $header = 'Bcc';
228+
elseif(strtolower($header) == 'cc') $header = 'Cc';
229+
elseif(strtolower($header) == 'from') $header = 'From';
214230
$this->headers["$header"] = $value;
215231
}
216232

@@ -224,7 +240,10 @@ public function setHeader($header, $value) {
224240
* @return string header value
225241
*/
226242
public function getHeader($header) {
227-
return $this->headers["$header"];
243+
if(strtolower($header) == 'bcc') $header = 'Bcc';
244+
elseif(strtolower($header) == 'cc') $header = 'Cc';
245+
elseif(strtolower($header) == 'from') $header = 'From';
246+
return (isset($this->headers["$header"]) ? $this->headers["$header"] : '');
228247
}
229248

230249
/**
@@ -388,7 +407,7 @@ private function create() {
388407

389408
if (isset($this->body)) {
390409
// Add message ID header
391-
$message_id = sprintf('<%s.%s@%s>', base_convert(time(), 10, 36), base_convert(rand(), 10, 36), !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);
410+
$message_id = sprintf('<%s.%s@%s>', base_convert(time(), 10, 36), base_convert(rand(), 10, 36), $this->smtp_helo != '' ? $this->smtp_helo : $this->detectHelo());
392411
$this->headers['Message-ID'] = $message_id;
393412
return true;
394413
} else {
@@ -489,7 +508,7 @@ public function send($recipients) {
489508

490509
// Get flat representation of headers
491510
foreach ($this->headers as $name => $value) {
492-
if(strtolower($name) == 'to') continue; // never add the To header
511+
if(strtolower($name) == 'to' || strtolower($name) == 'cc' || strtolower($name) == 'bcc') continue; // never add the To header
493512
$headers[] = $name . ': ' . $this->_encodeHeader($value, $this->mail_charset);
494513
}
495514

@@ -499,6 +518,15 @@ public function send($recipients) {
499518
if(!$result) return false;
500519
}
501520
foreach($recipients as $recipname => $recip) {
521+
if($this->_sent_mails >= $this->smtp_max_mails) {
522+
// close connection to smtp and reconnect
523+
$this->_sent_mails = 0;
524+
$this->_smtp_close();
525+
$result = $this->_smtp_login();
526+
if(!$result) return false;
527+
}
528+
$this->_sent_mails += 1;
529+
502530
$recipname = trim(str_replace('"', '', $recipname));
503531
$recip = $this->_encodeHeader($recip, $this->mail_charset);
504532
$recipname = $this->_encodeHeader($recipname, $this->mail_charset);
@@ -519,8 +547,10 @@ public function send($recipients) {
519547
if($recipname && !is_numeric($recipname)) $this->setHeader('To', $recipname . ' <' . $recip . '>');
520548
else $this->setHeader('To', $recip);
521549

522-
523-
$mail_content = 'To: ' . $this->getHeader('To') . $this->_crlf . 'Subject: ' . $enc_subject . $this->_crlf;
550+
$mail_content = 'To: ' . $this->getHeader('To') . $this->_crlf;
551+
if($this->getHeader('Bcc') != '') $mail_content .= 'Bcc: ' . $this->_encodeHeader($this->getHeader('Bcc'), $this->mail_charset) . $this->_crlf;
552+
if($this->getHeader('Cc') != '') $mail_content .= 'Cc: ' . $this->_encodeHeader($this->getHeader('Cc'), $this->mail_charset) . $this->_crlf;
553+
$mail_content .= 'Subject: ' . $enc_subject . $this->_crlf;
524554
$mail_content .= implode($this->_crlf, $headers) . $this->_crlf . $this->_crlf . $this->body;
525555

526556
fputs($this->_smtp_conn, $mail_content . $this->_crlf . '.' . $this->_crlf);
@@ -530,6 +560,8 @@ public function send($recipients) {
530560
$result = true;
531561
}
532562
} else {
563+
if($this->getHeader('Bcc') != '') $headers[] = 'Bcc: ' . $this->_encodeHeader($this->getHeader('Bcc'), $this->mail_charset);
564+
if($this->getHeader('Cc') != '') $headers[] = 'Cc: ' . $this->_encodeHeader($this->getHeader('Cc'), $this->mail_charset);
533565
$rec_string = '';
534566
foreach($recipients as $recipname => $recip) {
535567
$recipname = trim(str_replace('"', '', $recipname));
@@ -579,6 +611,7 @@ public function finish() {
579611
$this->use_smtp = false;
580612
$this->smtp_crypt = false;
581613
$this->mail_charset = 'UTF-8';
614+
$this->_sent_mails = 0;
582615
return;
583616
}
584617
}

0 commit comments

Comments
 (0)