01.<?php
02./**
09. **/
10.class Email
11.{
12. /******************************************
13. *函数:sendMail
14. *作用:发送邮件函数
15. *输入:$to:收件人(一个收件人 a@123.com,多个 a@1.com,b@2.com)
16. ,$subject:主题,$message:内容,$from:发件人地址,$fromname:发件人名称,$ishtml:是否为html邮件(1/0),$cc:抄送的邮件,$bcc:暗送的邮件
17. *输出:true/false
18. *用法:$to:可为多个邮件,每个邮件用半角逗号隔开;$from为:somebody@example.com;$to和$subject中不能有换行的字符;
19. **
20. ******************************************
21. *--制作--日期--
22. *KuaiYigang@xingmo.com 2004-07-13 15:00
23. ******************************************
24. *--修改--日期--目的--
25. *
26. */
27. function sendMail($to, $subject, $message, $from, $fromname, $ishtml=1, $FILE='', $cc='', $bcc='', $charset = 'gbk', $type=4)
28. {
29. //mail方式发送
30. if($type == 1)
31. {
32. $tousers = array();
33. foreach(explode(',', $to) as $touser)
34. {
35. $tousers[] = preg_match('/^(.+?) \$/',$touser, $temp) ? ($fromname ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= " : $temp[2]) : $touser;
36. }
37. $to = implode(',', $tousers);
38. $subject = '=?'.$charset.'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', $subject))).'?=';
39. $message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message)))))));
40. //$from = $from == '' ? '=?'.$charset.'?B?'.base64_encode($fromname)."?= " : (preg_match('/^(.+?) \$/',$from, $temp) ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= " : $from);
41. $from = preg_match('/^(.+?) \$/',$from, $temp) ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= " : '=?'.$charset.'?B?'.base64_encode($fromname)."?= ";
42.
43. if ($ishtml == 1)
44. {
45. if($FILE['name'] <> '')
46. {
47. //附件
48. $file = $FILE['tmp_name'];
49. // 定义分界线
50. $boundary = uniqid( "");
51. $headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
52. $headers .= "From:$from\r\n";
53. //确定上传文件的MIME类型
54. if($FILE['type'])
55. $mimeType = $FILE['type'];
56. else
57. $mimeType ="application/unknown";
58. //文件名
59. $fileName = $FILE['name'];
60.
61. // 打开文件
62. $fp = fopen($file, "r");
63. // 把整个文件读入一个变量
64. $read = fread($fp, filesize($file));
65. //我们用base64方法把它编码
66. $read = base64_encode($read);
67. //把这个长字符串切成由每行76个字符组成的小块
68. $read = chunk_split($read);
69. //现在我们可以建立邮件的主体
70. $body = "--$boundary
71. Content-type: text/plain; charset=iso-8859-1
72. Content-transfer-encoding: 8bit
73. $message
74. --$boundary
75. Content-type: $mimeType; name=$fileName
76. Content-disposition: attachment; filename=$fileName
77. Content-transfer-encoding: base64
78. $read
79. --$boundary--";
80. //发送邮件
81. $mail = mail($to, $subject,$body,$headers);
82. return $mail;
83. }
84. else
85. {
86. /* To send HTML mail, you can set the Content-type header. */
87. $headers = "MIME-Version: 1.0\r\n";
88. $headers .= "Content-type: text/html; charset=".$charset."\r\n";
89. /* additional headers */
90. $headers .= "From: ".$from."\r\n";
91. $headers .= "Cc: ".$cc."\r\n";
92. $headers .= "Bcc: ".$bcc."\r\n";
93.
94. $headers .= "Content-Transfer-Encoding: base64\r\n";
95.
96. /* and now mail it */
97. $mail = @mail($to, $subject, $message, $headers);
98. return $mail;
99. }
100. }
101. else
102. {
103. /*
104. $mail = mail($to, $subject, $message,
105. "From: ".$from."\r\n"
106. ."Reply-To: ".$from."\r\n"
107. ."X-Mailer: PHP/" . phpversion());
108.
109. return $mail;
110. */
111. /* To send HTML mail, you can set the Content-type header. */
112. $headers = "MIME-Version: 1.0\r\n";
113. $headers .= "Content-type: text/plain; charset=".$charset."\r\n";
114. /* additional headers */
115. $headers .= "From: ".$from."\r\n";
116. $headers .= "Cc: ".$cc."\r\n";
117. $headers .= "Bcc: ".$bcc."\r\n";
118.
119. $headers .= "Content-Transfer-Encoding: base64\r\n";
120.
121. /* and now mail it */
122. $mail = @mail($to, $subject, $message, $headers);
123. return $mail;
124. }
125. }
126.
127. //sendmail
128. if($type == 2)
129. {
130. if ( $from == '')
131. {
132. $from = '163邮箱帐号';
133. }
134. if ( $ishtml == 1 )
135. {
136. $header = "MIME-Version: 1.0\r\n";
137. $header .= "Content-type: text/html; charset=gb2312\r\n";
138. $header .= "From: ".$from."\r\n";
139. $header .= "Subject: ".$subject."\r\n";
140. $header .= "Cc: \r\n";
141. $header .= "Bcc: \r\n";
142. $header .= "Bcc: X-Mailer: PHP/opendesktopmailer\r\n";
143. }
144. else
145. {
146. $header = "Return-Path: ".$from."\r\n";
147. $header .= "From: ".$from."\r\n";
148. $header .= "Cc: \r\n";
149. $header .= "Bcc: \r\n";
150. $header .= "Bcc: X-Mailer: PHP/opendesktopmailer\r\n";
151. }
152. if ( !@$mailok = popen("/usr/sbin/sendmail ".$to, "w") )
153. {
154. return false;
155. }
156. fputs($mailok, $header);
157. fputs($mailok, $message);
158.
159. $result = pclose($mailok) >> 8 & 0xFF;
160. if($result != 0)
161. {
162. return false;
163. }
164.
165. return true;
166. }
167.
168. //SMTP
169. if($type == 3)
170. {
171. $host = 'smtp.163.com';
172. $username = '163邮箱帐号';
173. $password = '163邮箱密码';
174.
175. $res = $this -> sendSmtpMail($to,$subject,$message,$from, $fromname, $host, $username, $password, $ishtml);
176. return $res ? true :false;
177. }
178.
179. //phpmailer
180. if($type == 4)
181. {
182. $res = $this->sendPhpMail($to,$subject,$message,$from, $fromname, $ishtml, $FILE, $cc,$bcc, $charset);
183. return $res ? true :false;
184. }
185. }
186.
187.
188. function sendSmtpMail($to,$subject,$message,$from, $fromname, $host, $username, $password, $ishtml=0)
189. {
190. require '../inc/class.phpmailer.php';
191. $mail = new PHPMailer();
192.
193. $mail->IsSMTP(); // send via SMTP
194. $mail->Host = $host; // SMTP servers
195. $mail->SMTPAuth = true; // turn on SMTP authentication