Advertisement
Guest User

Untitled

a guest
Nov 9th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. <?php
  2. /**
  3. * MailSender
  4. * Per utilitzar PHPMailer de forma molt més senzilla i
  5. * utilitzant plantilles HTML.
  6. *
  7. * Dependencies:
  8. * - PHPMailer
  9. *
  10. * @version 0.24
  11. * @license MIT
  12. * @author Mario M. <gameofender@gmail.com>
  13. */
  14. class MailSender {
  15.  
  16. var $mailer;
  17. var $template;
  18. var $body = '';
  19. var $body_alt = '';
  20.  
  21. public function __construct($host, $username, $password, $isHTML = true) {
  22. $mail = new PHPMailer;
  23. $mail->isSMTP(); // Set mailer to use SMTP
  24. $mail->Host = $host; // Specify main and backup SMTP servers
  25. $mail->SMTPAuth = true; // Enable SMTP authentication
  26. $mail->Username = $username; // SMTP username
  27. $mail->Password = $password; // SMTP password
  28. $mail->isHTML($isHTML); // Set email format to HTML
  29. $mail->CharSet = 'UTF-8';
  30. // Save PHPMailer Instance
  31. $this->mailer = $mail;
  32. }
  33.  
  34. /**
  35. * Template .html path
  36. * @param String $path
  37. */
  38. public function setTemplateURL($path) {
  39. $this->template = $path;
  40. $this->body = file_get_contents($this->template);
  41. }
  42.  
  43. public function setBodyAlt($string) {
  44. $this->body_alt = $string;
  45. }
  46.  
  47. /**
  48. * Remplace mail variables inside template with {{var}} notation.
  49. * @return Array
  50. */
  51. public function compose($args) {
  52. if(is_array($args)) {
  53. foreach($args as $key => $value){
  54. if(!is_array($value)) $this->body = preg_replace('/{{'.$key.'}}/', $value, $this->body);
  55. }
  56. }
  57. }
  58.  
  59. /**
  60. * Send!
  61. * @param [type] $from [description]
  62. * @param [type] $to [description]
  63. * @param [type] $subject [description]
  64. * @return [type] [description]
  65. */
  66. function send($from, $to, $subject) {
  67.  
  68. $this->mailer->Subject = $subject;
  69. $this->mailer->Body = $this->body;
  70.  
  71. if(!empty($this->body_alt)) $this->mailer->AltBody = $this->body_alt;
  72. if(is_array($from)) $this->mailer->setFrom($from[0], $from[1]);
  73. else $this->mailer->setFrom($from);
  74.  
  75. if(!is_array($to)) $this->mailer->addAddress($to);
  76. else {
  77. foreach ($to as $email => $name) {
  78. $this->mailer->addBcc($email, $name);
  79. }
  80. }
  81.  
  82. return $this->mailer->send();
  83. }
  84. }
  85. ?>
  86.  
  87.  
  88. <?php
  89. //////////////////////////////////////////////////////////////////////////////////////
  90. // How to use:
  91. $mailer = new MailSender('localhost', 'smtp_user', 'smtp_password');
  92.  
  93. // Set .html template
  94. $mailer->setTemplateURL('views/mails/message.html');
  95.  
  96. // Remplace strings patterns {{var}} on HTML template
  97. // ex.: `<h2>{{name}}</h2>` ===> `<h2>Mario M.</h2>`
  98. $mailer->compose(array(
  99. name => 'Mario M.',
  100. sex => 'Mr.',
  101. email => 'gameofender@gmail.com'
  102. ));
  103.  
  104. // Send email with from, to and Subject.
  105. $sended = $mailer->send(
  106. array('from@email.com', 'From name'),
  107. 'to@email.com',
  108. 'subject'
  109. );
  110. /////////////////////////////////////////////////////////////////////////////////////////
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement