Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Modules\Email\Services;
- use App\Modules\Email\Exceptions\InvalidRecipientTypeException;
- use Illuminate\Contracts\Config\Repository as Config;
- use Illuminate\Mail\Message;
- use Mail;
- use Swift_Mailer;
- use Swift_Transport_Esmtp_AuthHandler;
- use App;
- use Config as AppConfig;
- class EmailSenderService
- {
- /**
- * Config
- *
- * @var Config
- */
- protected $config;
- /**
- * Custom e-mail send configuration
- *
- * @var array|null
- */
- protected $sendConfig;
- public function __construct(Config $config)
- {
- $this->config = $config;
- $this->clearOneTimeSendConfig();
- }
- /**
- * Set one time send configuration
- *
- * @param $sendConfig
- */
- public function setOneTimeSendConfig($sendConfig)
- {
- $this->sendConfig = $sendConfig;
- }
- /**
- * Clear one time send configuration
- */
- protected function clearOneTimeSendConfig()
- {
- $this->sendConfig = null;
- }
- /**
- * Send e-mail of type $type (depending on configuration)
- *
- * @param string $type
- * @param string $subject
- * @param string $body
- */
- public function sendType($type, $subject, $body)
- {
- if ($this->config->get("email.actions.{$type}.enabled") === true) {
- $from = $this->config->get("email.actions.{$type}.from");
- $to = $this->config->get("email.actions.{$type}.to");
- $this->sendPlain($to, $from, $subject, $body);
- }
- }
- /**
- * Send html e-mail (with optional attachments)
- *
- * @param string $to
- * @param string $from
- * @param string $subject
- * @param string $view
- * @param array $data
- * @param array $attachments Array of attachment. It can contains location
- * of attachments or array of objects (containing properties location and
- * optionally mime and filename to use different filename than saved in
- * storage)
- */
- public function sendHtml(
- $to,
- $from,
- $subject,
- $view,
- array $data = [],
- array $attachments = []
- )
- {
- Mail::send($view, $data,
- function ($message) use ($to, $from, $subject, $attachments) {
- /** @var Message $message */
- $message->from($from);
- //if to is array then loop and add all emails as recipients
- if (is_array($to)) {
- foreach ($to as $sendToEmail) {
- $message->to($sendToEmail);
- }
- } else {
- $message->to($to);
- }
- $message->subject($subject);
- // @todo use below addAttachments method instead of below code
- foreach ($attachments as $attachment) {
- $options = [];
- if (is_object($attachment) || is_array($attachment)) {
- $attachment = (object)$attachment;
- $location = $attachment->location;
- if (isset($attachment->mime)) {
- $options['mime'] = $attachment->mime;
- }
- if (isset($attachment->filename)) {
- $options['as'] = $attachment->filename;
- }
- } else {
- $location = $attachment;
- }
- $message->attach($location, $options);
- }
- });
- }
- /**
- * Add attachments to given message
- *
- * @param Message $message
- * @param array $attachments
- */
- protected function addAttachments(Message $message, array $attachments)
- {
- foreach ($attachments as $attachment) {
- $options = [];
- if (is_object($attachment) || is_array($attachment)) {
- $attachment = (object)$attachment;
- $location = $attachment->location;
- if (isset($attachment->mime)) {
- $options['mime'] = $attachment->mime;
- }
- if (isset($attachment->filename)) {
- $options['as'] = $attachment->filename;
- }
- } else {
- $location = $attachment;
- }
- $message->attach($location, $options);
- }
- }
- /**
- * Send e-mail in plain text
- *
- * @param string $to
- * @param string $from
- * @param string $subject
- * @param string $body
- * @param string|null $fromName
- * @param array $attachments Array of attachment. It can contains location
- * of attachments or array of objects (containing properties location and
- * optionally mime and filename to use different filename than saved in
- * storage)
- */
- public function sendPlain(
- $to,
- $from,
- $subject,
- $body,
- $fromName = null,
- array $attachments = []
- )
- {
- Mail::raw($body,
- function ($message) use ($to, $from, $fromName, $subject, $attachments) {
- /** @var Message $message */
- $message->from($from, $fromName);
- $message->to($to);
- $message->subject($subject);
- $this->addAttachments($message, $attachments);
- });
- }
- /**
- * Send e-mail together with cc, bcc and optional attachments
- *
- * @param string $to
- * @param string $from
- * @param string $cc
- * @param string $bcc
- * @param string $subject
- * @param string $body
- * @param array $attachments
- * @return boolean
- */
- public function sendAdvancedEmail(
- $to,
- $from,
- $cc,
- $bcc,
- $subject,
- $body,
- array $attachments = []
- )
- {
- $mailer = null;
- if ($this->sendConfig !== null) {
- /** @var Swift_Mailer $mailer */
- $mailer = Mail::getSwiftMailer();
- /** @var Swift_Transport_Esmtp_AuthHandler $auth */
- $transport = $mailer->getTransport();
- $transport = $transport::newInstance($this->sendConfig['host'],
- $this->sendConfig['port'])
- ->setUserName($this->sendConfig['username'])
- ->setPassword($this->sendConfig['password']);
- $newMailer = $mailer::newInstance($transport);
- Mail::setSwiftMailer($newMailer);
- }
- Mail::raw($body, function ($message) use (
- $to,
- $from,
- $cc,
- $bcc,
- $subject,
- $attachments
- ) {
- /** @var Message $message */
- if ($from) {
- list($from, $name) = $this->getValidRecipient($from);
- } else {
- $from = $this->config->get('mail.from.address');
- $name = $this->config->get('mail.from.name');
- }
- $message->from($from, $name);
- $this->addRecipients($message, $to, 'to');
- $this->addRecipients($message, $cc, 'cc');
- $this->addRecipients($message, $bcc, 'bcc');
- $message->subject($subject);
- foreach ($attachments as $name => $content) {
- $message->attachData($content, $name);
- }
- });
- // restoring previous mailer
- if ($this->sendConfig !== null) {
- Mail::setSwiftMailer($mailer);
- }
- return !Mail::failures();
- }
- /**
- * Add recipients of given type for email
- *
- * @param Message $message
- * @param array|string $emails
- * @param string $type
- *
- * @throws \Exception
- */
- protected function addRecipients($message, $emails, $type = 'to')
- {
- $allowedTypes = ['to', 'cc', 'bcc'];
- if (!in_array($type, $allowedTypes)) {
- $exp = App::make(InvalidRecipientTypeException::class);
- $exp->setData(['type' => $type, 'allowed_types' => $allowedTypes]);
- throw $exp;
- }
- if ($emails) {
- if (!is_array($emails)) {
- $emails = [$emails];
- }
- foreach ($emails as $item) {
- list($email, $name) = $this->getValidRecipient($item);
- if (trim($email)) {
- $message->{$type}($email, $name);
- }
- }
- }
- }
- /**
- * Explodes recipient to mail address and name
- *
- * @param string $address
- *
- * @return array
- */
- public function getValidRecipient($address)
- {
- preg_match('/(.*)<(.*)>/', $address, $matches);
- if (count($matches) == 3) {
- $mail = trim($matches[2]);
- $to = trim($matches[1]);
- } else {
- $mail = trim(trim(trim($address), '<>'));
- $to = $mail;
- }
- return [$mail, $to];
- }
- /**
- * Send html e-mail with custom transport config
- *
- * @param string $to
- * @param array $from
- * @param array $config
- * @param string $subject
- * @param string $view
- * @param array $data
- */
- public function sendHtmlCustomConfig(
- $to,
- $from,
- $config,
- $subject,
- $view,
- array $data = []
- )
- {
- //Change mail settings on the fly
- AppConfig::set('mail.encryption', $config['encryption']);
- AppConfig::set('mail.host', $config['host']);
- AppConfig::set('mail.port', $config['port']);
- AppConfig::set('mail.username', $config['username']);
- AppConfig::set('mail.password', $config['password']);
- if(!is_array($from)) {
- $from = [
- 'address' => $from
- ];
- }
- Mail::send($view, $data,
- function ($message) use ($to, $from, $subject) {
- /** @var Message $message */
- $message->from($from['address'], isset($from['name']) ? $from['name'] : null);
- //if to is array then loop and add all emails as recipients
- if (is_array($to)) {
- foreach ($to as $sendToEmail) {
- $message->to($sendToEmail);
- }
- } else {
- $message->to($to);
- }
- $message->subject($subject);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement