ホームページ  >  記事  >  バックエンド開発  >  phpソケットはsmtpを使用して添付ファイル付きのメールを送信します

phpソケットはsmtpを使用して添付ファイル付きのメールを送信します

WBOY
WBOYオリジナル
2016-07-25 08:54:43986ブラウズ
  1. /**

  2. * php ソケット smtp メール送信
  3. * 編集: bbs.it-home.org
  4. */

  5. //define("SOL", "n");

  6. define(" EOL", "rn");
  7. define("SMTP_HOST", "smtp.163.com");//SMTP サーバー
  8. define("SMTP_PORT", "25");//SMTP サーバーのポート
  9. define("SMTP_USER") ", "");//SMTP サーバーのユーザー アカウント
  10. define("SMTP_PASS", "");//SMTP サーバーのユーザー パスワード

  11. $from = ""; //SMTP サーバーのユーザーメールアドレス

  12. $to = "";//送信先、カンマ区切りで複数のメールを送信可能
  13. $cc = "";
  14. $bcc = "";

  15. $subject= "これは PHP によって送信された添付ファイル付きのメールです";//多くのクライアントではメールの件名が文字化けするので、変換してください

  16. $body = "これは添付ファイル付きのメール送信プログラムです
    ご覧ください。HTM タグがここに表示されます。リンクをクリックしてください ".date('Y-m-d H:i:s');//メール内容
  17. $smtp = new smtp(SMTP_HOST,SMTP_PORT,SMTP_USER,SMTP_PASS, true);//これらのうち true は認証が使用されることを意味し、それ以外の場合は認証が使用されません。
  18. $smtp->addAttachment("mail.zip");
  19. $smtp->sendmail($to, $from, $subject, $body, $ cc , $bcc);

  20. class smtp {

  21. /* パブリック変数 */

  22. public $attachments = array();
  23. /* プライベート変数 * /
  24. プライベート $smtp_host;
  25. プライベート $smtp_port;
  26. プライベート $time_out;
  27. プライベート $host_name;
  28. プライベート $auth;
  29. プライベート $user;
  30. プライベート $pass;
  31. プライベート $sock;

  32. public function smtp($smtp_host = null, $smtp_port = null, $user = null, $pass = null, $auth = true) {
  33. $this->smtp_host = (!empty($ smtp_host )) $smtp_host : SMTP_HOST;
  34. $this->smtp_port = (!empty($smtp_port)) ? $smtp_port : SMTP_PORT;
  35. $this->user = (!empty($user)) ? : SMTP_PORT;
  36. $this->pass = (!empty($pass)) ? $pass : SMTP_PORT;
  37. $this->auth = $auth;
  38. $this->time_out = 15;
  39. #
  40. $ this ->host_name = "localhost";
  41. $this->sock = FALSE;
  42. }

  43. /* Main Function */

  44. public function sendmail($to, $from, $ subject = "", $body = "", $cc = "", $bcc = "") {
  45. $bndp = md5(uniqid("")) . rand(1000, 9999);
  46. $bnd = md5( uniqid ("")) . rand(1000, 9999);
  47. list ($msec, $sec) =explode(" ", microtime());

  48. $mail_from = $this- > ;strip_line_breaks($from);

  49. $mail_to =explode(",", $to);
  50. $body = preg_replace("/(^|(rn))(\.)/", "", $body) ;
  51. if ($cc != "") $mail_to = array_merge($mail_to,explode(",", $cc));
  52. if ($bcc != "") $mail_to = array_merge($mail_to,explode( " ,", $bcc));

  53. $headers = "MIME-Version:1.0" . EOL;

  54. $headers .= "To: " . $ cc != "") {
  55. $headers .= "Cc: " . $cc }
  56. $headers .= "差出人: $from<" 。 $から。 「>」 。 EOL;
  57. $headers .= "件名: " . $subject 。 EOL;
  58. $headers .= "日付: " . date("r") . EOL;
  59. $headers .= "X-Mailer: Webmail ver 1.0 (PHP バージョン/" . phpversion() . ")" . EOL;
  60. $headers .= "メッセージ ID: <" 。 date("YmdHis", $sec) 。 「。」 。 ($msec * 1000000) 。 「。」 。 $から。 「>」 。 EOL;
  61. if (count($this->attachments) > 0) {
  62. $headers .= "Content-Type: multipart/mixed;" 。 EOL 。 chr(9) 。 " 境界="" . $bndp . """ . EOL 。 EOL;
  63. $headers .= '--'.$bndp 。 EOL;
  64. $headers .= 'Content-Type : multipart/alternative;境界="' . $bnd . '"' . EOL 。 EOL;
  65. $headers .= '--' 。 $bnd 。 EOL;
  66. $headers .= 'Content-Type: text/plain; charset=utf-8' 。 EOL;
  67. $headers .= "コンテンツ転送エンコーディング: 8 ビット" . EOL 。 EOL;
  68. $headers .= $body . EOL;
  69. $headers .= '--' 。 $bnd 。 EOL;
  70. $headers .= 'コンテンツタイプ: text/html; charset=utf-8' 。 EOL;
  71. $headers .= "コンテンツ転送エンコーディング: 8 ビット" . EOL 。 EOL;
  72. $headers .= $body . EOL;
  73. $headers .= '--' 。 $bnd 。 「--」。 EOL;

  74. foreach ($this->attachments as $att) {

  75. $headers .= "--" . $bndp 。 EOL 。 $att;
  76. }
  77. $headers .= '--' . $bndp 。 「--」。 EOL;
  78. $this->clear_attachments();
  79. } else {
  80. $headers .= 'Content-Type : multipart/alternative;boundary="'.$bnd.'"' 。 EOL 。 EOL;
  81. $headers .= '--'.$bnd 。 EOL;
  82. $headers .= 'Content-Type: text/plain; charset=utf-8' 。 EOL;
  83. $headers .= "コンテンツ転送エンコーディング: 8 ビット" . EOL 。 EOL;
  84. $headers .= $body . EOL;
  85. $headers .= '--'.$bnd 。 EOL;
  86. $headers .= 'コンテンツタイプ: text/html; charset=utf-8' 。 EOL;
  87. $headers .= "コンテンツ転送エンコーディング: 8 ビット" . EOL 。 EOL;
  88. $headers .= $body . EOL;
  89. $headers .= '--'.$bnd.'--' 。 EOL;
  90. }

  91. $sent = TRUE;

  92. foreach ($mail_to as $rcpt_to) {
  93. $rcpt_to = $this->strip_line_breaks($rcpt_to);
  94. if (!$this->smtp_sockopen($rcpt_to)) {
  95. $this- >log_write("エラー: " に電子メールを送信できません。$rcpt_to);
  96. $sent = FALSE;
  97. continue;
  98. }
  99. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to) , $headers, $body)) {
  100. $this->log_write("メールが <" . $rcpt_to . ">" に送信されました);
  101. } else {
  102. $this->log_write( 「エラー: $rcpt_to にメールを送信できません。">");
  103. $sent = FALSE;
  104. }
  105. $this->log_write(" {$mail_to} send over;");
  106. return $sent;
  107. }

  108. public function addAttachment($file, $dispo = "添付ファイル") {

  109. $file_data = (file_exists($file)) ? file_get_contents($file) : "";
  110. if ($file_data != "") {
  111. $filename = Basename($file);
  112. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  113. $chunks = chunk_split(base64_encode( $file_data));
  114. $parts = "Content-Type: application/$ext; name="" . $filename . """ . EOL;
  115. $parts .= "コンテンツ転送エンコーディング: Base64" . EOL;
  116. $parts .= "Content-Disposition: " . $dispo 。 "; ファイル名="" . $ファイル名 . """ . EOL 。 EOL;
  117. $parts .= $chunks 。 EOL 。 EOL;
  118. $this->attachments[] = $parts;
  119. }
  120. }

  121. private function clear_attachments() {

  122. unset($this->attachments);
  123. $this->attachments = array();
  124. }

  125. /* プライベート関数 */

  126. プライベート関数 smtp_send($helo, $from, $to, $header, $body = "") {
  127. if (!$this->smtp_putcmd("HELO", $helo)) {
  128. //$this->log_write("エラー: HELO コマンドの送信中にエラーが発生しました。");
  129. return FALSE;
  130. }
  131. #auth
  132. if ($this->auth) {
  133. if (!$this- >smtp_putcmd("AUTH LOGIN",base64_encode($this->user))) {
  134. //$this->log_write("エラー: HELO コマンドの送信中にエラーが発生しました。");
  135. return FALSE;
  136. }
  137. if (!$this->smtp_putcmd("",base64_encode($this->pass))) {
  138. //$this->log_write("エラー: HELO コマンドの送信中にエラーが発生しました。");
  139. return FALSE;
  140. }
  141. }
  142. if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  143. //$this->log_write("エラー: MAIL FROM コマンドの送信中にエラーが発生しました。");
  144. return FALSE;
  145. }
  146. if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  147. //$this->log_write("エラー: RCPT の送信中にエラーが発生しましたTO コマンド。");
  148. return FALSE;
  149. }
  150. if (!$this->smtp_putcmd("DATA")) {
  151. //$this->log_write("エラー: DATA コマンドの送信中にエラーが発生しました。" );
  152. return FALSE;
  153. }
  154. if (!$this->smtp_message($header, $body)) {
  155. //$this->log_write("エラー: メッセージの送信中にエラーが発生しました。");
  156. return FALSE;
  157. }
  158. if (!$this->smtp_eom()) {
  159. //$this->log_write("エラー: の送信中にエラーが発生しました。 return FALSE;
  160. }
  161. if (!$this->smtp_putcmd("QUIT")) {
  162. //$this->log_write("エラー: 送信中にエラーが発生しましたQUIT コマンド。");
  163. return FALSE;
  164. }
  165. return TRUE;
  166. }
  167. private function smtp_sockopen($address) {
  168. if ($this->smtp_host == "") {
  169. return $this->smtp_sockopen_mx ($address);
  170. } else { // bbs.it-home.org
  171. return $this->smtp_sockopen_relay();
  172. }
  173. }
  174. private function smtp_sockopen_relay() {
  175. $this->log_write("Trying接続する " 。 $this->smtp_host 。 「:」。 $this->smtp_port 。 "...");
  176. $this->sock = @fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  177. if (! ($this->sock && $this->smtp_ok​​())) {
  178. $this->log_write("エラー: 接続エラー" . $errstr . " (" . $errno . ")");
  179. return FALSE;
  180. }
  181. $this->log_write("Connected Ok");
  182. return TRUE;
  183. }
  184. プライベート関数 smtp_sockopen_mx($address) {
  185. $domain = preg_replace("/^.+@([^@ ]+)$/", "1", $address);
  186. if (!@getmxrr($domain, $MXHOSTS)) {
  187. $this->log_write("エラー: MX を解決できません "" . $domain . """);
  188. return FALSE;
  189. }
  190. foreach ($MXHOSTS as $host) {
  191. $this->log_write("Trying to " . $host . ":" . $this->smtp_port);
  192. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  193. if (!($this->sock && $this-> ;smtp_ok​​())) {
  194. $this->log_write("接続エラー ," . $errstr . " (" . $errno . ")");
  195. 続行;
  196. }
  197. $this->log_write(" mx ホストに接続しました " . $host);
  198. return TRUE;
  199. }
  200. $this->log_write("エラー: どの mx ホストにも接続できません (" . implode(", ", $MXHOSTS) 。 ")");
  201. return FALSE;
  202. }
  203. プライベート関数 smtp_message($header, $body) {
  204. fputs($this->sock, $header . "rn" . $body);
  205. return TRUE;
  206. }
  207. プライベート関数 smtp_eom() {
  208. fputs($this->sock, "rn.rn");
  209. return $this->smtp_ok​​();
  210. }
  211. プライベート関数 smtp_ok​​() {
  212. $response = str_replace( "rn", "", fgets($this->sock, 512));
  213. if (!preg_match("/^[23]/", $response)) {
  214. fputs($this->sock, "QUITrn");
  215. fgets($this->sock, 512);
  216. $this->log_write("エラー: リモート ホストが "" . $response . """);
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }
  221. プライベート関数 smtp_putcmd($cmd, $arg = "") {
  222. if ($arg != "") $cmd = ($cmd == "") ? $arg : ($cmd . " " . $arg);
  223. fputs($this->sock, $cmd . "rn");
  224. return $this->smtp_ok​​();
  225. }
  226. プライベート関数strip_line_breaks( $address) {
  227. $address = preg_replace("/([trn])+/", "", $address);
  228. $address = preg_replace("/^.*<(.+)>.*$ /", "", $address);
  229. return $address;
  230. }
  231. public function log_write($message) {
  232. $message = date("M d H:i:s ") . get_current_user() 。 「[」。 getmypid() 。 "]: " 。 $message;
  233. file_put_contents(dirname(__FILE__) . '/mail.log', $message . PHP_EOL, FILE_APPEND | LOCK_EX);
  234. }
  235. }

复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。