Use the mail function in php to send emails with attachments

Source: Internet
Author: User
Tags eol
Mail function, mail syntax: mail (to, subject, message, headers, parameters) to specifies the subject of the email specified by the mail recipient subject. This parameter cannot include the... mail function to send emails.

Syntax: mail (to, subject, message, headers, parameters)

To specifies the recipient of the email

Subject specifies the subject of the email. This parameter cannot contain any line breaks

Message specifies the message to be sent

Headers specifies additional headers, such as From, Cc, and Bcc.

Parameters specifies additional parameters for the sendmail program.

The main problem encountered was garbled code. at the beginning, some clients were good at receiving emails (such as QQ mail, which is estimated to contain the automatic identification code). some of them were not foxmail, and the ipad showed garbled code, set the mail headers correctly. The following is a perfect example without garbled characters.

Configuring the PHP. ini file in php involves two steps:

1. first, find the place where you put all the PHP, Apache, and MySQL files. in the PHP folder, you can find a file: php. ini, open it, find the mail function, and change the original configuration code to the following (only for windows ):

[mail function] ; For Win32 only. SMTP =smtp.sohu.com     mtp_port=25 ; For Win32 only.

Sendmail_from = fill in the full name of your email.

The setting here is the sohu email server. if you use a 163 email address, it is set to smtp.163.com.

2. search for php. ini, select the php that is not a shortcut. ini, which should be in C/WINDOWS, open it, modify it as above, save it, remember to restart the Apache server after setting, and then the mail () function can be used, the sample code is as follows:

 

