Advertisement
Guest User

script

a guest
Dec 25th, 2018
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <?php
  2. /**
  3. * PHP lightMail
  4. *
  5. * Send mail in a simple quick way with the PHP mail() function
  6. *
  7. * @package lightMail
  8. * @author Nildo Pontes <nildo.pontes@bol.com.br>
  9. * @version 1.0
  10. * @license GNU General Public License v3.0
  11. *
  12. */
  13. class lightMail{
  14. public $sender;
  15. public $nameSender;
  16. public $receiver;
  17. public $subject;
  18. public $message;
  19. public $attachment = '';
  20. public $boundary = '_myBoundaryLightMail_';
  21. public $headers;
  22. public function __construct($sender, $receiver, $subject, $nameSender){
  23. $this->sender = $sender;
  24. $this->receiver = $receiver;
  25. $this->subject = $subject;
  26. $this->nameSender = $nameSender;
  27. }
  28. public function send(){
  29. if(strlen($this->headers) == 0){
  30. if(strlen($this->attachment) != 0){
  31. $this->headers = "From: ".$this->nameSender." <".$this->sender.">\r\nReply-To: <".$this->sender.">\r\nReturn-Path: ".$this->sender."\r\nMIME-Version: 1.0\r\nX-Priority: 3\r\nContent-type: multipart/mixed;\r\n boundary=\"".$this->boundary."\";\r\n charset=utf-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\n\r\n";
  32. }else{
  33. $this->headers = "From: ".$this->nameSender." <".$this->sender.">\r\nReply-To: <".$this->sender.">\r\nReturn-Path: ".$this->sender."\r\nMIME-Version: 1.0\r\nX-Priority: 3\r\nContent-type: text/html;\r\n charset=utf-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
  34. }
  35. }
  36. if(strlen($this->attachment) > 0){
  37. $this->message = "--".$this->boundary."\r\nContent-Type: text/html;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".$this->message;
  38. $this->message .= $this->attachment."--".$this->boundary."--";
  39. }
  40. return mail($this->receiver, $this->subject, $this->message, $this->headers);
  41. }
  42. public function addAttachment($fileName){
  43. $mime = mime_content_type($fileName);
  44. $fp = fopen($fileName, 'rb');
  45. $data = fread($fp, filesize($fileName));
  46. $data = base64_encode($data);
  47. fclose($fp);
  48. $data = chunk_split($data);
  49. $cache = "--".$this->boundary."\r\nContent-Type: ".$mime.";\r\n charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment;\r\n filename=\"".array_reverse(explode('/', $fileName))[0]."\"\r\nfilename: ".array_reverse(explode('/', $fileName))[0]."\r\n\r\n".$data."\r\n\r\n";
  50. $this->attachment .= $cache;
  51. }
  52. public function removeAllAttachment(){
  53. $this->attachment = '';
  54. }
  55. public function setMessage($msg){
  56. $this->message = $msg."\r\n";
  57. }
  58. public function modifyBoundary($newBundary){
  59. $this->boundary = $newBundary;
  60. }
  61. public function modifyHeaders($newHeaders){
  62. $this->headers = $newHeaders;
  63. }
  64. }
  65. /*
  66. *
  67. * Usage:
  68. *
  69. * include('lightMail.php');
  70. *
  71. * $sender = 'example@mail.com';
  72. * $nameSender = 'Name Sender';
  73. * $receiver = 'receiver@mail.com';
  74. * $subject = 'Subject example';
  75. *
  76. * $mail = new lightMail($sender, $receiver, $subject, $nameSender);
  77. * $mail->setMessage('<p>My HTML message.</p>');
  78. * $mail->addAttachment('file1.mp3');
  79. * $mail->addAttachment('file2.mp3');
  80. * $rtr = $mail->send();
  81. *
  82. * if($rtr){
  83. * echo 'Sucess!\n';
  84. * }else{
  85. * echo 'Error\n';
  86. * }
  87. *
  88. */
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement