SimpleMail.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace SimpleMail;
  3. /**
  4. * Powerful Class to send emails with PHP.
  5. *
  6. * @package SimpleMail
  7. * @author Pedro Rogério <pinceladasdaweb@hotmail.com>
  8. * @copyright 2016 Pedro Rogério <pinceladasdaweb@hotmail.com>
  9. */
  10. class SimpleMail
  11. {
  12. protected $to;
  13. protected $from;
  14. protected $sender;
  15. protected $subject;
  16. protected $html;
  17. public function setTo($to)
  18. {
  19. $this->to = $to;
  20. }
  21. public function setFrom($from)
  22. {
  23. $this->from = $from;
  24. }
  25. public function setSender($sender)
  26. {
  27. $this->sender = $sender;
  28. }
  29. public function setSubject($subject)
  30. {
  31. $this->subject = $subject;
  32. }
  33. public function setHtml($html)
  34. {
  35. $this->html = $html;
  36. }
  37. public function send()
  38. {
  39. $boundary = '----=_NextPart_' . md5(time());
  40. $header = 'MIME-Version: 1.0' . PHP_EOL;
  41. $header .= 'To: <' . $this->to . '>' . PHP_EOL;
  42. $header .= 'Date: ' . date('D, d M Y H:i:s O') . PHP_EOL;
  43. $header .= 'From: =?UTF-8?B?' . base64_encode($this->sender) . '?= <' . $this->from . '>' . PHP_EOL;
  44. $header .= 'Reply-To: =?UTF-8?B?' . base64_encode($this->sender) . '?= <' . $this->from . '>' . PHP_EOL;
  45. $header .= 'Return-Path: ' . $this->from . PHP_EOL;
  46. $header .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
  47. $header .= 'X-Priority: 3' . PHP_EOL;
  48. $header .= 'MIME-Version: 1.0' . PHP_EOL;
  49. $header .= 'Message-ID: ' . '<' . md5(uniqid(rand(), true )) . '@' . $_SERVER[ 'HTTP_HOST' ] . '>' . PHP_EOL;
  50. $header .= 'Content-Type: multipart/mixed; boundary="' . $boundary . '"' . PHP_EOL . PHP_EOL;
  51. $message = '--' . $boundary . PHP_EOL;
  52. $message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . PHP_EOL . PHP_EOL;
  53. $message .= '--' . $boundary . '_alt' . PHP_EOL;
  54. $message .= 'Content-Type: text/plain; charset="utf-8"' . PHP_EOL;
  55. $message .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL . PHP_EOL;
  56. $message .= '--' . $boundary . '_alt' . PHP_EOL;
  57. $message .= 'Content-Type: text/html; charset="utf-8"' . PHP_EOL;
  58. $message .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL . PHP_EOL;
  59. $message .= $this->html . PHP_EOL;
  60. $message .= '--' . $boundary . '_alt--' . PHP_EOL;
  61. ini_set('sendmail_from', $this->from);
  62. mail($this->to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header);
  63. }
  64. }