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