系统需要安装 sendmail,并启动 sendmail 服务
开启httpd发送邮件支持:
setsebool -P httpd_can_sendmail 1
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
<?php // email.class.php, 发送邮件,支持文本信息和附件 class CMailMessage { var $subject; var $addr_to; var $text_msg; var $text_encoded_files; var $text_tail; var $mime_headers; var $mime_boundary = ""; var $smtp_headers; function CMailMessage($from, $to, $subject, $msg) { $this->mime_boundary = $this->getRandomBoundary(1); $this->subject = "=?utf-8?B?" . base64_encode($subject) . "?="; $this->addr_to = $to; $this->smtp_headers = $this->write_smtpheaders($from); $this->text_msg = $this->write_msg($msg); $this->mime_headers = $this->write_mimeheaders(); $this->text_tail = $this->write_tail(); $this->text_encoded_files = ""; } function getRandomBoundary($offset=0) { srand(time()+$offset); return("----==".strtoupper(md5(rand()))) . "==_"; } function attach_content($contents, $downfilename, $mimetype) { $encoded = chunk_split(base64_encode($contents)); $out = "--" . $this->mime_boundary . "\n"; $out .= "Content-type: " . $mimetype . ";\n"; $out .= " name=\"$downfilename\";\n"; $out .= "Content-Transfer-Encoding: base64\n"; $out .= "Content-disposition: attachment;\n"; $out .= " filename=\"$downfilename\"\n\n"; $out .= $encoded . "\n"; $this->text_encoded_files .= $out; } function attach_file($sourcefile,$downfilename,$mimetype) { if (is_readable($sourcefile)) { $fd = fopen($sourcefile, "r"); $contents = fread($fd, filesize($sourcefile)); fclose($fd); $out = $this->attach_content($contents, $downfilename, $mimetype); $this->text_encoded_files .= $out; } } function write_smtpheaders($addr_from) { $out = "From: $addr_from\n"; $out .= "Reply-To: $addr_from\n"; $out .= "X-Mailer: PHP3\n"; $out .= "X-Sender: $addr_from\n"; return $out; } function write_mimeheaders() { $out = "MIME-version: 1.0\n"; $out .= "Content-type: multipart/alternative;\n"; $out .= " boundary=\"$this->mime_boundary\"\n"; $out .= "Content-Transfer-Encoding: 8Bit\n"; $out .= "This is a multi-part message in MIME format.\n"; return $out; } function write_msg($msgtext) { $out = "--" . $this->mime_boundary . "\n"; $out .= "Content-Type: text/plain;\n"; $out .= " charset=\"utf-8\"\n"; $out .= "Content-Transfer-Encoding: base64\n\n"; $encoded .= chunk_split(base64_encode($msgtext)); $out .= $encoded . "\n"; return $out; } function write_tail() { $out = "--" . $this->mime_boundary . "--\n"; return $out; } function sendMessage() { $headers = $this->smtp_headers . $this->mime_headers; $message = $this->text_msg . $this->text_encoded_files . $this->text_tail; var_dump($this->addr_to); var_dump($this->subject); var_dump($headers); var_dump($message); mail($this->addr_to, $this->subject, $message, $headers); } } ?> |
使用方法如下:
第1种情况,只发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php require_once('email.class.php'); //发件人 $from = 'support@aqwu.net'; //收件人 $to = 'zhangxj@aqwu.net'; //主題 $subject = "test1"; $message = "这是一条中文信息,this is a english message."; $mailMsg = new CMailMessage($from,$to,$subject,$message); $mailMsg->sendMessage(); ?> |
第2种情况,发送消息和文件,文件以附件形式来发送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php require_once('email.class.php'); //发件人 $from = 'support@aqwu.net'; //收件人 $to = 'zhangxj@aqwu.net'; //主題 $subject = "test2"; $message = "这是一条中文信息,this is a english message."; $sourcefile = 'test.exe'; $downfilename = "test.exe"; $mimetype = "application/octet-stream"; $mailMsg = new CMailMessage($from,$to,$subject,$message); $mailMsg->attach_file($sourcefile,$downfilename,$mimetype); $mailMsg->sendMessage(); ?> |
第3种情况,发送消息和内容,内容以附件形式来发送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php require_once('email.class.php'); //发件人 $from = 'support@aqwu.net'; //收件人 $to = 'zhangxj@aqwu.net'; //主題 $subject = "test3"; $message = "这是一条中文信息,this is a english message."; $contents = 'this is a test file info'; $downfilename = "test.txt"; $mimetype = "application/octet-stream"; $mailMsg = new CMailMessage($from,$to,$subject,$message); $mailMsg->attach_content($contents,$downfilename,$mimetype); $mailMsg->sendMessage(); ?> |
第4种情况,发送消息和多个文件和多个内容,文件和内容均以附件形式来发送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php require_once('email.class.php'); //发件人 $from = 'support@aqwu.net'; //收件人 $to = 'zhangxj@aqwu.net'; //主題 $subject = "test4"; $message = "这是一条中文信息,this is a english message."; $contents = 'this is a test file info'; $downfilename = "test.txt"; $mimetype = "application/octet-stream"; $mailMsg = new CMailMessage($from,$to,$subject,$message); $mailMsg->attach_content($contents,$downfilename,$mimetype); $sourcefile = 'test.exe'; $downfilename = "test.exe"; $mimetype = "application/octet-stream"; $mailMsg->attach_file($sourcefile,$downfilename,$mimetype); $mailMsg->sendMessage(); ?> |