PHPメール操作クラス

WBOY
リリース: 2016-07-25 08:42:28
オリジナル
914 人が閲覧しました
  1. class smtp
  2. {
  3. var $smtp_port;
  4. var $time_out;
  5. var $host_name;
  6. var $log_file;
  7. var $relay_host;
  8. var $debug;
  9. var $auth;
  10. var $user;
  11. var $pass;
  12. var $sock;
  13. function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  14. {
  15. $this->debug = true;
  16. $this->smtp_port = $smtp_port;
  17. $this->relay_host = $relay_host;
  18. $this->time_out = 30;
  19. $this->auth = $auth;
  20. $this->user = $user;
  21. $this->pass = $pass;
  22. $this->host_name = "localhost";
  23. $this->log_file ="";
  24. $this->sock = FALSE;
  25. }
  26. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $Additional_headers = "")
  27. {
  28. $mail_from = $this->get_address($this->strip_comment($from));
  29. $body = ereg_replace("(^ |(rn))(\.)", "\1.\3", $body);
  30. $header .= "MIME-Version:1.0rn";
  31. if($mailtype=="HTML")
  32. {
  33. $header .= "Content-Type:text/htmlrn";
  34. }
  35. $header .= "To: ".$to."rn";
  36. if ($cc != "")
  37. {
  38. $header . = "Cc: ".$cc."rn";
  39. }
  40. $header .= "差出人: $from<".$from.">rn";
  41. $header .= "件名: ".$subject. "rn";
  42. $header .= $Additional_headers;
  43. $header .= "日付: ".date("r")."rn";
  44. $header .= "X-Mailer:By Redhat (PHP/". phpversion().")rn";
  45. list($msec, $sec) =explode(" ", microtime());
  46. $header .= "メッセージID: <".date("YmdHis", $秒).".".($msec*1000000).".".$mail_from.">rn";
  47. $TO =explode(",", $this->strip_comment($to));
  48. if ($cc != "") {
  49. $TO = array_merge($TO,explode(",", $this->strip_comment($cc)));
  50. }
  51. if ($bcc != "" ) {
  52. $TO = array_merge($TO,explode(",", $this->strip_comment($bcc)));
  53. }
  54. $sent = TRUE;
  55. foreach ($TO as $rcpt_to) {
  56. $rcpt_to = $this->get_address($rcpt_to);
  57. if (!$this->smtp_sockopen($rcpt_to)) {
  58. $this->log_write("エラー: ".$rcpt_to. "n");
  59. $sent = FALSE;
  60. 続行;
  61. }
  62. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  63. $this ->log_write("電子メールは <".$rcpt_to.">n");
  64. } else {
  65. $this->log_write("エラー: < に電子メールを送信できません。". $rcpt_to.">n");
  66. $sent = FALSE;
  67. }
  68. fclose($this->sock);
  69. $this->log_write("リモートホストnから切断されました");
  70. }
  71. echo "
    ";
  72. echo $header;
  73. return $sent;
  74. }
  75. /* プライベート関数 */
  76. function smtp_send($helo, $from, $to, $header, $body = "")
  77. {
  78. if (!$this->smtp_putcmd("HELO", $helo)) {
  79. return $this->smtp_error("HELO コマンドの送信");
  80. }
  81. #auth
  82. if($this-> auth){
  83. if (!$this->smtp_putcmd("AUTH LOGIN",base64_encode($this->user))) {
  84. return $this->smtp_error("HELO コマンドを送信中");
  85. }
  86. if (!$this->smtp_putcmd("",base64_encode($this->pass))) {
  87. return $this->smtp_error("HELO コマンドの送信中");
  88. }
  89. }
  90. #
  91. if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
  92. return $this->smtp_error("sending MAIL FROM command");
  93. }
  94. if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
  95. return $this->smtp_error("RCPT TO コマンドの送信中");
  96. }
  97. if (!$this->smtp_putcmd("DATA")) {
  98. return $this->smtp_error("DATA コマンドの送信");
  99. }
  100. if (!$this->smtp_message($ header, $body)) {
  101. return $this->smtp_error("メッセージ送信中");
  102. }
  103. if (!$this->smtp_eom()) {
  104. return $this->smtp_error("送信中) . [EOM]");
  105. }
  106. if (!$this->smtp_putcmd("QUIT")) {
  107. return $this->smtp_error("QUIT コマンドを送信中");
  108. }
  109. return TRUE;
  110. }
  111. function smtp_sockopen($address)
  112. {
  113. if ($this->relay_host == "") {
  114. return $this->smtp_sockopen_mx($address);
  115. } else {
  116. return $this-> smtp_sockopen_relay();
  117. }
  118. }
  119. function smtp_sockopen_relay()
  120. {
  121. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");
  122. $this-> sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  123. if (!($this->sock && $this->gt; smtp_ok​​())) {
  124. $this->log_write("エラー: リレー ホスト ".$this->relay_host."n") に接続できません;
  125. $this->log_write("エラー: ".$ errstr." (".$errno.")n");
  126. return FALSE;
  127. }
  128. $this->log_write("リレー ホスト ".$this->relay_host."n");
  129. return TRUE;;
  130. }
  131. function smtp_sockopen_mx($address)
  132. {
  133. $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
  134. if (!@ getmxrr($domain, $MXHOSTS)) {
  135. $this->log_write("エラー: MX "".$domain.""n");
  136. return FALSE;
  137. }
  138. foreach ($MXHOSTS as $host ) {
  139. $this->log_write("Trying to ".$host.":".$this->smtp_port."n");
  140. $this->sock = @fsockopen($host, $this ->smtp_port, $errno, $errstr, $this->time_out);
  141. if (!($this->sock && $this->smtp_ok​​())) {
  142. $this->log_write( "警告: mx ホスト ".$host."n に接続できません");
  143. $this->log_write("エラー: ".$errstr." (".$errno.")n");
  144. 続行;
  145. }
  146. $this->log_write("MX ホスト ".$host."n に接続しました");
  147. return TRUE;
  148. }
  149. $this->log_write("エラー: どの mx ホストにも接続できません (" .implode(", ", $MXHOSTS).")n");
  150. return FALSE;
  151. }
  152. function smtp_message($header, $body)
  153. {
  154. fputs($this->sock, $header." rn".$body);
  155. $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
  156. return TRUE;
  157. }
  158. function smtp_eom()
  159. {
  160. fputs($this->sock, "rn.rn");
  161. $this->smtp_debug(".[EOM]n");
  162. return $this->smtp_ok​​();
  163. }
  164. function smtp_ok​​()
  165. {
  166. $response = str_replace("rn", "", fgets($this->sock, 512));
  167. $this->smtp_debug($response."n");
  168. if (!ereg("^[23]", $response)) {
  169. fputs($this->sock, "QUITrn");
  170. fgets($this->sock, 512);
  171. $this-> ;log_write("エラー: リモート ホストが "".$response.""n");
  172. return FALSE;
  173. }
  174. return TRUE;
  175. }
  176. function smtp_putcmd($cmd, $arg = "")
  177. {
  178. if ($arg != "") {
  179. if($cmd=="") $cmd = $arg;
  180. else $cmd = $cmd。 ".$arg;
  181. }
  182. fputs($this->sock, $cmd."rn");
  183. $this->smtp_debug("> ".$cmd."n");
  184. return $this->smtp_ok​​();
  185. }
  186. function smtp_error($string)
  187. {
  188. $this->log_write("Error: ".$string.".n" 中にエラーが発生しました");
  189. return FALSE;
  190. }
  191. function log_write($message)
  192. {
  193. $this->smtp_debug($message);
  194. if ($this->log_file == "") {
  195. return TRUE;
  196. }
  197. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  198. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  199. $this->smtp_debug("警告: ログ ファイルを開けません "".$this->log_file.""n ");
  200. return FALSE;
  201. }
  202. flock($fp, LOCK_EX);
  203. fputs($fp, $message);
  204. fclose($fp);
  205. return TRUE;
  206. }
  207. functionstrip_comment($address)
  208. {
  209. $comment = "\([^()]*\)";
  210. while (ereg($comment, $address)) {
  211. $address = ereg_replace($comment, "", $address);
  212. }
  213. return $address;
  214. }
  215. function get_address($address)
  216. {
  217. $address = ereg_replace("([ trn])+", "", $address);
  218. $address = ereg_replace("^.* <(.+)>.*$", "\1", $address);
  219. return $address;
  220. }
  221. function smtp_debug($message)
  222. {
  223. if ($this->debug) {
  224. echo $message."
    ";
  225. }
  226. }
  227. function get_attach_type($image_tag) { //
  228. $filedata = array();
  229. $img_file_con=fopen($image_tag,"r");
  230. unset($image_data);
  231. while ($tem_buffer=AddSlashes( fread($img_file_con,filesize($image_tag))))
  232. $image_data.=$tem_buffer;
  233. fclose($img_file_con);
  234. $filedata['context'] = $image_data;
  235. $filedata['filename']= Basename($image_tag);
  236. $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
  237. switch($extension){
  238. case ".gif":
  239. $filedata['type'] = "image/gif";
  240. break;
  241. case ".gz":
  242. $filedata['type'] = "application/x-gzip";
  243. break ;
  244. case ".htm":
  245. $filedata['type'] = "text/html";
  246. break;
  247. case ".html":
  248. $filedata['type'] = "text/html";
  249. break ;
  250. case ".jpg":
  251. $filedata['type'] = "image/jpeg";
  252. break;
  253. case ".tar":
  254. $filedata['type'] = "application/x-tar";
  255. break;
  256. case ".txt":
  257. $filedata['type'] = "text/plain";
  258. break;
  259. case ".zip":
  260. $filedata['type'] = "application/zip";
  261. break;
  262. default:
  263. $filedata['type'] = "application/octet-stream";
  264. break;
  265. }
  266. return $filedata;
  267. }
  268. }
  269. ?>
  270. $smtpserver = "smtp.163.com";//SMTPサービス
  271. $smtpserverport =25;//SMTPサービス端末口
  272. $smtpusermail = "caowlong163@163.com";//SMTPサービスの用户邮箱
  273. $smtpemailto = "caowlong@qq.com";//送信给谁
  274. $smtpuser = "caowlong163@163.com";//SMTP サーバーの用人帐号
  275. $smtppass = "XXX";//SMTP サーバーの用人秘密コード
  276. $mailsubject = "PHP100测试邮件系统";//邮件主题
  277. $mailbody = "

    gt;你的用户名是张三,密码是11111

    ";//邮件内容
  278. $mailtype = "HTML" ;//邮件格式(HTML/TXT),TXT は文本邮件です
  279. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
  280. $smtp->debug = true;//否か表示送信の调试情報
  281. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  282. ?>
复制代

PHP


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!