some changes in sender

This commit is contained in:
Yuri Kuznetsov
2014-01-21 15:45:08 +02:00
parent 145f12e295
commit 6080e85523
+57 -14
View File
@@ -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);
}
}