Source for file Mail.php

Documentation is available at Mail.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Mail
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Mail provides functionality to compose and send both text and MIME-compliant multipart e-mail messages.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Mail
  20. 20:  *
  21. 21:  * @property   string $from 
  22. 22:  * @property   string $subject 
  23. 23:  * @property   string $returnPath 
  24. 24:  * @property   int $priority 
  25. 25:  * @property   string $htmlBody 
  26. 26:  */
  27. 27: class Mail extends MailMimePart
  28. 28: {
  29. 29:     /**#@+ Priority */
  30. 30:     const HIGH = 1;
  31. 31:     const NORMAL = 3;
  32. 32:     const LOW = 5;
  33. 33:     /**#@-*/
  34. 34:  
  35. 35:     /** @var IMailer */
  36. 36:     public static $defaultMailer 'Nette\Mail\SendmailMailer';
  37. 37:  
  38. 38:     /** @var array */
  39. 39:     public static $defaultHeaders array(
  40. 40:         'MIME-Version' => '1.0',
  41. 41:         'X-Mailer' => 'Nette Framework',
  42. 42:     );
  43. 43:  
  44. 44:     /** @var IMailer */
  45. 45:     private $mailer;
  46. 46:  
  47. 47:     /** @var string */
  48. 48:     private $charset 'UTF-8';
  49. 49:  
  50. 50:     /** @var array */
  51. 51:     private $attachments array();
  52. 52:  
  53. 53:     /** @var array */
  54. 54:     private $inlines array();
  55. 55:  
  56. 56:     /** @var mixed */
  57. 57:     private $html;
  58. 58:  
  59. 59:     /** @var string */
  60. 60:     private $basePath;
  61. 61:  
  62. 62:  
  63. 63:  
  64. 64:     public function __construct()
  65. 65:     {
  66. 66:         foreach (self::$defaultHeaders as $name => $value{
  67. 67:             $this->setHeader($name$value);
  68. 68:         }
  69. 69:         $this->setHeader('Date'date('r'));
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Sets the sender of the message.
  76. 76:      * @param  string  e-mail or format "John Doe" <doe@example.com>
  77. 77:      * @param  string 
  78. 78:      * @return Mail  provides a fluent interface
  79. 79:      */
  80. 80:     public function setFrom($email$name NULL)
  81. 81:     {
  82. 82:         $this->setHeader('From'$this->formatEmail($email$name));
  83. 83:         return $this;
  84. 84:     }
  85. 85:  
  86. 86:  
  87. 87:  
  88. 88:     /**
  89. 89:      * Returns the sender of the message.
  90. 90:      * @return array 
  91. 91:      */
  92. 92:     public function getFrom()
  93. 93:     {
  94. 94:         return $this->getHeader('From');
  95. 95:     }
  96. 96:  
  97. 97:  
  98. 98:  
  99. 99:     /**
  100. 100:      * Adds the reply-to address.
  101. 101:      * @param  string  e-mail or format "John Doe" <doe@example.com>
  102. 102:      * @param  string 
  103. 103:      * @return Mail  provides a fluent interface
  104. 104:      */
  105. 105:     public function addReplyTo($email$name NULL)
  106. 106:     {
  107. 107:         $this->setHeader('Reply-To'$this->formatEmail($email$name)TRUE);
  108. 108:         return $this;
  109. 109:     }
  110. 110:  
  111. 111:  
  112. 112:  
  113. 113:     /**
  114. 114:      * Sets the subject of the message.
  115. 115:      * @param  string 
  116. 116:      * @return Mail  provides a fluent interface
  117. 117:      */
  118. 118:     public function setSubject($subject)
  119. 119:     {
  120. 120:         $this->setHeader('Subject'$subject);
  121. 121:         return $this;
  122. 122:     }
  123. 123:  
  124. 124:  
  125. 125:  
  126. 126:     /**
  127. 127:      * Returns the subject of the message.
  128. 128:      * @return string 
  129. 129:      */
  130. 130:     public function getSubject()
  131. 131:     {
  132. 132:         return $this->getHeader('Subject');
  133. 133:     }
  134. 134:  
  135. 135:  
  136. 136:  
  137. 137:     /**
  138. 138:      * Adds email recipient.
  139. 139:      * @param  string  e-mail or format "John Doe" <doe@example.com>
  140. 140:      * @param  string 
  141. 141:      * @return Mail  provides a fluent interface
  142. 142:      */
  143. 143:     public function addTo($email$name NULL// addRecipient()
  144. 144:     {
  145. 145:         $this->setHeader('To'$this->formatEmail($email$name)TRUE);
  146. 146:         return $this;
  147. 147:     }
  148. 148:  
  149. 149:  
  150. 150:  
  151. 151:     /**
  152. 152:      * Adds carbon copy email recipient.
  153. 153:      * @param  string  e-mail or format "John Doe" <doe@example.com>
  154. 154:      * @param  string 
  155. 155:      * @return Mail  provides a fluent interface
  156. 156:      */
  157. 157:     public function addCc($email$name NULL)
  158. 158:     {
  159. 159:         $this->setHeader('Cc'$this->formatEmail($email$name)TRUE);
  160. 160:         return $this;
  161. 161:     }
  162. 162:  
  163. 163:  
  164. 164:  
  165. 165:     /**
  166. 166:      * Adds blind carbon copy email recipient.
  167. 167:      * @param  string  e-mail or format "John Doe" <doe@example.com>
  168. 168:      * @param  string 
  169. 169:      * @return Mail  provides a fluent interface
  170. 170:      */
  171. 171:     public function addBcc($email$name NULL)
  172. 172:     {
  173. 173:         $this->setHeader('Bcc'$this->formatEmail($email$name)TRUE);
  174. 174:         return $this;
  175. 175:     }
  176. 176:  
  177. 177:  
  178. 178:  
  179. 179:     /**
  180. 180:      * Formats recipient e-mail.
  181. 181:      * @param  string 
  182. 182:      * @param  string 
  183. 183:      * @return array 
  184. 184:      */
  185. 185:     private function formatEmail($email$name)
  186. 186:     {
  187. 187:         if (!$name && preg_match('#^(.+) +<(.*)>$#'$email$matches)) {
  188. 188:             return array($matches[2=> $matches[1]);
  189. 189:         else {
  190. 190:             return array($email => $name);
  191. 191:         }
  192. 192:     }
  193. 193:  
  194. 194:  
  195. 195:  
  196. 196:     /**
  197. 197:      * Sets the Return-Path header of the message.
  198. 198:      * @param  string  e-mail
  199. 199:      * @return Mail  provides a fluent interface
  200. 200:      */
  201. 201:     public function setReturnPath($email)
  202. 202:     {
  203. 203:         $this->setHeader('Return-Path'$email);
  204. 204:         return $this;
  205. 205:     }
  206. 206:  
  207. 207:  
  208. 208:  
  209. 209:     /**
  210. 210:      * Returns the Return-Path header.
  211. 211:      * @return string 
  212. 212:      */
  213. 213:     public function getReturnPath()
  214. 214:     {
  215. 215:         return $this->getHeader('From');
  216. 216:     }
  217. 217:  
  218. 218:  
  219. 219:  
  220. 220:     /**
  221. 221:      * Sets email priority.
  222. 222:      * @param  int 
  223. 223:      * @return Mail  provides a fluent interface
  224. 224:      */
  225. 225:     public function setPriority($priority)
  226. 226:     {
  227. 227:         $this->setHeader('X-Priority'(int) $priority);
  228. 228:         return $this;
  229. 229:     }
  230. 230:  
  231. 231:  
  232. 232:  
  233. 233:     /**
  234. 234:      * Returns email priority.
  235. 235:      * @return int 
  236. 236:      */
  237. 237:     public function getPriority()
  238. 238:     {
  239. 239:         return $this->getHeader('X-Priority');
  240. 240:     }
  241. 241:  
  242. 242:  
  243. 243:  
  244. 244:     /**
  245. 245:      * Sets HTML body.
  246. 246:      * @param  string|Nette\Templates\ITemplate
  247. 247:      * @param  mixed base-path or FALSE to disable parsing
  248. 248:      * @return Mail  provides a fluent interface
  249. 249:      */
  250. 250:     public function setHtmlBody($html$basePath NULL)
  251. 251:     {
  252. 252:         $this->html $html;
  253. 253:         $this->basePath $basePath;
  254. 254:         return $this;
  255. 255:     }
  256. 256:  
  257. 257:  
  258. 258:  
  259. 259:     /**
  260. 260:      * Gets HTML body.
  261. 261:      * @return mixed 
  262. 262:      */
  263. 263:     public function getHtmlBody()
  264. 264:     {
  265. 265:         return $this->html;
  266. 266:     }
  267. 267:  
  268. 268:  
  269. 269:  
  270. 270:     /**
  271. 271:      * Adds embedded file.
  272. 272:      * @param  string 
  273. 273:      * @param  string 
  274. 274:      * @param  string 
  275. 275:      * @return MailMimePart 
  276. 276:      */
  277. 277:     public function addEmbeddedFile($file$content NULL$contentType NULL)
  278. 278:     {
  279. 279:         $part new MailMimePart;
  280. 280:         $part->setBody($content === NULL $this->readFile($file$contentType: (string) $content);
  281. 281:         $part->setContentType($contentType $contentType 'application/octet-stream');
  282. 282:         $part->setEncoding(self::ENCODING_BASE64);
  283. 283:         $part->setHeader('Content-Disposition''inline; filename="' basename($file'"');
  284. 284:         $part->setHeader('Content-ID''<' md5(uniqid(''TRUE)) '>');
  285. 285:         return $this->inlines[$file$part;
  286. 286:     }
  287. 287:  
  288. 288:  
  289. 289:  
  290. 290:     /**
  291. 291:      * Adds attachment.
  292. 292:      * @param  string 
  293. 293:      * @param  string 
  294. 294:      * @param  string 
  295. 295:      * @return MailMimePart 
  296. 296:      */
  297. 297:     public function addAttachment($file$content NULL$contentType NULL)
  298. 298:     {
  299. 299:         $part new MailMimePart;
  300. 300:         $part->setBody($content === NULL $this->readFile($file$contentType: (string) $content);
  301. 301:         $part->setContentType($contentType $contentType 'application/octet-stream');
  302. 302:         $part->setEncoding(self::ENCODING_BASE64);
  303. 303:         $part->setHeader('Content-Disposition''attachment; filename="' basename($file'"');
  304. 304:         return $this->attachments[$part;
  305. 305:     }
  306. 306:  
  307. 307:  
  308. 308:  
  309. 309:     /**
  310. 310:      * Creates file MIME part.
  311. 311:      * @param  string 
  312. 312:      * @param  string 
  313. 313:      * @return string 
  314. 314:      */
  315. 315:     private function readFile($file$contentType)
  316. 316:     {
  317. 317:         if (!is_file($file)) {
  318. 318:             throw new FileNotFoundException("File '$file' not found.");
  319. 319:         }
  320. 320:         if (!$contentType && $info getimagesize($file)) {
  321. 321:             $contentType $info['mime'];
  322. 322:         }
  323. 323:         return file_get_contents($file);
  324. 324:     }
  325. 325:  
  326. 326:  
  327. 327:  
  328. 328:     /********************* building and sending ****************d*g**/
  329. 329:  
  330. 330:  
  331. 331:  
  332. 332:     /**
  333. 333:      * Sends e-mail.
  334. 334:      * @return void 
  335. 335:      */
  336. 336:     public function send()
  337. 337:     {
  338. 338:         $this->getMailer()->send($this->build());
  339. 339:     }
  340. 340:  
  341. 341:  
  342. 342:  
  343. 343:     /**
  344. 344:      * Sets the mailer.
  345. 345:      * @param  IMailer 
  346. 346:      * @return Mail  provides a fluent interface
  347. 347:      */
  348. 348:     public function setMailer(IMailer $mailer)
  349. 349:     {
  350. 350:         $this->mailer $mailer;
  351. 351:         return $this;
  352. 352:     }
  353. 353:  
  354. 354:  
  355. 355:  
  356. 356:     /**
  357. 357:      * Returns mailer.
  358. 358:      * @return IMailer 
  359. 359:      */
  360. 360:     public function getMailer()
  361. 361:     {
  362. 362:         if ($this->mailer === NULL{
  363. 363:             Framework::fixNamespace(self::$defaultMailer);
  364. 364:             $this->mailer is_object(self::$defaultMailerself::$defaultMailer new self::$defaultMailer;
  365. 365:         }
  366. 366:         return $this->mailer;
  367. 367:     }
  368. 368:  
  369. 369:  
  370. 370:  
  371. 371:     /**
  372. 372:      * Builds e-mail.
  373. 373:      * @return void 
  374. 374:      */
  375. 375:     protected function build()
  376. 376:     {
  377. 377:         $mail clone $this;
  378. 378:         $hostname isset($_SERVER['HTTP_HOST']$_SERVER['HTTP_HOST'isset($_SERVER['SERVER_NAME']$_SERVER['SERVER_NAME''localhost';
  379. 379:         $mail->setHeader('Message-ID''<' md5(uniqid(''TRUE)) "@$hostname>");
  380. 380:  
  381. 381:         $mail->buildHtml();
  382. 382:         $mail->buildText();
  383. 383:  
  384. 384:         $cursor $mail;
  385. 385:         if ($mail->attachments{
  386. 386:             $tmp $cursor->setContentType('multipart/mixed');
  387. 387:             $cursor $cursor->addPart();
  388. 388:             foreach ($mail->attachments as $value{
  389. 389:                 $tmp->addPart($value);
  390. 390:             }
  391. 391:         }
  392. 392:  
  393. 393:         if ($mail->html != NULL// intentionally ==
  394. 394:             $tmp $cursor->setContentType('multipart/alternative');
  395. 395:             $cursor $cursor->addPart();
  396. 396:             $alt $tmp->addPart();
  397. 397:             if ($mail->inlines{
  398. 398:                 $tmp $alt->setContentType('multipart/related');
  399. 399:                 $alt $alt->addPart();
  400. 400:                 foreach ($mail->inlines as $name => $value{
  401. 401:                     $tmp->addPart($value);
  402. 402:                 }
  403. 403:             }
  404. 404:             $alt->setContentType('text/html'$mail->charset)
  405. 405:                 ->setEncoding(preg_match('#[\x80-\xFF]#'$mail->htmlself::ENCODING_8BIT self::ENCODING_7BIT)
  406. 406:                 ->setBody($mail->html);
  407. 407:         }
  408. 408:  
  409. 409:         $text $mail->getBody();
  410. 410:         $mail->setBody(NULL);
  411. 411:         $cursor->setContentType('text/plain'$mail->charset)
  412. 412:             ->setEncoding(preg_match('#[\x80-\xFF]#'$textself::ENCODING_8BIT self::ENCODING_7BIT)
  413. 413:             ->setBody($text);
  414. 414:  
  415. 415:         return $mail;
  416. 416:     }
  417. 417:  
  418. 418:  
  419. 419:  
  420. 420:     /**
  421. 421:      * Builds HTML content.
  422. 422:      * @return void 
  423. 423:      */
  424. 424:     protected function buildHtml()
  425. 425:     {
  426. 426:         if ($this->html instanceof ITemplate{
  427. 427:             $this->html->mail $this;
  428. 428:             if ($this->basePath === NULL && $this->html instanceof IFileTemplate{
  429. 429:                 $this->basePath dirname($this->html->getFile());
  430. 430:             }
  431. 431:             $this->html $this->html->__toString(TRUE);
  432. 432:         }
  433. 433:  
  434. 434:         if ($this->basePath !== FALSE{
  435. 435:             $cids array();
  436. 436:             preg_match_all('#(src\s*=\s*|background\s*=\s*|url\()(["\'])(?![a-z]+:|[/\\#])(.+?)\\2#i'$this->html$matchesPREG_SET_ORDER PREG_OFFSET_CAPTURE);
  437. 437:             foreach (array_reverse($matchesas $m)    {
  438. 438:                 $file rtrim($this->basePath'/\\''/' $m[3][0];
  439. 439:                 $cid isset($cids[$file]$cids[$file$cids[$filesubstr($this->addEmbeddedFile($file)->getHeader("Content-ID")1-1);
  440. 440:                 $this->html substr_replace($this->html"{$m[1][0]}{$m[2][0]}cid:$cid{$m[2][0]}"$m[0][1]strlen($m[0][0]));
  441. 441:             }
  442. 442:         }
  443. 443:  
  444. 444:         if (!$this->getSubject(&& preg_match('#<title>(.+?)</title>#is'$this->html$matches)) {
  445. 445:             $this->setSubject(html_entity_decode($matches[1]ENT_QUOTES$this->charset));
  446. 446:         }
  447. 447:     }
  448. 448:  
  449. 449:  
  450. 450:  
  451. 451:     /**
  452. 452:      * Builds text content.
  453. 453:      * @return void 
  454. 454:      */
  455. 455:     protected function buildText()
  456. 456:     {
  457. 457:         $text $this->getBody();
  458. 458:         if ($text instanceof ITemplate{
  459. 459:             $text->mail $this;
  460. 460:             $this->setBody($text->__toString(TRUE));
  461. 461:  
  462. 462:         elseif ($text == NULL && $this->html != NULL// intentionally ==
  463. 463:             $text preg_replace('#<(style|script|head).*</\\1>#Uis'''$this->html);
  464. 464:             $text preg_replace('#<t[dh][ >]#i'" $0"$text);
  465. 465:             $text preg_replace('#[ \t\r\n]+#'' '$text);
  466. 466:             $text preg_replace('#<(/?p|/?h\d|li|br|/tr)[ >]#i'"\n$0"$text);
  467. 467:             $text html_entity_decode(strip_tags($text)ENT_QUOTES$this->charset);
  468. 468:             $this->setBody(trim($text));
  469. 469:         }
  470. 470:     }
  471. 471: