Commit 89e31e52 authored by Marius Burkard's avatar Marius Burkard

- improved mail sending library, contributed by Timme Hosting

parent 9145f9d3
<?php
// for testing, put this file in interface/web/tools
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
//* Check permissions for module
$app->auth->check_module_permissions('admin');
$app->uses('getconf,ispcmail');
$mail_config = $app->getconf->get_global_config('mail');
if($mail_config['smtp_enabled'] == 'y') {
$mail_config['use_smtp'] = true;
$app->ispcmail->setOptions($mail_config);
}
$to = 't.heller@timmehosting.de';
$subject = 'Test von ISPConfig-Mailqueue';
$text = '123'."\n\n".date(DATE_RFC822)."\n\n".'SMTP: '.$mail_config['use_smtp'];
$from = 'ispconfig@thomas.timmeserver.de';
$filepath = '';
$filetype = 'application/pdf';
$filename = '';
$cc = '';
$bcc = '';
$from_name = 'ISPConfig';
$app->ispcmail->setSender($from, $from_name);
$app->ispcmail->setSubject($subject);
$app->ispcmail->setMailText($text);
if($filepath != '') {
if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath);
$app->ispcmail->readAttachFile($filepath);
}
if($cc != '') $app->ispcmail->setHeader('Cc', $cc);
if($bcc != '') $app->ispcmail->setHeader('Bcc', $bcc);
$app->ispcmail->send($to);
$app->ispcmail->finish();
echo $text;
......@@ -18,4 +18,4 @@ ALTER TABLE `dns_rr` CHANGE `data` `data` TEXT NOT NULL;
ALTER TABLE `web_database` CHANGE `database_quota` `database_quota` INT(11) NULL DEFAULT NULL;
ALTER TABLE `web_domain` ADD `log_retention` INT NOT NULL DEFAULT '30' ;
ALTER TABLE spamfilter_policy CHANGE spam_tag_level spam_tag_level DECIMAL(5,2) NULL DEFAULT NULL, CHANGE spam_tag2_level spam_tag2_level DECIMAL(5,2) NULL DEFAULT NULL, CHANGE spam_kill_level spam_kill_level DECIMAL(5,2) NULL DEFAULT NULL, CHANGE spam_dsn_cutoff_level spam_dsn_cutoff_level DECIMAL(5,2) NULL DEFAULT NULL, CHANGE spam_quarantine_cutoff_level spam_quarantine_cutoff_level DECIMAL(5,2) NULL DEFAULT NULL;
UPDATE `web_database` as d LEFT JOIN `web_domain` as w ON (w.domain_id = d.parent_domain_id) SET d.parent_domain_id = 0 WHERE w.domain_id IS NULL AND d.parent_domain_id != 0 AND (SELECT EXISTS(SELECT * FROM web_domain));
\ No newline at end of file
UPDATE `web_database` as d LEFT JOIN `web_domain` as w ON (w.domain_id = d.parent_domain_id) SET d.parent_domain_id = 0 WHERE w.domain_id IS NULL AND d.parent_domain_id != 0 AND (SELECT EXISTS(SELECT * FROM web_domain));
......@@ -151,3 +151,12 @@ CREATE TABLE IF NOT EXISTS `addons` (
PRIMARY KEY (`addon_id`),
UNIQUE KEY `ident` (`addon_ident`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `sys_mailqueue` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`from` varchar(255) NOT NULL DEFAULT '',
`recipients` text NOT NULL,
`mail_content` mediumblob NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
......@@ -1585,6 +1585,21 @@ CREATE TABLE `sys_log` (
-- --------------------------------------------------------
--
-- Table structure for table `sys_mailqueue`
--
CREATE TABLE IF NOT EXISTS `sys_mailqueue` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`from` varchar(255) NOT NULL DEFAULT '',
`recipients` text NOT NULL,
`mail_content` mediumblob NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `sys_remoteaction`
--
......
......@@ -744,32 +744,22 @@ class ispcmail {
}
if($this->use_smtp == true) {
if(!$this->_logged_in || !$this->_smtp_conn) {
$result = $this->_smtp_login();
if(!$result) return false;
}
$bcc_cc_sent = false;
foreach($recipients as $recipname => $recip) {
if($this->_sent_mails >= $this->smtp_max_mails) {
// close connection to smtp and reconnect
$this->_sent_mails = 0;
$this->_smtp_close();
$result = $this->_smtp_login();
if(!$result) return false;
}
$this->_sent_mails += 1;
// Build mail headers, content etc.
$m_recipients = array();
$m_mail_content = '';
$recipname = trim(str_replace('"', '', $recipname));
$recip = $this->_encodeHeader($recip, $this->mail_charset);
$recipname = $this->_encodeHeader($recipname, $this->mail_charset);
//Email From
fputs($this->_smtp_conn, 'MAIL FROM: <' . $this->_mail_sender . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_from = $this->_mail_sender;
//Email To
fputs($this->_smtp_conn, 'RCPT TO: <' . $recip . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_recipients[] = $recip;
if($bcc_cc_sent == false) {
$add_recips = array();
......@@ -777,17 +767,12 @@ class ispcmail {
if($this->getHeader('Bcc') != '') $add_recips = array_merge($add_recips, $this->_extract_names($this->getHeader('Bcc')));
foreach($add_recips as $add_recip) {
if(!$add_recip['mail']) continue;
fputs($this->_smtp_conn, 'RCPT TO: <' . $this->_encodeHeader($add_recip['mail'], $this->mail_charset) . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_recipients[] = $this->_encodeHeader($add_recip['mail'], $this->mail_charset);
}
unset($add_recips);
$bcc_cc_sent = true;
}
//The Email
fputs($this->_smtp_conn, 'DATA' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
//Construct Headers
if($recipname && !is_numeric($recipname)) $this->setHeader('To', $recipname . ' <' . $recip . '>');
else $this->setHeader('To', $recip);
......@@ -797,10 +782,32 @@ class ispcmail {
if($this->getHeader('Cc') != '') $mail_content .= 'Cc: ' . $this->_encodeHeader($this->getHeader('Cc'), $this->mail_charset) . $this->_crlf;
$mail_content .= implode($this->_crlf, $headers) . $this->_crlf . ($this->_is_signed == false ? $this->_crlf : '') . $this->body;
fputs($this->_smtp_conn, $mail_content . $this->_crlf . '.' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_mail_content = $mail_content;
// Attempt SMTP login:
if(!$this->_logged_in || !$this->_smtp_conn) {
$result = $this->_smtp_login();
}
if($this->_sent_mails >= $this->smtp_max_mails) {
// close connection to smtp and reconnect
$this->_sent_mails = 0;
$this->_smtp_close();
$result = $this->_smtp_login();
}
$this->_sent_mails += 1;
// Send mail or queue it
if ($result) {
$this->send_smtp($m_from, $m_recipients, $m_mail_content);
} else {
$this->add_to_queue($m_from, $m_recipients, $m_mail_content);
}
// hopefully message was correctly sent or queued now
// hopefully message was correctly sent now
$result = true;
}
} else {
......@@ -828,7 +835,87 @@ class ispcmail {
return $result;
}
/**
* Send all mails in queue (usually called from cron)
*/
public function send_queue() {
global $app;
$mails = $app->db->queryAllRecords('SELECT * FROM sys_mailqueue');
if (is_array($mails)) {
foreach ($mails as $mail) {
// Open SMTP connections if not open:
if(!$this->_logged_in || !$this->_smtp_conn) {
$result = $this->_smtp_login();
}
if($this->_sent_mails >= $this->smtp_max_mails) {
// close connection to smtp and reconnect
$this->_sent_mails = 0;
$this->_smtp_close();
$result = $this->_smtp_login();
}
$this->_sent_mails += 1;
if (!$result) {
return false;
}
// Send mail:
$id = $mail['id'];
$from = $mail['from'];
$recipients = explode("\n", $mail['recipients']);
$mail_content = $mail['mail_content'];
$this->send_smtp($from, $recipients, $mail_content);
// Delete from queue:
$app->db->query('DELETE FROM sys_mailqueue WHERE id = ?', $id);
}
}
}
/**
* Send mail via SMTP
*/
private function send_smtp($from, $recipients, $mail_content) {
// from:
fputs($this->_smtp_conn, 'MAIL FROM: <' . $from . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
// recipients (To, Cc or Bcc):
foreach ($recipients as $recipient) {
fputs($this->_smtp_conn, 'RCPT TO: <' . $recipient . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
}
// mail content:
fputs($this->_smtp_conn, 'DATA' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
fputs($this->_smtp_conn, $mail_content . $this->_crlf . '.' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
// hopefully message was correctly sent now
$result = true;
return $result;
}
/**
* Add mail to queue for sending later
*/
private function add_to_queue($from, $recipients, $mail_content) {
global $app;
$app->db->query('INSERT INTO `sys_mailqueue` (`from`, `recipients`, `mail_content`) VALUES (?,?,?)', $from, implode("\n", $recipients), $mail_content);
}
/**
* Close mail connections
......
<?php
/*
Copyright (c) 2013, Marius Cramer, pixcept KG
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class cronjob_send_sys_mailqueue extends cronjob {
// job schedule
protected $_schedule = '* * * * *';
protected $_run_at_new = true;
/* this function is optional if it contains no custom code */
public function onPrepare() {
global $app;
parent::onPrepare();
}
/* this function is optional if it contains no custom code */
public function onBeforeRun() {
global $app;
return parent::onBeforeRun();
}
public function onRunJob() {
global $app, $conf;
$app->uses('getconf,ispcmail');
$mail_config = $app->getconf->get_global_config('mail');
if($mail_config['smtp_enabled'] == 'y') {
$mail_config['use_smtp'] = true;
$app->ispcmail->setOptions($mail_config);
// Mail queue is supported only with SMTP...
$app->ispcmail->send_queue();
}
parent::onRunJob();
}
/* this function is optional if it contains no custom code */
public function onAfterRun() {
global $app;
parent::onAfterRun();
}
}
?>
......@@ -740,32 +740,22 @@ class ispcmail {
}
if($this->use_smtp == true) {
if(!$this->_logged_in || !$this->_smtp_conn) {
$result = $this->_smtp_login();
if(!$result) return false;
}
$bcc_cc_sent = false;
foreach($recipients as $recipname => $recip) {
if($this->_sent_mails >= $this->smtp_max_mails) {
// close connection to smtp and reconnect
$this->_sent_mails = 0;
$this->_smtp_close();
$result = $this->_smtp_login();
if(!$result) return false;
}
$this->_sent_mails += 1;
// Build mail headers, content etc.
$m_recipients = array();
$m_mail_content = '';
$recipname = trim(str_replace('"', '', $recipname));
$recip = $this->_encodeHeader($recip, $this->mail_charset);
$recipname = $this->_encodeHeader($recipname, $this->mail_charset);
//Email From
fputs($this->_smtp_conn, 'MAIL FROM: <' . $this->_mail_sender . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_from = $this->_mail_sender;
//Email To
fputs($this->_smtp_conn, 'RCPT TO: <' . $recip . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_recipients[] = $recip;
if($bcc_cc_sent == false) {
$add_recips = array();
......@@ -773,17 +763,12 @@ class ispcmail {
if($this->getHeader('Bcc') != '') $add_recips = array_merge($add_recips, $this->_extract_names($this->getHeader('Bcc')));
foreach($add_recips as $add_recip) {
if(!$add_recip['mail']) continue;
fputs($this->_smtp_conn, 'RCPT TO: <' . $this->_encodeHeader($add_recip['mail'], $this->mail_charset) . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_recipients[] = $this->_encodeHeader($add_recip['mail'], $this->mail_charset);
}
unset($add_recips);
$bcc_cc_sent = true;
}
//The Email
fputs($this->_smtp_conn, 'DATA' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
//Construct Headers
if($recipname && !is_numeric($recipname)) $this->setHeader('To', $recipname . ' <' . $recip . '>');
else $this->setHeader('To', $recip);
......@@ -793,10 +778,32 @@ class ispcmail {
if($this->getHeader('Cc') != '') $mail_content .= 'Cc: ' . $this->_encodeHeader($this->getHeader('Cc'), $this->mail_charset) . $this->_crlf;
$mail_content .= implode($this->_crlf, $headers) . $this->_crlf . ($this->_is_signed == false ? $this->_crlf : '') . $this->body;
fputs($this->_smtp_conn, $mail_content . $this->_crlf . '.' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
$m_mail_content = $mail_content;
// Attempt SMTP login:
if(!$this->_logged_in || !$this->_smtp_conn) {
$result = $this->_smtp_login();
}
if($this->_sent_mails >= $this->smtp_max_mails) {
// close connection to smtp and reconnect
$this->_sent_mails = 0;
$this->_smtp_close();
$result = $this->_smtp_login();
}
$this->_sent_mails += 1;
// Send mail or queue it
if ($result) {
$this->send_smtp($m_from, $m_recipients, $m_mail_content);
} else {
$this->add_to_queue($m_from, $m_recipients, $m_mail_content);
}
// hopefully message was correctly sent or queued now
// hopefully message was correctly sent now
$result = true;
}
} else {
......@@ -824,7 +831,87 @@ class ispcmail {
return $result;
}
/**
* Send all mails in queue (usually called from cron)
*/
public function send_queue() {
global $app;
$mails = $app->db->queryAllRecords('SELECT * FROM sys_mailqueue');
if (is_array($mails)) {
foreach ($mails as $mail) {
// Open SMTP connections if not open:
if(!$this->_logged_in || !$this->_smtp_conn) {
$result = $this->_smtp_login();
}
if($this->_sent_mails >= $this->smtp_max_mails) {
// close connection to smtp and reconnect
$this->_sent_mails = 0;
$this->_smtp_close();
$result = $this->_smtp_login();
}
$this->_sent_mails += 1;
if (!$result) {
return false;
}
// Send mail:
$id = $mail['id'];
$from = $mail['from'];
$recipients = explode("\n", $mail['recipients']);
$mail_content = $mail['mail_content'];
$this->send_smtp($from, $recipients, $mail_content);
// Delete from queue:
$app->db->query('DELETE FROM sys_mailqueue WHERE id = ?', $id);
}
}
}
/**
* Send mail via SMTP
*/
private function send_smtp($from, $recipients, $mail_content) {
// from:
fputs($this->_smtp_conn, 'MAIL FROM: <' . $from . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
// recipients (To, Cc or Bcc):
foreach ($recipients as $recipient) {
fputs($this->_smtp_conn, 'RCPT TO: <' . $recipient . '>' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
}
// mail content:
fputs($this->_smtp_conn, 'DATA' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
fputs($this->_smtp_conn, $mail_content . $this->_crlf . '.' . $this->_crlf);
$response = fgets($this->_smtp_conn, 515);
// hopefully message was correctly sent now
$result = true;
return $result;
}
/**
* Add mail to queue for sending later
*/
private function add_to_queue($from, $recipients, $mail_content) {
global $app;
$app->db->query('INSERT INTO `sys_mailqueue` (`from`, `recipients`, `mail_content`) VALUES (?,?,?)', $from, implode("\n", $recipients), $mail_content);
}
/**
* Close mail connections
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment