2012-02-08 23:14:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ModelMailMail extends Model {
|
|
|
|
|
|
|
|
|
2023-01-23 17:32:33 +01:00
|
|
|
public function send_smtp_email($smtphost, $smtpport, $yourdomain, $from, $to = [], $msg){
|
2023-01-23 16:47:48 +01:00
|
|
|
|
|
|
|
require_once 'Zend/Mail/Protocol/Smtp.php';
|
|
|
|
require_once 'Zend/Mail/Protocol/Smtp/Auth/Login.php';
|
|
|
|
|
2023-06-04 18:24:18 +02:00
|
|
|
// Workaround for the zend framework
|
|
|
|
$msg = str_replace("\r", "", $msg);
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
$ok = 0;
|
|
|
|
|
|
|
|
if($to == "" || strlen($msg) < 30){ return $ok; }
|
|
|
|
|
2012-11-14 15:28:39 +01:00
|
|
|
if(REWRITE_MESSAGE_ID == 1) {
|
2013-07-29 21:13:15 +02:00
|
|
|
$msg = preg_replace("/Message-ID:([^\n]+)\n/i", "Message-ID: <" . generate_random_string(25) . '@' . SITE_NAME . ">\n", $msg);
|
2012-11-14 15:28:39 +01:00
|
|
|
}
|
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
# config array for connection configuration
|
|
|
|
$config = [];
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
# setting implicit ssl/tls for port 465 and explicit ssl/tls for any other port
|
|
|
|
# in config array zend needs ssl and port 465 for implicit ssl/tls
|
|
|
|
# and tls with any other port for explicit ssl/tls
|
|
|
|
if ($smtpport == '465') {
|
|
|
|
$config['ssl'] = 'ssl';
|
|
|
|
} else {
|
|
|
|
$config['ssl'] = 'tls';
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
$config['port'] = $smtpport;
|
|
|
|
$config['name'] = $yourdomain;
|
|
|
|
|
|
|
|
try {
|
2023-01-23 17:32:33 +01:00
|
|
|
$config['username'] = SMARTHOST_USER;
|
|
|
|
$config['password'] = SMARTHOST_PASSWORD;
|
|
|
|
|
|
|
|
if($config['username'] && $config['password']) {
|
2023-01-23 16:47:48 +01:00
|
|
|
$config['auth'] = 'login';
|
|
|
|
$connection = new Zend_Mail_protocol_Smtp_Auth_Login($smtphost, $smtpport, $config);
|
|
|
|
} else {
|
|
|
|
$connection = new Zend_Mail_protocol_Smtp($smtphost, $smtpport, $config);
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
$connection->connect();
|
|
|
|
$connection->helo($smtphost);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$connection->__destruct();
|
|
|
|
|
|
|
|
# fallback to unencrypted connection if the port is not 465
|
|
|
|
if ($smtpport != '465') {
|
|
|
|
unset($config['ssl']);
|
|
|
|
|
|
|
|
try {
|
2023-01-31 15:20:37 +01:00
|
|
|
if($config['username'] && $config['password']) {
|
2023-01-23 16:47:48 +01:00
|
|
|
$config['auth'] = 'login';
|
|
|
|
$connection = new Zend_Mail_protocol_Smtp_Auth_Login($smtphost, $smtpport, $config);
|
|
|
|
} else {
|
|
|
|
$connection = new Zend_Mail_protocol_Smtp($smtphost, $smtpport, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
$connection->connect();
|
2023-07-22 13:27:29 +02:00
|
|
|
$connection->helo(SMTP_DOMAIN);
|
2023-01-23 16:47:48 +01:00
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$connection->__destruct();
|
|
|
|
syslog(LOG_ERR, "sending mail from=$from, rcpt=" . implode(" ", $to) . ", status=$ok (" . $e->getCode() . "), msg=" . rtrim($e->getMessage()) );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
syslog(LOG_ERR, "sending mail from=$from, rcpt=" . implode(" ", $to) . ", status=$ok (" . $e->getCode() . "), msg=" . rtrim($e->getMessage()) );
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
try {
|
|
|
|
$connection->mail($from);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
foreach ($to as $recipient) {
|
|
|
|
$connection->rcpt($recipient);
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
$connection->data($msg);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
$connectionResponse = $connection->getResponse();
|
2023-01-23 17:22:44 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
$connectionResponseArray = explode(" ", $connectionResponse[0]);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 17:22:44 +01:00
|
|
|
$queue_id = '';
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 17:22:44 +01:00
|
|
|
if($connectionResponseArray[0] == 250) {
|
2023-01-23 16:47:48 +01:00
|
|
|
$ok = 1;
|
2023-01-23 17:22:44 +01:00
|
|
|
$queue_id = trim($connectionResponse[0]);
|
2023-01-23 16:47:48 +01:00
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2023-01-23 16:47:48 +01:00
|
|
|
syslog(LOG_INFO, "sending mail from=$from, rcpt=" . implode(" ", $to) . ", status=$ok, queue_id=$queue_id");
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
syslog(LOG_ERR, "sending mail from=$from, rcpt=" . implode(" ", $to) . ", status=$ok (" . $e->getCode() . "), msg=" . rtrim($e->getMessage()) );
|
|
|
|
$ok = -1;
|
|
|
|
} finally {
|
|
|
|
$connection->__destruct();
|
|
|
|
}
|
2014-02-24 10:10:28 +01:00
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
2013-04-02 22:22:30 +02:00
|
|
|
|
2014-01-05 22:47:47 +01:00
|
|
|
public function message_as_rfc822_attachment($piler_id = '', $msg = '', $rcpt = '') {
|
|
|
|
if($piler_id == '' || $msg == '' || $rcpt == '') { return ''; }
|
|
|
|
|
|
|
|
$boundary = generate_random_string(24);
|
|
|
|
|
|
|
|
$hdr = substr($msg, 0, 8192);
|
|
|
|
$subject = "";
|
|
|
|
|
|
|
|
$s = strstr($hdr, "Subject:");
|
|
|
|
if($s) {
|
|
|
|
$l1 = strlen($s);
|
2023-06-29 16:52:03 +02:00
|
|
|
$l2 = strlen(strstr($s, EOL));
|
2014-01-05 22:47:47 +01:00
|
|
|
if($l1 > $l2 + 10) {
|
|
|
|
$subject = substr($s, 0, $l1 - $l2) . EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$s = "";
|
|
|
|
$s .= "Received: by piler" . EOL . PILER_HEADER_FIELD . $piler_id . EOL;
|
|
|
|
$s .= "Date: " . date("r") . EOL;
|
|
|
|
$s .= "Message-ID: <" . generate_random_string(25) . '@' . SITE_NAME . ">" . EOL;
|
|
|
|
$s .= "From: " . SMTP_FROMADDR . EOL;
|
|
|
|
$s .= "To: " . $rcpt . EOL;
|
|
|
|
|
|
|
|
if($subject) { $s .= $subject; }
|
|
|
|
else { $s .= "Subject: Retrieved message from the email archive" . EOL; }
|
|
|
|
|
|
|
|
$s .= "MIME-Version: 1.0" . EOL;
|
|
|
|
$s .= "Content-Type: multipart/mixed; boundary=\"$boundary\"" . EOL . EOL . EOL;
|
|
|
|
$s .= "--$boundary" . EOL;
|
|
|
|
$s .= "Content-Type: message/rfc822; name=\"" . $piler_id . "\"" . EOL;
|
|
|
|
$s .= "Content-Disposition: attachment; filename=\"" . $piler_id . "\"" . EOL . EOL;
|
|
|
|
$s .= $msg . EOL;
|
|
|
|
$s .= "--$boundary" . EOL;
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 22:22:30 +02:00
|
|
|
public function connect_imap() {
|
|
|
|
$this->imap = new Zend_Mail_Protocol_Imap(IMAP_HOST, IMAP_PORT, IMAP_SSL);
|
|
|
|
|
2013-11-18 19:24:33 +01:00
|
|
|
$session = Registry::get('session');
|
|
|
|
|
2013-04-02 22:22:30 +02:00
|
|
|
if($this->imap) {
|
2013-11-18 19:24:33 +01:00
|
|
|
if($this->imap->login($session->get("username"), $session->get("password"))) { return 1; }
|
2023-01-23 16:47:48 +01:00
|
|
|
}
|
2013-04-02 22:22:30 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function disconnect_imap() {
|
|
|
|
$this->imap->logout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|