The above function cannot contain attachments. the code is as follows:

 GetEOT (); // Generate the end line break $ this-> getUniq_id (); $ this-> header = ''; $ this-> attach = ''; $ this-> cc = ''; $ this-> msg ='';} public function getFromaddr () {return $ this-> fromaddr;} public function setFromaddr ($ fromaddr) {$ this-> fromaddr = $ fromaddr;} public function getTopic () {return $ this-> topic;} public function getToaddr () {return $ this-> toaddr ;} public function getCc () {return $ this-> cc;} pub Lic function getContent () {return $ this-> content;} public function getAttach () {return $ this-> attach;} public function setTopic ($ topic) {$ this-> topic = mb_convert_encoding (trim ($ topic), 'utf-8', 'auto');} public function setToaddr ($ toaddr) {$ this-> toaddr = trim ($ toaddr);} public function setCc ($ cc) {$ this-> cc = trim ($ cc );} public function setContent ($ content) {$ this-> content = mb_conve Rt_encoding (trim ($ content), 'utf-8', 'auto');} public function setAttach ($ attach) {$ this-> attach = trim ($ attach);} public function getDomain () {return $ this-> domain;} public function setDomain ($ domain) {$ this-> domain = $ domain; // the input value is '@ domain.com'}/** set the linefeed according to the system type */private function getEOT () {if (strtoupper (substr (PHP_ OS, 0, 3) = 'win') {$ this-> eol = "rn";} elseif (strtoupper (subst R (PHP_ OS, 0, 3) = 'Mac') {$ this-> eol = "r ";} else {$ this-> eol = "n" ;}} private function getBoundary () {$ this-> boundary = '--'. substr (md5 (time (). rand (1000,200 0), 0, 16);} private function getUniq_id () {$ this-> uniqid = md5 (microtime (). time (). rand (1,100);} private function outputCommonHeader () {$ this-> header. = 'from :'. $ this-> fromaddr. $ this-> eol; // $ this-> header. = 'to :'. $ This-> toaddr. $ this-> eol; // $ this-> header. = 'Subject :'. $ this-> topic. $ this-> eol; $ this-> header. = 'message-ID: <'. $ this-> uniqid. $ this-> domain. '> '. $ this-> eol; $ this-> header. = 'Mime-Version: 100 '. $ this-> eol; $ this-> header. = 'reply-:'. $ this-> fromaddr. $ this-> eol; $ this-> header. = 'return-Path :'. $ this-> fromaddr. $ this-> eol; $ this-> header. = 'x-Mailer: Xmail system '. $ this-> eol; $ th Is-> header. = 'content-Disposition: inline '. $ this-> eol;} private function mime_content_type ($ f) {$ temp = trim (exec ('file-bi '. escapeshellarg ($ f); $ temp = preg_replace ('/s +/', '', $ temp); $ temp = explode ('', $ temp ); return $ temp [0];} // determines the mime type of the file./** it only contains CC */private function mailWithCC () {$ this-> header. = 'CC :'. $ this-> cc. $ this-> eol; $ this-> header. = 'content-type: text/html; chars Et = UTF-8 '. $ this-> eol; $ this-> header. = 'content-Transfer-Encoding: 8bit '. $ this-> eol; $ this-> msg = $ this-> content; if (mail ($ this-> toaddr, $ this-> topic, $ this-> msg, $ this-> header) {return 1;} else {return 0 ;}/ ** $ filedir must be an absolute address */private function attachmentToBase64 ($ filedir) {$ this-> filename = basename ($ filedir); @ $ fopen = fopen ($ filedir, 'r'); $ str = fread ($ fopen, filesize ($ filedir )); $ Str = base64_encode ($ str); $ this-> filestr = $ str;}/** with only attachments */private function mailWithAttach () {$ this-> attachmentToBase64 ($ this-> attach); $ this-> header. = 'content-type: multipart/mixed; boundary = "'. str_replace ('--', '', $ this-> boundary ). '"'. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> boundary. $ this-> eol; $ this-> msg. = 'content-Type: text/html; charset = utf-8 '. $ this-> eol; $ this-> m Sg. = 'content-Disposition: inline '. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> content. $ this-> eol; $ this-> msg. = $ this-> boundary. $ this-> eol; $ this-> msg. = 'content-Type :'. $ this-> mime_content_type ($ this-> attach ). $ this-> eol; $ this-> msg. = 'content-Disposition: attachment; filename = "'. $ this-> filename. '"'. $ this-> eol; $ this-> msg. = 'content-Transfer-Encoding: base64 '. $ this-> eol; $ this-> Msg. = $ this-> eol. $ this-> filestr. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> boundary. '--'; if (mail ($ this-> toaddr, $ this-> topic, $ this-> msg, $ this-> header) {return 1 ;} else {return 0 ;}/ ** with attachments and CC */private function mailAll () {$ this-> attachmentToBase64 ($ this-> attach ); $ this-> header. = 'CC :'. $ this-> cc. $ this-> eol; $ this-> header. = 'content-type: multipart/mixed; boundary = "'. str _ Replace ('--', '', $ this-> boundary ). '"'. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> boundary. $ this-> eol; $ this-> msg. = 'content-Type: text/html; charset = utf-8 '. $ this-> eol; $ this-> msg. = 'content-Disposition: inline '. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> content. $ this-> eol; $ this-> msg. = $ this-> boundary. $ this-> eol; $ this-> msg. = 'content-Type :'. $ this-> mime_content_type ($ this-> Ttach ). $ this-> eol; $ this-> msg. = 'content-Disposition: attachment; filename = "'. $ this-> filename. '"'. $ this-> eol; $ this-> msg. = 'content-Transfer-Encoding: base64 '. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> filestr. $ this-> eol; $ this-> msg. = $ this-> eol. $ this-> boundary. '--'; if (mail ($ this-> toaddr, $ this-> topic, $ this-> msg, $ this-> header) {return 1 ;} else {return 0 ;}/ ** without CC and attachment */Private function mailSimple () {$ this-> header. = 'content-type: text/html; charset = UTF-8 '. $ this-> eol; $ this-> header. = 'content-Transfer-Encoding: 8bit '. $ this-> eol; $ this-> msg = $ this-> content; if (mail ($ this-> toaddr, $ this-> topic, $ this-> msg, $ this-> header) {return 1;} else {return 0;} public function send () {if (emptyempty ($ this-> attach) & amp; emptyempty ($ this-> cc) {$ this-> outputComm OnHeader (); return $ this-> mailSimple ();} else if (emptyempty ($ this-> attach) {$ this-> outputCommonHeader (); return $ this-> mailWithCC ();} else if (emptyempty ($ this-> cc) {$ this-> outputCommonHeader (); $ this-> getBoundary (); // Generate boundary return $ this-> mailWithAttach ();} else if (! Emptyempty ($ this-> toaddr )&&! Emptyempty ($ this-> topic )&&! Emptyempty ($ this-> cc )&&! Emptyempty ($ this-> content )&&! Emptyempty ($ this-> attach) {$ this-> outputCommonHeader ();

$ This-> getBoundary (); // A boundary is generated when an attachment exists.

Return $ this-> mailAll ();

}

}

}

?>

Sample code. some variables require the context. the code is as follows:

 setToaddr($this->temp['receipt_address']);$m->setTopic($this->temp['mail_title']);$m->setContent($this->temp['mail_content']);$m->setFromaddr($_SESSION['user']['name'] . ' <' . $_SESSION['user']['name'] . '@' . SystemDomain . '>');$m->setDomain('@' . SystemDomain);$m->setCc($this->temp['cc_address']);$m->setAttach(PATH . '/temp/' . $this->temp['attachment_file']);$m->send();?>

Advantage: easy to use a simple function

Disadvantage: php. ini is required to support this function. if some servers do not support this function and cannot change the environment, it will not work and will always be unstable, and sometimes it will not work.


Address:

Reprinted at will, but please attach the article address :-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.