Source for file GoSwift.class.inc.php
Documentation is available at GoSwift.class.inc.php
* This file is part of Group-Office. You should have received a copy of the
* Group-Office license along with Group-Office. See the file /LICENSE.TXT
* If you have questions write an e-mail to info@intermesh.nl
* @copyright Copyright Intermesh
* @version $Id: GoSwift.class.inc.php 1263 2008-10-27 16:35:59Z mschering $
* @author Merijn Schering <mschering@intermesh.nl>
* Require all mail classes that are used by this class
require_once($GO_CONFIG->class_path. "html2text.class.inc");
require_once $GO_CONFIG->class_path. 'mail/RFC822.class.inc';
require_once $GO_CONFIG->class_path. 'mail/mimeDecode.class.inc';
require_once $GO_CONFIG->class_path. 'mail/swift/lib/Swift.php';
require_once $GO_CONFIG->class_path. 'mail/swift/lib/Swift/Connection/SMTP.php';
require_once $GO_CONFIG->class_path. 'mail/swift/lib/Swift/Plugin/FileEmbedder.php';
require_once $GO_CONFIG->class_path. 'mail/swift/lib/Swift/Cache/Disk.php';
require_once $GO_CONFIG->class_path. 'mail/smtp_restrict.class.inc.php';
//You change the cache class using this call...
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
//Then you set up the disk cache to write to a writable folder...
Swift_Cache_Disk::setSavePath($GO_CONFIG->tmpdir);
* This class can be used to send an e-mail. It extends the 3rd party Swift class.
* Swift documentation can be found here:
* {@link http://www.swiftmailer.org/wikidocs/"target="_blank Documentation}
* @author Merijn Schering <mschering@intermesh.nl>
* @version $Id: GoSwift.class.inc.php 1263 2008-10-27 16:35:59Z mschering $
* @copyright Copyright Intermesh
* @license GNU General Public License
* @since Group-Office 3.0
* The Swift message to send
* The Swift recipients list
* @var Swift_RecipientList
* The raw message data to store in the sent folder or for a linked message
* When repied to a message it flags the orignal message with ANSWERED
* When repied to a message it flags the orignal message with ANSWERED
* The account record as an array. see table em_accounts
* Constructor. This will create a Swift instance and a Swift message public property.
* @param String $email_to The reciepents in a comma separated string
* @param String $subject The subject of the e-mail
* @param Int $account_id The account id from the em_accounts table. Used for smtp server and sent items
* @param String $priority The priority can be 3 for normal, 1 for high or 5 for low.
function __construct($email_to, $subject, $account_id= 0, $priority = '3', $plain_text_body= null)
global $GO_CONFIG, $GO_MODULES;
require_once ($GO_MODULES->modules['email']['class_path']. "email.class.inc");
$this->account = $email->get_account($account_id);
$this->smtp_host= $this->account['smtp_host'];
$smtp_connection= new Swift_Connection_SMTP($this->account['smtp_host'], $this->account['smtp_port'], $this->account['smtp_encryption']);
if(!empty($this->account['smtp_username']))
$smtp_connection->setUsername($this->account['smtp_username']);
$smtp_connection->setPassword($this->account['smtp_password']);
$this->smtp_host= $GO_CONFIG->smtp_server;
$smtp_connection= new Swift_Connection_SMTP($GO_CONFIG->smtp_server, $GO_CONFIG->smtp_port);
if(!empty($GO_CONFIG->smtp_username))
$smtp_connection->setUsername($GO_CONFIG->smtp_username);
$smtp_connection->setPassword($GO_CONFIG->smtp_password);
parent::__construct($smtp_connection);
$this->message = & new Swift_Message($subject, $plain_text_body);
$this->message->setPriority($priority);
$this->message->headers->set("X-Mailer", "Group-Office ". $GO_CONFIG->version);
$this->message->headers->set("X-MimeOLE", "Produced by Group-Office ". $GO_CONFIG->version);
$to_addresses = $RFC822->parse_address_list($email_to);
foreach($to_addresses as $address)
$this->recipients->addTo($address['email'], $address['personal']);
* @param String $body The message body in HTML or text
* @param String $type Can be html or text
//correction for IE. It likes to add the domain name to relative url's
$this->message->attach(new Swift_Message_Part($body, 'text/'. $type, null, 'UTF-8'));
//add text version of the HTML body
$htmlToText = new Html2Text ($body);
$this->message->attach(new Swift_Message_Part($htmlToText->get_text(), 'text/plain', null, 'UTF-8'));
* If this message is a reply to another message then you must supply the UID and the mailbox
* of the original message. The account id must be passed to the constructor for this to work.
* @param String $reply_uid
* @param String $reply_mailbox
$this->reply_uid= $reply_uid;
$this->reply_mailbox= $reply_mailbox;
function get_mime($email_from= null, $name_from= null)
$name_from= !empty($name_from) ? $name_from : $this->account['name'];
$email_from= !empty($email_from) ? $email_from : $this->account['email'];
$this->message->setFrom(new Swift_Address($email_from, $name_from));
return $data->readFull();
function get_data($email_from= null, $name_from= null)
$name_from= !empty($name_from) ? $name_from : $this->account['name'];
$email_from= !empty($email_from) ? $email_from : $this->account['email'];
$this->message->setFrom(new Swift_Address($email_from, $name_from));
return $data->readFull();
* @param String $email_from The from e-mail address. If you don't supply this then you must supply the account_id to the constructor
* @param String $name_from The from name. If you don't supply this then you must supply the account_id to the constructor
* @param boolean $batch If you set this to true it will use the Swift batchSend method. See the swift docs.
* @throws Swift_ConnectionException If sending fails for any reason.
* @return int The number of successful recipients
function sendmail($email_from= null, $name_from= null, $batch= false)
if(!$smtp_restrict->is_allowed($this->smtp_host))
$msg = sprintf($lang['common']['max_emails_reached'], $this->smtp_host, $smtp_restrict->hosts[gethostbyname($this->smtp_host)]);
throw new Exception($msg);
$name_from= !empty($name_from) ? $name_from : $this->account['name'];
$email_from= !empty($email_from) ? $email_from : $this->account['email'];
//$send_success = parent::batchSend($this->message,$this->recipients, new Swift_Address($email_from, $name_from));
$this->batch = & new Swift_BatchMailer($this);
$this->batch->setSleepTime(10);
$this->batch->setMaxTries(2);
$this->batch->setMaxSuccessiveFailures(10);
$this->batch->send($this->message, $this->recipients, new Swift_Address($email_from, $name_from));
$send_success = parent::send($this->message,$this->recipients, new Swift_Address($email_from, $name_from));
//for appending to send and link
$this->message->setFrom(new Swift_Address($email_from, $name_from));
if($send_success && $this->account)
require_once ($GO_CONFIG->class_path. "mail/imap.class.inc");
$this->account['novalidate_cert'])) {
$this->data = $this->message->build();
$this->data = $this->data->readFull();
if ($imap->append_message($this->account['sent'], $this->data,"\\Seen"))
if (!empty($this->reply_uid) && !empty($this->reply_mailbox))
$uid_arr = array($this->reply_uid);
$imap->set_message_flag($this->reply_mailbox, $uid_arr, "\\Answered");
* Links the message to items in Group-Office. Must be called after send()
* @param array $links Format Array(Array(link_id=>1, link_type=>1));
global $GO_CONFIG, $GO_LINKS;
$link_message['path']= $GO_CONFIG->file_storage_path. 'email/'. date('mY'). '/sent_'. time(). '.eml';
require_once($GO_CONFIG->class_path. 'filesystem.class.inc');
$fs->mkdir_recursive(dirname($link_message['path']));
$this->data = $this->message->build();
$this->data = $this->data->readFull();
$fp = fopen($link_message['path'],"w+");
$link_message['from']= $this->message->headers->get('from');
foreach ($to as $key => $value)
$to[$key] = $value->build();
$link_message['to']= implode(',',$to);
$link_message['subject']= $this->message->headers->get('subject');
$link_message['ctime']= $link_message['time']= time();
$link_message['link_id'] = $email->link_message($link_message);
$link_message['link_id'],
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['input'] = $mime;
if(isset ($structure->headers['from']) )
$addresses= $RFC822->parse_address_list($structure->headers['from']);
$from_email= $addresses[0]['email'];
$from_name= $addresses[0]['personal'];
$subject = isset ($structure->headers['subject']) ? $structure->headers['subject'] : '';
if(isset ($structure->headers['disposition-notification-to']))
//$mail->ConfirmReadingTo = $structure->headers['disposition-notification-to'];
$to = isset ($structure->headers['to']) ? $structure->headers['to'] : '';
$cc = isset ($structure->headers['cc']) ? $structure->headers['cc'] : '';
$bcc = isset ($structure->headers['bcc']) ? $structure->headers['bcc'] : '';
$this->get_parts($structure);
private function get_parts($structure, $part_number_prefix= '')
global $GO_CONFIG, $GO_MODULES;
if (isset ($structure->parts))
foreach ($structure->parts as $part_number=> $part) {
//text part and no attachment so it must be the body
if($structure->ctype_primary== 'multipart' && $structure->ctype_secondary== 'alternative' &&
$part->ctype_primary == 'text' && $part->ctype_secondary== 'plain')
if ($part->ctype_primary == 'text' && (!isset ($part->disposition) || $part->disposition != 'attachment') && empty($part->d_parameters['filename']))
if (eregi('plain', $part->ctype_secondary))
$content_part = nl2br($part->body);
$content_part = $part->body;
$this->body .= $content_part;
}elseif($part->ctype_primary== 'multipart')
$dir= $GO_CONFIG->tmpdir. 'attachments/';
$tmp_file = $dir. $part->d_parameters['filename'];
$mime_type = $part->ctype_primary. '/'. $part->ctype_secondary;
if(isset ($part->headers['content-id']))
$content_id= trim($part->headers['content-id']);
$content_id = substr($part->headers['content-id'], 1,strlen($part->headers['content-id'])- 2);
$img = & new Swift_Message_Image(new Swift_File($tmp_file),utf8_basename($tmp_file), $mime_type,$content_id);
$file = & new Swift_File($tmp_file);
$attachment = & new Swift_Message_Attachment($file,utf8_basename($tmp_file), $mime_type);
$this->message->attach($attachment);
$this->get_parts($part, $part_number_prefix. $part_number. '.');
}elseif(isset ($structure->body))
if (eregi('plain', $structure->ctype_secondary))
$text_part = nl2br($structure->body);
$text_part = $structure->body;
$this->body .= $text_part;
|