Source of file Email.php
Size: 12,201 Bytes - Last Modified: 2021-12-23T10:53:20+00:00
/var/www/docs.ssmods.com/process/src/code/SendGrid/Email.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 | <?php namespace SendGrid; class Email { private $from, $from_name, $reply_to, $cc_list, $bcc_list, $subject, $text, $html, $headers, $smtpapi_headers, $attachments; protected $use_headers; public function __construct() { $this->from_name = false; $this->reply_to = false; $this->smtpapi_headers = new SmtpapiHeaders(); } /** * _removeFromList * Given a list of key/value pairs, removes the associated keys * where a value matches the given string ($item) * @param Array $list - the list of key/value pairs * @param String $item - the value to be removed */ private function _removeFromList(&$list, $item, $key_field = null) { foreach ($list as $key => $val) { if($key_field) { if($val[$key_field] == $item) { unset($list[$key]); } } else { if ($val == $item) { unset($list[$key]); } } } //repack the indices $list = array_values($list); } public function addTo($email, $name=null) { $this->smtpapi_headers->addTo($email, $name); return $this; } public function setTo($email) { $this->smtpapi_headers->setTo($email); return $this; } public function setTos(array $emails) { $this->smtpapi_headers->setTos($emails); return $this; } public function removeTo($search_term) { $this->smtpapi_headers->removeTo($search_term); return $this; } public function getTos() { return $this->smtpapi_headers->getTos(); } public function setFrom($email) { $this->from = $email; return $this; } public function getFrom($as_array = false) { if($as_array && ($name = $this->getFromName())) { return array("$this->from" => $name); } else { return $this->from; } } public function setFromName($name) { $this->from_name = $name; return $this; } public function getFromName() { return $this->from_name; } public function setReplyTo($email) { $this->reply_to = $email; return $this; } public function getReplyTo() { return $this->reply_to; } public function setCc($email) { $this->cc_list = array($email); return $this; } public function setCcs(array $email_list) { $this->cc_list = $email_list; return $this; } public function addCc($email) { $this->cc_list[] = $email; return $this; } public function removeCc($email) { $this->_removeFromList($this->cc_list, $email); return $this; } public function getCcs() { return $this->cc_list; } public function setBcc($email) { $this->bcc_list = array($email); return $this; } public function setBccs($email_list) { $this->bcc_list = $email_list; return $this; } public function addBcc($email) { $this->bcc_list[] = $email; return $this; } public function removeBcc($email) { $this->_removeFromList($this->bcc_list, $email); return $this; } public function getBccs() { return $this->bcc_list; } public function setSubject($subject) { $this->subject = $subject; return $this; } public function getSubject() { return $this->subject; } public function setText($text) { $this->text = $text; return $this; } public function getText() { return $this->text; } public function setHtml($html) { $this->html = $html; return $this; } public function getHtml() { return $this->html; } public function setAttachments(array $files) { $this->attachments = array(); foreach($files as $filename => $file) { if (is_string($filename)) { $this->addAttachment($file, $filename); } else { $this->addAttachment($file); } } return $this; } public function setAttachment($file, $custom_filename=null) { $this->attachments = array($this->_getAttachmentInfo($file, $custom_filename)); return $this; } public function addAttachment($file, $custom_filename=null) { $this->attachments[] = $this->_getAttachmentInfo($file, $custom_filename); return $this; } public function getAttachments() { return $this->attachments; } public function removeAttachment($file) { $this->_removeFromList($this->attachments, $file, "file"); return $this; } private function _getAttachmentInfo($file, $custom_filename=null) { $info = pathinfo($file); $info['file'] = $file; if (!is_null($custom_filename)) { $info['custom_filename'] = $custom_filename; } return $info; } public function setCategories($categories) { $this->smtpapi_headers->setCategories($categories); return $this; } public function setCategory($category) { $this->smtpapi_headers->setCategory($category); return $this; } public function addCategory($category) { $this->smtpapi_headers->addCategory($category); return $this; } public function removeCategory($category) { $this->smtpapi_headers->removeCategory($category); return $this; } public function setSubstitutions($key_value_pairs) { $this->smtpapi_headers->setSubstitutions($key_value_pairs); return $this; } public function addSubstitution($from_value, array $to_values) { $this->smtpapi_headers->addSubstitution($from_value, $to_values); return $this; } public function setSections(array $key_value_pairs) { $this->smtpapi_headers->setSections($key_value_pairs); return $this; } public function addSection($from_value, $to_value) { $this->smtpapi_headers->addSection($from_value, $to_value); return $this; } public function setUniqueArguments(array $key_value_pairs) { $this->smtpapi_headers->setUniqueArguments($key_value_pairs); return $this; } public function addUniqueArgument($key, $value) { $this->smtpapi_headers->addUniqueArgument($key, $value); return $this; } public function setFilterSettings($filter_settings) { $this->smtpapi_headers->setFilterSettings($filter_settings); return $this; } public function addFilterSetting($filter_name, $parameter_name, $parameter_value) { $this->smtpapi_headers->addFilterSetting($filter_name, $parameter_name, $parameter_value); return $this; } public function getHeaders() { syslog(LOG_NOTICE, "DEPRECATION NOTICE: getHeaders is deprecated. Use getSmtpapiHeaders instead.\n"); return $this->getSmtpapiHeaders(); } public function getSmtpapiHeaders() { return $this->smtpapi_headers->getHeaders(); } public function getHeadersJson() { syslog(LOG_NOTICE, "DEPRECATION NOTICE: getHeadersJson is deprecated. Use getSmtpapiHeadersJson instead.\n"); return $this->getSmtpapiHeadersJson(); } public function getSmtpapiHeadersJson() { if (count($this->getSmtpapiHeaders()) <= 0) { return "{}"; } return json_encode($this->getSmtpapiHeaders(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); } public function setHeaders($key_value_pairs) { syslog(LOG_NOTICE, "DEPRECATION NOTICE: setHeaders is deprecated. Use setSmtpapiHeaders instead.\n"); $this->setSmtpapiHeaders($key_value_pairs); return $this; } public function setSmtpapiHeaders($key_value_pairs) { $this->smtpapi_headers->setHeaders($key_value_pairs); return $this; } public function addHeader($key, $value) { syslog(LOG_NOTICE, "DEPRECATION NOTICE: addHeader is deprecated. Use addSmtpapiHeader instead.\n"); $this->addSmtpapiHeader($key, $value); return $this; } public function addSmtpapiHeader($key, $value) { $this->smtpapi_headers->addHeader($key, $value); return $this; } public function removeHeader($key) { syslog(LOG_NOTICE, "DEPRECATION NOTICE: removeHeader is deprecated. Use removeSmtpapiHeader instead.\n"); $this->removeSmtpapiHeader($key); return $this; } public function removeSmtpapiHeader($key) { $this->smtpapi_headers->removeHeader($key); return $this; } public function getMessageHeaders() { return $this->headers; } public function getMessageHeadersJson() { if (count($this->getMessageHeaders()) <= 0) { return "{}"; } return json_encode($this->getMessageHeaders(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); } public function setMessageHeaders($key_value_pairs) { $this->headers = $key_value_pairs; return $this; } public function addMessageHeader($key, $value) { $this->headers[$key] = $value; return $this; } public function removeMessageHeader($key) { unset($this->headers[$key]); return $this; } /** * useHeaders * Checks to see whether or not we can or should you headers. In most cases, * we prefer to send our recipients through the headers, but in some cases, * we actually don't want to. However, there are certain circumstances in * which we have to. */ public function useHeaders() { return !($this->_preferNotToUseHeaders() && !$this->_isHeadersRequired()); } public function setRecipientsInHeader($preference) { $this->use_headers = $preference; return $this; } /** * isHeaderRequired * determines whether or not we need to force recipients through the smtpapi headers * @return boolean, if true headers are required */ protected function _isHeadersRequired() { if(count($this->getAttachments()) > 0 || $this->use_headers ) { return true; } return false; } /** * _preferNotToUseHeaders * There are certain cases in which headers are not a preferred choice * to send email, as it limits some basic email functionality. Here, we * check for any of those rules, and add them in to decide whether or * not to use headers * @return boolean, if true we don't */ protected function _preferNotToUseHeaders() { if (count($this->getBccs()) > 0 || count($this->getCcs()) > 0) { return true; } if ($this->use_headers !== null && !$this->use_headers) { return true; } return false; } public function toWebFormat() { $web = array( 'to' => $this->getFrom(), // intentional, set below. 'from' => $this->getFrom(), 'subject' => $this->getSubject(), 'text' => $this->getText(), 'html' => $this->getHtml(), 'headers' => $this->getMessageHeadersJson(), 'x-smtpapi' => $this->getSmtpapiHeadersJson(), ); if ($this->getCcs()) { $web['cc'] = $this->getCcs(); } if ($this->getBccs()) { $web['bcc'] = $this->getBccs(); } if ($this->getFromName()) { $web['fromname'] = $this->getFromName(); } if ($this->getReplyTo()) { $web['replyto'] = $this->getReplyTo(); } // determine if we should send our recipients through our headers, // and set the properties accordingly if ($this->useHeaders()) { $headers = $this->getSmtpapiHeaders(); $headers['to'] = $this->getTos(); $this->setSmtpapiHeaders($headers); $web['x-smtpapi'] = $this->getSmtpapiHeadersJson(); } else { $web['to'] = $this->getTos(); } if ($this->getAttachments()) { foreach($this->getAttachments() as $f) { $file = $f['file']; $extension = null; if (array_key_exists('extension', $f)) { $extension = $f['extension']; }; $filename = $f['filename']; $full_filename = $filename; if (isset($extension)) { $full_filename = $filename.'.'.$extension; } if (array_key_exists('custom_filename', $f)) { $full_filename = $f['custom_filename']; } $contents = '@' . $file; if (class_exists('CurlFile')) { // php >= 5.5 $contents = new \CurlFile($file, $extension, $filename); } $web['files['.$full_filename.']'] = $contents; }; } return $web; } } |