From 6080e8552389add519b3fdcaff22aa334c38e716 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 21 Jan 2014 15:45:08 +0200 Subject: [PATCH] some changes in sender --- application/Espo/Core/Mail/Sender.php | 71 +++++++++++++++++++++------ 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php index b14e5e0f27..a88950efbd 100644 --- a/application/Espo/Core/Mail/Sender.php +++ b/application/Espo/Core/Mail/Sender.php @@ -11,26 +11,25 @@ use Zend\Mail\Transport\SmtpOptions; class Sender { protected $config; - + protected $transport; - + public function __construct($config) { - $this->config = $config; - $this->trasport = new SmtpTransport(); + $this->config = $config; + $this->trasport = new SmtpTransport(); $this->setupGlobal(); } - + protected function setupGlobal() { $config = $this->config; - + $opts = array( 'name' => 'admin', 'host' => $config->get('smtpServer'), 'port' => $config->get('smtpPort'), 'connection_config' => array(); - ); if ($config->get('smtpAuth')) { $opts['connection_class'] = 'login'; @@ -40,21 +39,65 @@ class Sender if ($config->get('smtpSecurity')) { $opts['connection_config']['ssl'] = strtolower($config->get('smtpSecurity')); } - + $options = new SmtpOptions($opts); $transport->setOptions($options); - + return $this; } - - public function send(Email $email, $attachments = array()) + + public function send(Email $email) { $message = new Message(); - + + if ($email->get('fromEmailAddressName')) { + $message->addFrom($email->get('fromEmailAddressName')); + } else { + if (!$config->get('outboundEmailFromAddress')) { + throw new Error('outboundEmailFromAddress is not specified in config.'); + } + $message->addFrom($email->get('outboundEmailFromAddress'), $email->get('outboundEmailFromName')); + } + + $toIds = $email->get('toEmailAddressesIds'); + if (is_array($toIds)) { + $hash = $email->get('toEmailAddressesNames'); + foreach ($toIds as $id) { + $address = $hash[$id]; + if (!empty($address)) { + $message->addTo($address); + } + } + } + + $ccIds = $email->get('ccEmailAddressesIds'); + if (is_array($toIds)) { + $hash = $email->get('ccEmailAddressesIds'); + foreach ($ccIds as $id) { + $address = $hash[$id]; + if (!empty($address)) { + $message->addCC($address); + } + } + } + + $bccIds = $email->get('bccEmailAddressesIds'); + if (is_array($toIds)) { + $hash = $email->get('bccEmailAddressesIds'); + foreach ($bccIds as $id) { + $address = $hash[$id]; + if (!empty($address)) { + $message->addBCC($address); + } + } + } + $message->setSubject($email->get('name')); $message->setBody($email->get('body')); - $message->setSubject($email->get('subject')); - + // TODO attachments + + return $this->transport->send($message); } } +