Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mail.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Mail/Transport/Abstract.php';
27 
31 #require_once 'Zend/Mime.php';
32 
36 #require_once 'Zend/Mime/Message.php';
37 
41 #require_once 'Zend/Mime/Part.php';
42 
43 
53 {
62  protected static $_defaultTransport = null;
63 
68  protected static $_defaultFrom;
69 
74  protected static $_defaultReplyTo;
75 
80  protected $_charset = 'iso-8859-1';
81 
86  protected $_headers = array();
87 
93 
98  protected $_from = null;
99 
104  protected $_to = array();
105 
110  protected $_recipients = array();
111 
116  protected $_replyTo = null;
117 
122  protected $_returnPath = null;
123 
128  protected $_subject = null;
129 
134  protected $_date = null;
135 
140  protected $_messageId = null;
141 
146  protected $_bodyText = false;
147 
152  protected $_bodyHtml = false;
153 
158  protected $_mimeBoundary = null;
159 
164  protected $_type = null;
165 
172  public $hasAttachments = false;
173 
174 
183  public static function setDefaultTransport(Zend_Mail_Transport_Abstract $transport)
184  {
185  self::$_defaultTransport = $transport;
186  }
187 
195  public static function getDefaultTransport()
196  {
198  }
199 
203  public static function clearDefaultTransport()
204  {
205  self::$_defaultTransport = null;
206  }
207 
213  public function __construct($charset = null)
214  {
215  if ($charset != null) {
216  $this->_charset = $charset;
217  }
218  }
219 
225  public function getCharset()
226  {
227  return $this->_charset;
228  }
229 
239  public function setType($type)
240  {
241  $allowed = array(
245  );
246  if (!in_array($type, $allowed)) {
250  #require_once 'Zend/Mail/Exception.php';
251  throw new Zend_Mail_Exception('Invalid content type "' . $type . '"');
252  }
253 
254  $this->_type = $type;
255  return $this;
256  }
257 
263  public function getType()
264  {
265  return $this->_type;
266  }
267 
276  public function setMimeBoundary($boundary)
277  {
278  $this->_mimeBoundary = $boundary;
279 
280  return $this;
281  }
282 
288  public function getMimeBoundary()
289  {
290  return $this->_mimeBoundary;
291  }
292 
299  public function getEncodingOfHeaders()
300  {
301  return $this->getHeaderEncoding();
302  }
303 
311  public function getHeaderEncoding()
312  {
313  return $this->_headerEncoding;
314  }
315 
323  public function setEncodingOfHeaders($encoding)
324  {
325  return $this->setHeaderEncoding($encoding);
326  }
327 
336  public function setHeaderEncoding($encoding)
337  {
338  $allowed = array(
341  );
342  if (!in_array($encoding, $allowed)) {
346  #require_once 'Zend/Mail/Exception.php';
347  throw new Zend_Mail_Exception('Invalid encoding "' . $encoding . '"');
348  }
349  $this->_headerEncoding = $encoding;
350 
351  return $this;
352  }
353 
362  public function setBodyText($txt, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
363  {
364  if ($charset === null) {
365  $charset = $this->_charset;
366  }
367 
368  $mp = new Zend_Mime_Part($txt);
369  $mp->encoding = $encoding;
370  $mp->type = Zend_Mime::TYPE_TEXT;
371  $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
372  $mp->charset = $charset;
373 
374  $this->_bodyText = $mp;
375 
376  return $this;
377  }
378 
386  public function getBodyText($textOnly = false)
387  {
388  if ($textOnly && $this->_bodyText) {
389  $body = $this->_bodyText;
390  return $body->getContent();
391  }
392 
393  return $this->_bodyText;
394  }
395 
404  public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
405  {
406  if ($charset === null) {
407  $charset = $this->_charset;
408  }
409 
410  $mp = new Zend_Mime_Part($html);
411  $mp->encoding = $encoding;
412  $mp->type = Zend_Mime::TYPE_HTML;
413  $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
414  $mp->charset = $charset;
415 
416  $this->_bodyHtml = $mp;
417 
418  return $this;
419  }
420 
427  public function getBodyHtml($htmlOnly = false)
428  {
429  if ($htmlOnly && $this->_bodyHtml) {
430  $body = $this->_bodyHtml;
431  return $body->getContent();
432  }
433 
434  return $this->_bodyHtml;
435  }
436 
443  public function addAttachment(Zend_Mime_Part $attachment)
444  {
445  $this->addPart($attachment);
446  $this->hasAttachments = true;
447 
448  return $this;
449  }
450 
465  public function createAttachment($body,
466  $mimeType = Zend_Mime::TYPE_OCTETSTREAM,
467  $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
468  $encoding = Zend_Mime::ENCODING_BASE64,
469  $filename = null)
470  {
471 
472  $mp = new Zend_Mime_Part($body);
473  $mp->encoding = $encoding;
474  $mp->type = $mimeType;
475  $mp->disposition = $disposition;
476  $mp->filename = $filename;
477 
478  $this->addAttachment($mp);
479 
480  return $mp;
481  }
482 
488  public function getPartCount()
489  {
490  return count($this->_parts);
491  }
492 
502  protected function _encodeHeader($value)
503  {
504  if (Zend_Mime::isPrintable($value) === false) {
507  } else {
509  }
510  }
511 
512  return $value;
513  }
514 
525  protected function _storeHeader($headerName, $value, $append = false)
526  {
527  if (isset($this->_headers[$headerName])) {
528  $this->_headers[$headerName][] = $value;
529  } else {
530  $this->_headers[$headerName] = array($value);
531  }
532 
533  if ($append) {
534  $this->_headers[$headerName]['append'] = true;
535  }
536 
537  }
538 
545  protected function _clearHeader($headerName)
546  {
547  $this->clearHeader($headerName);
548  }
549 
557  protected function _addRecipientAndHeader($headerName, $email, $name)
558  {
559  $email = $this->_filterEmail($email);
560  $name = $this->_filterName($name);
561  // prevent duplicates
562  $this->_recipients[$email] = 1;
563  $this->_storeHeader($headerName, $this->_formatAddress($email, $name), true);
564  }
565 
574  public function addTo($email, $name='')
575  {
576  if (!is_array($email)) {
577  $email = array($name => $email);
578  }
579 
580  foreach ($email as $n => $recipient) {
581  $this->_addRecipientAndHeader('To', $recipient, is_int($n) ? '' : $n);
582  $this->_to[] = $recipient;
583  }
584 
585  return $this;
586  }
587 
596  public function addCc($email, $name='')
597  {
598  if (!is_array($email)) {
599  $email = array($name => $email);
600  }
601 
602  foreach ($email as $n => $recipient) {
603  $this->_addRecipientAndHeader('Cc', $recipient, is_int($n) ? '' : $n);
604  }
605 
606  return $this;
607  }
608 
615  public function addBcc($email)
616  {
617  if (!is_array($email)) {
618  $email = array($email);
619  }
620 
621  foreach ($email as $recipient) {
622  $this->_addRecipientAndHeader('Bcc', $recipient, '');
623  }
624 
625  return $this;
626  }
627 
633  public function getRecipients()
634  {
635  return array_keys($this->_recipients);
636  }
637 
644  public function clearHeader($headerName)
645  {
646  if (isset($this->_headers[$headerName])){
647  unset($this->_headers[$headerName]);
648  }
649  return $this;
650  }
651 
657  public function clearRecipients()
658  {
659  $this->_recipients = array();
660  $this->_to = array();
661 
662  $this->clearHeader('To');
663  $this->clearHeader('Cc');
664  $this->clearHeader('Bcc');
665 
666  return $this;
667  }
668 
677  public function setFrom($email, $name = null)
678  {
679  if (null !== $this->_from) {
683  #require_once 'Zend/Mail/Exception.php';
684  throw new Zend_Mail_Exception('From Header set twice');
685  }
686 
687  $email = $this->_filterEmail($email);
688  $name = $this->_filterName($name);
689  $this->_from = $email;
690  $this->_storeHeader('From', $this->_formatAddress($email, $name), true);
691 
692  return $this;
693  }
694 
703  public function setReplyTo($email, $name = null)
704  {
705  if (null !== $this->_replyTo) {
709  #require_once 'Zend/Mail/Exception.php';
710  throw new Zend_Mail_Exception('Reply-To Header set twice');
711  }
712 
713  $email = $this->_filterEmail($email);
714  $name = $this->_filterName($name);
715  $this->_replyTo = $email;
716  $this->_storeHeader('Reply-To', $this->_formatAddress($email, $name), true);
717 
718  return $this;
719  }
720 
726  public function getFrom()
727  {
728  return $this->_from;
729  }
730 
736  public function getReplyTo()
737  {
738  return $this->_replyTo;
739  }
740 
746  public function clearFrom()
747  {
748  $this->_from = null;
749  $this->clearHeader('From');
750 
751  return $this;
752  }
753 
759  public function clearReplyTo()
760  {
761  $this->_replyTo = null;
762  $this->clearHeader('Reply-To');
763 
764  return $this;
765  }
766 
774  public static function setDefaultFrom($email, $name = null)
775  {
776  self::$_defaultFrom = array('email' => $email, 'name' => $name);
777  }
778 
784  public static function getDefaultFrom()
785  {
786  return self::$_defaultFrom;
787  }
788 
794  public static function clearDefaultFrom()
795  {
796  self::$_defaultFrom = null;
797  }
798 
805  public function setFromToDefaultFrom() {
806  $from = self::getDefaultFrom();
807  if($from === null) {
808  #require_once 'Zend/Mail/Exception.php';
809  throw new Zend_Mail_Exception(
810  'No default From Address set to use');
811  }
812 
813  $this->setFrom($from['email'], $from['name']);
814 
815  return $this;
816  }
817 
825  public static function setDefaultReplyTo($email, $name = null)
826  {
827  self::$_defaultReplyTo = array('email' => $email, 'name' => $name);
828  }
829 
835  public static function getDefaultReplyTo()
836  {
837  return self::$_defaultReplyTo;
838  }
839 
845  public static function clearDefaultReplyTo()
846  {
847  self::$_defaultReplyTo = null;
848  }
849 
856  public function setReplyToFromDefault() {
857  $replyTo = self::getDefaultReplyTo();
858  if($replyTo === null) {
859  #require_once 'Zend/Mail/Exception.php';
860  throw new Zend_Mail_Exception(
861  'No default Reply-To Address set to use');
862  }
863 
864  $this->setReplyTo($replyTo['email'], $replyTo['name']);
865 
866  return $this;
867  }
868 
876  public function setReturnPath($email)
877  {
878  if ($this->_returnPath === null) {
879  $email = $this->_filterEmail($email);
880  $this->_returnPath = $email;
881  $this->_storeHeader('Return-Path', $email, false);
882  } else {
886  #require_once 'Zend/Mail/Exception.php';
887  throw new Zend_Mail_Exception('Return-Path Header set twice');
888  }
889  return $this;
890  }
891 
899  public function getReturnPath()
900  {
901  if (null !== $this->_returnPath) {
902  return $this->_returnPath;
903  }
904 
905  return $this->_from;
906  }
907 
913  public function clearReturnPath()
914  {
915  $this->_returnPath = null;
916  $this->clearHeader('Return-Path');
917 
918  return $this;
919  }
920 
928  public function setSubject($subject)
929  {
930  if ($this->_subject === null) {
931  $subject = $this->_filterOther($subject);
932  $this->_subject = $this->_encodeHeader($subject);
933  $this->_storeHeader('Subject', $this->_subject);
934  } else {
938  #require_once 'Zend/Mail/Exception.php';
939  throw new Zend_Mail_Exception('Subject set twice');
940  }
941  return $this;
942  }
943 
949  public function getSubject()
950  {
951  return $this->_subject;
952  }
953 
959  public function clearSubject()
960  {
961  $this->_subject = null;
962  $this->clearHeader('Subject');
963 
964  return $this;
965  }
966 
975  public function setDate($date = null)
976  {
977  if ($this->_date === null) {
978  if ($date === null) {
979  $date = date('r');
980  } else if (is_int($date)) {
981  $date = date('r', $date);
982  } else if (is_string($date)) {
983  $date = strtotime($date);
984  if ($date === false || $date < 0) {
988  #require_once 'Zend/Mail/Exception.php';
989  throw new Zend_Mail_Exception('String representations of Date Header must be ' .
990  'strtotime()-compatible');
991  }
992  $date = date('r', $date);
993  } else if ($date instanceof Zend_Date) {
994  $date = $date->get(Zend_Date::RFC_2822);
995  } else {
999  #require_once 'Zend/Mail/Exception.php';
1000  throw new Zend_Mail_Exception(__METHOD__ . ' only accepts UNIX timestamps, Zend_Date objects, ' .
1001  ' and strtotime()-compatible strings');
1002  }
1003  $this->_date = $date;
1004  $this->_storeHeader('Date', $date);
1005  } else {
1009  #require_once 'Zend/Mail/Exception.php';
1010  throw new Zend_Mail_Exception('Date Header set twice');
1011  }
1012  return $this;
1013  }
1014 
1020  public function getDate()
1021  {
1022  return $this->_date;
1023  }
1024 
1030  public function clearDate()
1031  {
1032  $this->_date = null;
1033  $this->clearHeader('Date');
1034 
1035  return $this;
1036  }
1037 
1049  public function setMessageId($id = true)
1050  {
1051  if ($id === null || $id === false) {
1052  return $this;
1053  } elseif ($id === true) {
1054  $id = $this->createMessageId();
1055  }
1056 
1057  if ($this->_messageId === null) {
1058  $id = $this->_filterOther($id);
1059  $this->_messageId = $id;
1060  $this->_storeHeader('Message-Id', '<' . $this->_messageId . '>');
1061  } else {
1065  #require_once 'Zend/Mail/Exception.php';
1066  throw new Zend_Mail_Exception('Message-ID set twice');
1067  }
1068 
1069  return $this;
1070  }
1071 
1077  public function getMessageId()
1078  {
1079  return $this->_messageId;
1080  }
1081 
1082 
1088  public function clearMessageId()
1089  {
1090  $this->_messageId = null;
1091  $this->clearHeader('Message-Id');
1092 
1093  return $this;
1094  }
1095 
1101  public function createMessageId() {
1102 
1103  $time = time();
1104 
1105  if ($this->_from !== null) {
1106  $user = $this->_from;
1107  } elseif (isset($_SERVER['REMOTE_ADDR'])) {
1108  $user = $_SERVER['REMOTE_ADDR'];
1109  } else {
1110  $user = getmypid();
1111  }
1112 
1113  $rand = mt_rand();
1114 
1115  if ($this->_recipients !== array()) {
1116  $recipient = array_rand($this->_recipients);
1117  } else {
1118  $recipient = 'unknown';
1119  }
1120 
1121  if (isset($_SERVER["SERVER_NAME"])) {
1122  $hostName = $_SERVER["SERVER_NAME"];
1123  } else {
1124  $hostName = php_uname('n');
1125  }
1126 
1127  return sha1($time . $user . $rand . $recipient) . '@' . $hostName;
1128  }
1129 
1139  public function addHeader($name, $value, $append = false)
1140  {
1141  $prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
1142  'reply-to', 'return-path',
1143  'date', 'message-id',
1144  );
1145  if (in_array(strtolower($name), $prohibit)) {
1149  #require_once 'Zend/Mail/Exception.php';
1150  throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
1151  }
1152 
1153  $value = $this->_filterOther($value);
1154  $value = $this->_encodeHeader($value);
1155  $this->_storeHeader($name, $value, $append);
1156 
1157  return $this;
1158  }
1159 
1165  public function getHeaders()
1166  {
1167  return $this->_headers;
1168  }
1169 
1178  public function send($transport = null)
1179  {
1180  if ($transport === null) {
1181  if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
1182  #require_once 'Zend/Mail/Transport/Sendmail.php';
1183  $transport = new Zend_Mail_Transport_Sendmail();
1184  } else {
1185  $transport = self::$_defaultTransport;
1186  }
1187  }
1188 
1189  if ($this->_date === null) {
1190  $this->setDate();
1191  }
1192 
1193  if(null === $this->_from && null !== self::getDefaultFrom()) {
1194  $this->setFromToDefaultFrom();
1195  }
1196 
1197  if(null === $this->_replyTo && null !== self::getDefaultReplyTo()) {
1198  $this->setReplyToFromDefault();
1199  }
1200 
1201  $transport->send($this);
1202 
1203  return $this;
1204  }
1205 
1212  protected function _filterEmail($email)
1213  {
1214  $rule = array("\r" => '',
1215  "\n" => '',
1216  "\t" => '',
1217  '"' => '',
1218  ',' => '',
1219  '<' => '',
1220  '>' => '',
1221  );
1222 
1223  return strtr($email, $rule);
1224  }
1225 
1232  protected function _filterName($name)
1233  {
1234  $rule = array("\r" => '',
1235  "\n" => '',
1236  "\t" => '',
1237  '"' => "'",
1238  '<' => '[',
1239  '>' => ']',
1240  );
1241 
1242  return trim(strtr($name, $rule));
1243  }
1244 
1251  protected function _filterOther($data)
1252  {
1253  $rule = array("\r" => '',
1254  "\n" => '',
1255  "\t" => '',
1256  );
1257 
1258  return strtr($data, $rule);
1259  }
1260 
1268  protected function _formatAddress($email, $name)
1269  {
1270  if ($name === '' || $name === null || $name === $email) {
1271  return $email;
1272  } else {
1273  $encodedName = $this->_encodeHeader($name);
1274  if ($encodedName === $name && strcspn($name, '()<>[]:;@\\,.') != strlen($name)) {
1275  $format = '"%s" <%s>';
1276  } else {
1277  $format = '%s <%s>';
1278  }
1279  return sprintf($format, $encodedName, $email);
1280  }
1281  }
1282 
1283 }
const TYPE_TEXT
Definition: Mime.php:33
setFrom($email, $name=null)
Definition: Mail.php:677
clearReplyTo()
Definition: Mail.php:759
send($transport=null)
Definition: Mail.php:1178
const TYPE_HTML
Definition: Mime.php:34
setMessageId($id=true)
Definition: Mail.php:1049
getRecipients()
Definition: Mail.php:633
static $_defaultTransport
Definition: Mail.php:62
const MULTIPART_RELATED
Definition: Mime.php:45
static setDefaultReplyTo($email, $name=null)
Definition: Mail.php:825
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
addAttachment(Zend_Mime_Part $attachment)
Definition: Mail.php:443
$_headerEncoding
Definition: Mail.php:92
getBodyText($textOnly=false)
Definition: Mail.php:386
$email
Definition: details.phtml:13
const DISPOSITION_INLINE
Definition: Mime.php:40
setFromToDefaultFrom()
Definition: Mail.php:805
setBodyText($txt, $charset=null, $encoding=Zend_Mime::ENCODING_QUOTEDPRINTABLE)
Definition: Mail.php:362
$id
Definition: fieldset.phtml:14
getCharset()
Definition: Mail.php:225
getFrom()
Definition: Mail.php:726
clearReturnPath()
Definition: Mail.php:913
$_bodyHtml
Definition: Mail.php:152
getReturnPath()
Definition: Mail.php:899
clearFrom()
Definition: Mail.php:746
static clearDefaultTransport()
Definition: Mail.php:203
createMessageId()
Definition: Mail.php:1101
$_from
Definition: Mail.php:98
setEncodingOfHeaders($encoding)
Definition: Mail.php:323
static getDefaultTransport()
Definition: Mail.php:195
$_charset
Definition: Mail.php:80
getDate()
Definition: Mail.php:1020
static getDefaultFrom()
Definition: Mail.php:784
addTo($email, $name='')
Definition: Mail.php:574
addPart(Zend_Mime_Part $part)
Definition: Message.php:79
_filterOther($data)
Definition: Mail.php:1251
const ENCODING_QUOTEDPRINTABLE
Definition: Mime.php:37
_clearHeader($headerName)
Definition: Mail.php:545
const TYPE_OCTETSTREAM
Definition: Mime.php:32
setReturnPath($email)
Definition: Mail.php:876
const ENCODING_BASE64
Definition: Mime.php:38
getEncodingOfHeaders()
Definition: Mail.php:299
static encodeQuotedPrintableHeader( $str, $charset, $lineLength=self::LINELENGTH, $lineEnd=self::LINEEND)
Definition: Mime.php:488
getSubject()
Definition: Mail.php:949
__construct($charset=null)
Definition: Mail.php:213
clearHeader($headerName)
Definition: Mail.php:644
static $_defaultReplyTo
Definition: Mail.php:74
$type
Definition: item.phtml:13
_addRecipientAndHeader($headerName, $email, $name)
Definition: Mail.php:557
setReplyToFromDefault()
Definition: Mail.php:856
const LINEEND
Definition: Mime.php:42
const LINELENGTH
Definition: Mime.php:41
getBodyHtml($htmlOnly=false)
Definition: Mail.php:427
setSubject($subject)
Definition: Mail.php:928
setBodyHtml($html, $charset=null, $encoding=Zend_Mime::ENCODING_QUOTEDPRINTABLE)
Definition: Mail.php:404
$_bodyText
Definition: Mail.php:146
addCc($email, $name='')
Definition: Mail.php:596
_encodeHeader($value)
Definition: Mail.php:502
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
static $_defaultFrom
Definition: Mail.php:68
createAttachment($body, $mimeType=Zend_Mime::TYPE_OCTETSTREAM, $disposition=Zend_Mime::DISPOSITION_ATTACHMENT, $encoding=Zend_Mime::ENCODING_BASE64, $filename=null)
Definition: Mail.php:465
getHeaders()
Definition: Mail.php:1165
$user
Definition: dummy_user.php:13
$hasAttachments
Definition: Mail.php:172
static setDefaultTransport(Zend_Mail_Transport_Abstract $transport)
Definition: Mail.php:183
_storeHeader($headerName, $value, $append=false)
Definition: Mail.php:525
getPartCount()
Definition: Mail.php:488
addHeader($name, $value, $append=false)
Definition: Mail.php:1139
clearDate()
Definition: Mail.php:1030
$_replyTo
Definition: Mail.php:116
getMimeBoundary()
Definition: Mail.php:288
getType()
Definition: Mail.php:263
$_returnPath
Definition: Mail.php:122
clearSubject()
Definition: Mail.php:959
$_headers
Definition: Mail.php:86
addBcc($email)
Definition: Mail.php:615
_filterName($name)
Definition: Mail.php:1232
static clearDefaultFrom()
Definition: Mail.php:794
setType($type)
Definition: Mail.php:239
clearRecipients()
Definition: Mail.php:657
_filterEmail($email)
Definition: Mail.php:1212
setMimeBoundary($boundary)
Definition: Mail.php:276
$_messageId
Definition: Mail.php:140
static getDefaultReplyTo()
Definition: Mail.php:835
$_subject
Definition: Mail.php:128
$_recipients
Definition: Mail.php:110
static clearDefaultReplyTo()
Definition: Mail.php:845
const MULTIPART_ALTERNATIVE
Definition: Mime.php:43
getReplyTo()
Definition: Mail.php:736
setHeaderEncoding($encoding)
Definition: Mail.php:336
setDate($date=null)
Definition: Mail.php:975
const DISPOSITION_ATTACHMENT
Definition: Mime.php:39
$_mimeBoundary
Definition: Mail.php:158
const MULTIPART_MIXED
Definition: Mime.php:44
getMessageId()
Definition: Mail.php:1077
static isPrintable($str)
Definition: Mime.php:410
static encodeBase64Header( $str, $charset, $lineLength=self::LINELENGTH, $lineEnd=self::LINEEND)
Definition: Mime.php:565
getHeaderEncoding()
Definition: Mail.php:311
static setDefaultFrom($email, $name=null)
Definition: Mail.php:774
setReplyTo($email, $name=null)
Definition: Mail.php:703
_formatAddress($email, $name)
Definition: Mail.php:1268
clearMessageId()
Definition: Mail.php:1088
if(!isset($_GET['name'])) $name
Definition: log.php:14