Source of file SmtpTransport.php
Size: 4,267 Bytes - Last Modified: 2021-12-24T06:50:28+00:00
/var/www/docs.ssmods.com/process/src/src/SmtpTransport.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 | <?php namespace Gurucomkz\campaignmonitor; use CS_REST_Transactional_ClassicEmail; use SilverStripe\Core\Config\Config; use Swift_Transport; use Swift_Mime_Message; use Swift_Events_EventListener; use Swift_TransportException; use SilverStripe\Control\Email\Email; use Swift_Attachment; class SmtpTransport implements Swift_Transport { private $_clientID = null; private $_apiKey = null; /** Connection status */ protected $_started = true; /** * Test if this Transport mechanism has started. * * @return bool */ public function isStarted() { return $this->_started; } /** * Start this Transport mechanism. */ public function start() { } /** * Stop this Transport mechanism. */ public function stop() { } public function setApiKey($value) { $this->_apiKey = $value; } public function getApiKey() { return $this->_apiKey; } public function setClientID($value) { $this->_clientID = $value; } public function getClientID() { return $this->_clientID; } /** * Send the given Message. * * Recipient/sender data will be retrieved from the Message API. * The return value is the number of recipients who were accepted for delivery. * * @param Swift_Mime_Message $message * @param string[] $failedRecipients An array of failures by-reference * * @throws Swift_TransportException * @return int sent messages count */ public function send(Swift_Mime_Message $message, &$failedRecipients = null) { if (null === $message->getHeaders()->get('To')) { throw new Swift_TransportException( 'Cannot send message without a recipient' ); } $auth = [ 'api_key' => $this->getApiKey(), ]; $client_id = $this->getClientID(); $mgClient = new CS_REST_Transactional_ClassicEmail($auth, $client_id); $result = $mgClient->send( $this->getPostData($message), null, 'No' ); $success = $result->http_status_code > 200 && $result->http_status_code < 299; return !!$success; } /** * Register a plugin in the Transport. * * @param \Swift_Events_EventListener $plugin */ public function registerPlugin(Swift_Events_EventListener $plugin) { } /** * Looks at the message headers to find post data. * * @param Swift_Mime_Message $message * * @return array $postData */ const HEADER_MAP = [ 'subject' => 'Subject', 'from' => 'From', 'to' => 'To', 'cc' => 'CC', 'bcc' => 'BCC', 'reply-to' => 'ReplyTo', ]; protected function getPostData(Swift_Mime_Message $message) { // get "form", "to" etc.. $postData = [ 'Html' => $message->getBody(), ]; $messageHeaders = $message->getHeaders(); foreach (self::HEADER_MAP as $swiftHeaderName => $csHeaderName) { /** @var \Swift_Mime_Headers_MailboxHeader $value */ if (null !== $value = $messageHeaders->get($swiftHeaderName)) { $postData[$csHeaderName] = $value->getFieldBody(); } } $send_all_emails_to = Config::forClass(Email::class)->get('send_all_emails_to'); if($send_all_emails_to && !empty($send_all_emails_to)) { $postData['To'] = $send_all_emails_to; if(isset($postData['BCC'])) unset($postData['BCC']); if(isset($postData['CC'])) unset($postData['CC']); } foreach($message->getChildren() as $child){ if(is_a($child, Swift_Attachment::class)) { /** @var Swift_Attachment $child */ if(!isset($postData['Attachments'])) { $postData['Attachments'] = []; } $postData['Attachments'][] = [ "Type" => $child->getContentType(), "Name" => $child->getFilename(), "Content" => base64_encode($child->getBody()) ]; } } return $postData; } } |