Created
August 29, 2012 19:36
-
-
Save mki/3517687 to your computer and use it in GitHub Desktop.
Class for mailing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $objMail = new Lib_Sent(); | |
| $objMail->to = array('email кому отсылаем письмо'); | |
| $objMail->from = 'email от кого будет письмо'; | |
| $objMail->subject = 'Заголовок письма'; | |
| $objMail->body = 'Текст письма.'; | |
| $objMail->add_attachment("Содержание атача. Предварительно нужно считать из файла", | |
| "название файла", "application/octet-stream"); | |
| $objMail->send(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Lib_Sent { | |
| var $parts; | |
| var $to; | |
| var $from; | |
| var $headers; | |
| var $subject; | |
| var $body; | |
| function Lib_Sent() { | |
| $this->parts = array(); | |
| $this->to = ""; | |
| $this->from = ""; | |
| $this->subject = ""; | |
| $this->body = ""; | |
| $this->headers = ""; | |
| } | |
| function add_attachment($message, $name = "", | |
| $ctype = "application/octet-stream", $cid='', $encode='') { | |
| $this->parts [] = array ( | |
| "ctype" => $ctype, | |
| "message" => $message, | |
| "encode" => $encode, | |
| "name" => $name, | |
| "cid" => $cid | |
| ); | |
| } | |
| function build_message($part) { | |
| $message = $part["message"]; | |
| if ($part["ctype"] == "image/jpeg") { | |
| $message = chunk_split(base64_encode($message)); | |
| $encoding = "base64"; | |
| $hdr = "Content-Type: ".$part["ctype"]."\n"; | |
| $hdr .= "Content-Transfer-Encoding: $encoding\n"; | |
| $hdr .= ($part["name"]? "Content-Disposition: attachment; filename = \"" | |
| .$part["name"]."\"\n" : "\n"); | |
| $hdr .= "Content-ID: <".$part["cid"].">\n"; | |
| } | |
| else { | |
| $hdr = "Content-Type: text/html; charset=utf-8\n"; | |
| $hdr.= "Content-Transfer-Encoding: Quot-Printed\n\n"; | |
| } | |
| $hdr.= "\n$message\n"; | |
| return $hdr; | |
| } | |
| function build_multipart() { | |
| $boundary = "--b".md5(uniqid(time())); | |
| $multipart = "Content-Type: multipart/mixed; boundary=\"$boundary\"\n\n--$boundary"; | |
| for($i = sizeof($this->parts)-1; $i>=0; $i--) { | |
| $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary"; | |
| } | |
| return $multipart.= "--\n"; | |
| } | |
| function send() { | |
| $mime = ""; | |
| if (!empty($this->from)) { | |
| $mime .= "From: ".$this->from. "\n"; | |
| } | |
| if (!empty($this->headers)) { | |
| $mime .= $this->headers. "\n"; | |
| } | |
| if (!empty($this->body)) { | |
| $this->add_attachment($this->body, "", "text/html;charset=utf-8"); | |
| } | |
| $mime .= "MIME-Version: 1.0\n".$this->build_multipart(); | |
| foreach ($this->to as $value) { | |
| mail($value, $this->subject, "", $mime); | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment