public function sendEmail($from, $to, $cc, $subject, $body, $attachmentFilename) { $message = new Message(); $message->setFrom($from); $message->setTo($to); $message->setCc($cc); $message->setSubject($subject); $mimeMessage = new \Zend\Mime\Message(); $part = new \Zend\Mime\Part($body); $part->setType(Mime::TYPE_TEXT); $part->setCharset('UTF-8'); $mimeMessage->addPart($part); $part = new \Zend\Mime\Part('<p>' . $body . '<p>'); $part->setType(Mime::TYPE_HTML); $part->setCharset('UTF-8'); $mimeMessage->addPart($part); $part = new \Zend\Mime\Part($body); $part->setType(Mime::TYPE_OCTETSTREAM); $part->setEncoding(Mime::ENCODING_BASE64); $part->setFileName($attachmentFilename); $part->setDisposition(Mime::DISPOSITION_ATTACHMENT); $mimeMessage->addPart($part); $message->setBody($mimeMessage); $this->transport->send($message); $this->debugSection('ZendMailer', $subject . ' ' . $from . ' -> ' . $to); }
public function createService(ServiceLocatorInterface $serviceLocator) { $transport = new FileTransport(); $options = new FileOptions(array('path' => 'data/mail/', 'callback' => function (FileTransport $transport) { return 'Message_' . microtime(true) . '_' . mt_rand() . '.txt'; })); $transport->setOptions($options); return $transport; }
public function testTransportSetup() { $transport = new Mail\Transport\File(); $callback = function () { return 'test'; }; $transport = new Mail\Transport\File(array('path' => $this->_tmpdir, 'callback' => $callback)); $this->assertEquals($this->_tmpdir, $transport->getPath()); $this->assertSame($callback, $transport->getCallback()); }
protected function setUp() { $message = new MailMessage(); $transport = new Transport\File(); $options = new Transport\FileOptions(array('path' => __DIR__, 'callback' => function (Transport\File $transport) { return MailTest::FILENAME; })); $transport->setOptions($options); $this->writer = new MailWriter($message, $transport); $this->log = new Logger(); $this->log->addWriter($this->writer); }
public function testConstructWithOptions() { $message = new MailMessage(); $transport = new Transport\File(); $options = new Transport\FileOptions(array('path' => __DIR__, 'callback' => function (Transport\File $transport) { return MailTest::FILENAME; })); $transport->setOptions($options); $formatter = new \Zend\Log\Formatter\Simple(); $filter = new \Zend\Log\Filter\Mock(); $writer = new MailWriter(array('filters' => $filter, 'formatter' => $formatter, 'mail' => $message, 'transport' => $transport)); $this->assertAttributeEquals($message, 'mail', $writer); $this->assertAttributeEquals($transport, 'transport', $writer); $this->assertAttributeEquals($formatter, 'formatter', $writer); $filters = self::readAttribute($writer, 'filters'); $this->assertCount(1, $filters); $this->assertEquals($filter, $filters[0]); }
/** * Sent a email to a user based on the set template. * * @param string * @param string $name * @param array $variables * * @return EmailService * @throws \Exception */ public function send($address, $name = null, array $variables = null) { $config = $this->getServiceManager()->get('Config')['service-email']; if (null !== $variables) { $this->setVariables($variables); } $this->setVariable('baseurl', $this->constructBaseUrl()); $message = $this->_constructEmail($address, $name); switch (strtolower($config['transport'])) { case 'sendmail': $transport = new Sendmail(); break; case 'smtp': $transport = new Smtp(new SmtpOptions($config['smtp'])); break; case 'file': $transport = new File(new FileOptions($config['file'])); break; default: throw new Exception('Invalid transport type: ' . $config->transport . '.'); } try { $transport->send($message); } catch (\Exception $e) { throw new \Exception('Could not send out the requested e-mail. An error occured: ' . $e->getMessage()); } $this->clean(); return $this; }
protected static function getDefaultTransport() { $config = Rails::application()->config()['action_mailer']; if (!$config['delivery_method']) { throw new Exception\RuntimeException("Delivery method for ActionMailer not set"); } switch ($config['delivery_method']) { /** * Rails -> Zend options: * address -> host * port -> port * domain -> name * authentication -> connection_class * user_name -> connection_config[username] * password -> connection_config[password] * enable_starttls_auto (true) -> connection_config[ssl] => 'tls' (requires openssl extension) * enable_starttls_auto (false) -> connection_config[ssl] => null * * Regarding `authentication`, ZF2 adds the `smtp` option and * the `cram_md5` option is called `crammd5`. * * For convenience, the options under `connection_config` should be * set */ case 'smtp': $defaultConfig = ['host' => '127.0.0.1', 'name' => 'localhost', 'port' => 25, 'connection_class' => 'login']; $defaultConnConfig = ['username' => '', 'password' => '', 'ssl' => 'tls']; $connConfig = array_merge($defaultConnConfig, $config['smtp_settings']->delete('connection_config')->toArray()); $smtp = array_merge($defaultConfig, $config['smtp_settings']->toArray()); $smtp['connection_config'] = $connConfig; $options = new Mail\Transport\SmtpOptions($smtp); $transport = new Mail\Transport\Smtp(); $transport->setOptions($options); break; /** * Rails -> Zend options * location -> path * name_generator -> callback */ /** * Rails -> Zend options * location -> path * name_generator -> callback */ case 'file': $fileConfig = $config['file_settings']; $options = []; if ($fileConfig->path === null) { $dir = Rails::root() . '/tmp/mail'; if (!is_dir($dir)) { mkdir($dir, 0777, true); } $fileConfig->path = $dir; } $options['path'] = $fileConfig->path; if ($fileConfig->name_generator === null) { $options['callback'] = 'Rails\\ActionMailer\\ActionMailer::filenameGenerator'; } else { $options['callback'] = $fileConfig->name_generator; } $fileOptions = new Mail\Transport\FileOptions($options); $transport = new Mail\Transport\File(); $transport->setOptions($fileOptions); break; case 'sendmail': $transport = new Mail\Transport\Sendmail(); break; case $config['delivery_method'] instanceof Closure: $transport = $config['delivery_method'](); break; default: throw new Exception\RuntimeException(sprintf("Unknown deilvery method %s", $config['delivery_method'])); break; } return $transport; }
/** * Load the services. * * @return array */ public function getServiceConfig() { return array('initializers' => array('DataSourceAwareInterface' => function ($instance, $sm) { if ($instance instanceof Lib\DataSourceAwareInterface) { $instance->setDataSource($sm->get('PDO')); } }, 'QueueConnectionAwareInterface' => function ($instance, $sm) { if ($instance instanceof QueueConnectionAwareInterface) { $i = $sm->get('Stjornvisi\\Lib\\QueueConnectionFactory'); $instance->setQueueConnectionFactory($sm->get('Stjornvisi\\Lib\\QueueConnectionFactory')); } }, 'LoggerAwareInterface' => function ($instance, $sm) { if ($instance instanceof LoggerAwareInterface) { $instance->setLogger($sm->get('Logger')); } }, 'DataStoreInterface' => function ($instance, $sm) { if ($instance instanceof DataStoreInterface) { $instance->setDateStore($sm->get('PDO\\Config')); } }, 'NotifyEventManagerAwareInterface' => function ($instance, $sm) { if ($instance instanceof NotifyEventManagerAwareInterface) { $instance->setEventManager($sm->get('ServiceEventManager')); } }, 'ServiceEventManagerAwareInterface' => function ($instance, $sm) { if ($instance instanceof ServiceEventManagerAwareInterface) { $instance->setEventManager($sm->get('ServiceEventManager')); } }), 'invokables' => ['Stjornvisi\\Service\\User' => 'Stjornvisi\\Service\\User', 'Stjornvisi\\Service\\Company' => 'Stjornvisi\\Service\\Company', 'Stjornvisi\\Service\\Event' => 'Stjornvisi\\Service\\Event', 'Stjornvisi\\Service\\Group' => 'Stjornvisi\\Service\\Group', 'Stjornvisi\\Service\\News' => 'Stjornvisi\\Service\\News', 'Stjornvisi\\Service\\Board' => 'Stjornvisi\\Service\\Board', 'Stjornvisi\\Service\\Article' => 'Stjornvisi\\Service\\Article', 'Stjornvisi\\Service\\Page' => 'Stjornvisi\\Service\\Page', 'Stjornvisi\\Service\\Values' => 'Stjornvisi\\Service\\Values', 'Stjornvisi\\Service\\Conference' => 'Stjornvisi\\Service\\Conference', 'Stjornvisi\\Service\\Skeleton' => 'Stjornvisi\\Service\\Skeleton', 'Stjornvisi\\Service\\Anaegjuvogin' => 'Stjornvisi\\Service\\Anaegjuvogin', 'Stjornvisi\\Notify\\Submission' => 'Stjornvisi\\Notify\\Submission', 'Stjornvisi\\Notify\\Event' => 'Stjornvisi\\Notify\\Event', 'Stjornvisi\\Notify\\Password' => 'Stjornvisi\\Notify\\Password', 'Stjornvisi\\Notify\\Group' => 'Stjornvisi\\Notify\\Group', 'Stjornvisi\\Notify\\All' => 'Stjornvisi\\Notify\\All', 'Stjornvisi\\Notify\\Attend' => 'Stjornvisi\\Notify\\Attend', 'Stjornvisi\\Notify\\UserValidate' => 'Stjornvisi\\Notify\\UserValidate', 'Stjornvisi\\Notify\\Digest' => 'Stjornvisi\\Notify\\Digest', 'Stjornvisi\\Event\\SystemExceptionListener' => 'Stjornvisi\\Event\\SystemExceptionListener', 'Stjornvisi\\Event\\PersistenceLoginListener' => 'Stjornvisi\\Event\\PersistenceLoginListener', 'Stjornvisi\\Event\\LayoutSelectListener' => 'Stjornvisi\\Event\\LayoutSelectListener', 'Stjornvisi\\Event\\NotifyListener' => 'Stjornvisi\\Event\\NotifyListener', 'Imagine\\Image\\Imagine' => 'Imagine\\Gd\\Imagine', 'Stjornvisi\\Auth\\Adapter' => 'Stjornvisi\\Auth\\Adapter', 'Stjornvisi\\Auth\\Facebook' => 'Stjornvisi\\Auth\\Facebook', 'AuthenticationService' => 'Zend\\Authentication\\AuthenticationService'], 'aliases' => array('UserService' => 'Stjornvisi\\Service\\User', 'GroupService' => 'Stjornvisi\\Service\\Group', 'Zend\\Authentication\\AuthenticationService' => 'AuthenticationService'), 'factories' => array('Logger' => function ($sm) { $log = new Logger('stjornvisi'); $log->pushHandler(new StreamHandler('php://stdout')); $evn = getenv('APPLICATION_ENV') ?: 'production'; if ($evn == 'development') { //... } else { $handler = new StreamHandler('./data/log/error.json', Logger::ERROR); $handler->setFormatter(new \Stjornvisi\Lib\JsonFormatter()); $log->pushHandler($handler); $handler = new StreamHandler('./data/log/system.log'); $handler->setFormatter(new JsonFormatter()); $log->pushHandler($handler); $log->pushHandler(new SlackHandler("xoxp-3745519896-3745519908-3921078470-26445a", "#stjornvisi", "Angry Hamster", true, null, Logger::CRITICAL)); } return $log; }, 'ServiceEventManager' => function ($sm) { $logger = $sm->get('Logger'); $manager = new EventManager(); $manager->attach(new ErrorEventListener($logger)); $manager->attach(new ServiceEventListener($logger)); $activityListener = new ActivityListener($logger); $activityListener->setQueueConnectionFactory($sm->get('Stjornvisi\\Lib\\QueueConnectionFactory')); $manager->attach($activityListener); return $manager; }, 'Stjornvisi\\Service\\Map' => function ($sm) { return new JaMap(new Client()); }, 'Stjornvisi\\Service\\Email' => function ($sm) { $config = $sm->get('config'); $obj = new Email(); $obj->setDataSource(new PDO($config['tracker']['dns'], $config['tracker']['user'], $config['tracker']['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ))); return $obj; }, 'PDO\\Config' => function ($sm) { $config = $sm->get('config'); return array('dns' => $config['db']['dns'], 'user' => $config['db']['user'], 'password' => $config['db']['password'], 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ)); }, 'PDO' => function ($sm) { $config = $sm->get('PDO\\Config'); return new PDO($config['dns'], $config['user'], $config['password'], $config['options']); }, 'MailTransport' => function ($sm) { $evn = getenv('APPLICATION_ENV') ?: 'production'; if ($evn == 'development') { $transport = new FileTransport(); $transport->setOptions(new FileOptions(['path' => './data/', 'callback' => function (FileTransport $transport) { return 'Message_' . microtime(true) . '.eml'; }])); return $transport; } else { $transport = new SmtpTransport(); $protocol = new \Zend\Mail\Protocol\Smtp(); $transport->setConnection($protocol); return $transport; } }, 'Stjornvisi\\Lib\\QueueConnectionFactory' => function ($sm) { $evn = getenv('APPLICATION_ENV') ?: 'production'; if ($evn == 'testing') { return new QueueConnectionFactoryStub(); } $config = $sm->get('config'); $queue = new QueueConnectionFactory(); $queue->setConfig($config['queue']); return $queue; }, 'Stjornvisi\\Form\\NewUserCompanySelect' => function ($sm) { return new NewUserCompanySelect($sm->get('Stjornvisi\\Service\\Company')); }, 'Stjornvisi\\Form\\NewUserCompany' => function ($sm) { return new NewUserCompany($sm->get('Stjornvisi\\Service\\Values'), $sm->get('Stjornvisi\\Service\\Company')); }, 'Stjornvisi\\Form\\NewUserUniversitySelect' => function ($sm) { return new NewUserUniversitySelect($sm->get('Stjornvisi\\Service\\Company')); }, 'Stjornvisi\\Form\\NewUserIndividual' => function ($sm) { return new NewUserIndividual($sm->get('Stjornvisi\\Service\\Values'), $sm->get('Stjornvisi\\Service\\Company')); }, 'Stjornvisi\\Form\\NewUserCredentials' => function ($sm) { return new NewUserCredentials($sm->get('Stjornvisi\\Service\\Values'), $sm->get('Stjornvisi\\Service\\User')); }, 'Stjornvisi\\Form\\Company' => function ($sm) { return new CompanyForm($sm->get('Stjornvisi\\Service\\Values'), $sm->get('Stjornvisi\\Service\\Company')); }, 'Stjornvisi\\Queue' => function () { $con = new AMQPConnection('localhost', 5672, 'guest', 'guest'); return $con; }), 'shared' => array('Stjornvisi\\Service\\Email' => false, 'Stjornvisi\\Queue' => false)); }
protected static function setDefaultTransport() { $config = Rails::application()->config()->action_mailer; if (!$config['delivery_method']) { throw new Exception\RuntimeException("Delivery method for ActionMailer not set"); } switch ($config['delivery_method']) { /** * Rails to Zend options: * address => name * domain => host * port => port * authentication => connection_class * user_name => connection_config[username] * password => connection_config[password] * enable_starttls_auto (true) => connection_config[ssl] => 'tls' * enable_starttls_auto (false) => connection_config[ssl] => 'ssl' */ case 'smtp': $defaultConfig = ['address' => '127.0.0.1', 'domain' => 'localhost', 'port' => 25, 'user_name' => '', 'password' => '', 'enable_starttls_auto' => true, 'authentication' => 'login']; $smtp = array_merge($defaultConfig, $config['smtp_settings']->toArray()); $options = ['host' => $smtp['address'], 'name' => $smtp['domain'], 'port' => $smtp['port'], 'connection_class' => $smtp['authentication'], 'connection_config' => ['username' => $smtp['user_name'], 'password' => $smtp['password'], 'ssl' => $smtp['enable_starttls_auto'] ? 'tls' : null]]; $options = new Mail\Transport\SmtpOptions($options); $transport = new Mail\Transport\Smtp(); $transport->setOptions($options); break; /** * location => path * name_generator => callback */ /** * location => path * name_generator => callback */ case 'file': $customOpts = $config['file_settings']; $options = []; if ($customOpts['location'] === null) { $dir = Rails::root() . '/tmp/mail'; if (!is_dir($dir)) { mkdir($dir, 0777, true); } $customOpts['location'] = $dir; } $options['path'] = $customOpts['location']; if ($customOpts['name_generator'] === null) { $options['callback'] = 'Rails\\ActionMailer\\ActionMailer::filenameGenerator'; } else { $options['callback'] = $customOpts['name_generator']; } $fileOptions = new Mail\Transport\FileOptions($options); $transport = new Mail\Transport\File(); $transport->setOptions($fileOptions); break; case 'sendmail': $transport = new Mail\Transport\Sendmail(); break; case $config['delivery_method'] instanceof Closure: $transport = $config['delivery_method'](); break; default: throw new Exception\RuntimeException(sprintf("Unknown deilvery method %s", $config['delivery_method'])); break; } self::transport($transport); }