検索
ホームページバックエンド開発PHPチュートリアルPHP で書かれた SMTP 電子メール送信クラス

  1. $smtpserver = "*****";
  2. $smtpserverport = 25;
  3. $smtpuser = "******";
  4. $smtppass = "***** **";
  5. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //ここで true は認証が使用されることを意味し、それ以外の場合は認証は使用されません。
  6. $smtp-> ;debug = false;
  7. //$emailtype = "HTML";
  8. for ($i=0; $i $smtp->sendmail("*****", "* ** ***", "Hello world!","これは単なるテストです!");
  9. }
  10. echo "合計 $i のメールが送信されました。 ";
  11. ?>
コードをコピー

以下は特定のクラスの実装です。

  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. /* プライベート変数 */
  13. var $sock;
  14. /* コンストラクター */
  15. function smtp($relay_host = "", $smtp_port = 25、$auth = false、$user、$pass)
  16. {
  17. $this->debug = false;
  18. $this->smtp_port = $smtp_port;
  19. $this->relay_host = $relay_host;
  20. $ this->time_out = 30; // fsockopen() で使用されます
  21. $this->auth = $auth; //auth
  22. $this->user = $user;
  23. $this->pass = $pass;
  24. $this->host_name = "localhost"; //HELO コマンドで使用されます
  25. $this->log_file = "";
  26. $this->sock = false;
  27. }
  28. /* Main Function */
  29. function sendmail($to, $from, $subject = "", $body = "", $mailtype= "", $cc = "", $bcc = "", $Additional_headers = "")
  30. {
  31. $mail_from = $this->get_address($this-> ;strip_comment($from));
  32. $body = ereg_replace("(^|( ))(.)", "1.3", $body);
  33. $header .= "MIME-Version:1.0 ";
  34. if ( $mailtype == "HTML") {
  35. $header .= "Content-Type:text/html ";
  36. }
  37. $header .= "To: " . $to 。 " ";
  38. if ($cc != "") {
  39. $header .= "Cc: " . $cc 。 " ";
  40. }
  41. $header .= "差出人: $from; ";
  42. $header .= "件名: " . $subject 。 " ";
  43. $header .= $Additional_headers;
  44. $header .= "日付: " . date("r") . " ";
  45. $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ") ";
  46. list($msec, $sec) =explode(" ", microtime());
  47. $ header .= "メッセージID: ; ";
  48. $TO =explode(",", $this->strip_comment($to));
  49. if ($cc != "") {
  50. $TO = array_merge($TO,explode( ",", $this->strip_comment($cc)));
  51. }
  52. if ($bcc != "") {
  53. $TO = array_merge($TO,explode(",", $this->ストリップコメント($bcc)));
  54. }
  55. $sent = true;
  56. foreach ($TO as $rcpt_to) {
  57. $rcpt_to = $this->get_address($rcpt_to);
  58. if (!$this-> smtp_sockopen($rcpt_to)) {
  59. $this->log_write("エラー: " . $rcpt_to . " " にメールを送信できません);
  60. $sent = false;
  61. continue;
  62. }
  63. if ($this-> smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  64. $this->log_write("電子メールが ; に送信されました。 ");
  65. } else {
  66. $this->log_write("エラー: に電子メールを送信できません。");
  67. $sent = false;
  68. }
  69. fclose($this- >sock);
  70. $this->log_write("リモート ホストから切断されました ");
  71. }
  72. return $sent;
  73. }
  74. /* プライベート関数 */

  75. function smtp_send($helo, $from, $to, $header, $body = "")
  76. {
  77. if (!$this->smtp_putcmd("HELO", $helo)) {
  78. return $this->smtp_error("sending HELO command");
  79. }
  80. // auth
  81. if ($this->auth) {
  82. if (!$this->smtp_putcmd("AUTH LOGIN",base64_encode) ($this->user))) {
  83. return $this->smtp_error("HELO コマンドの送信中");
  84. }
  85. if (!$this->smtp_putcmd("", Base64_encode($this->gt; pass))) {
  86. return $this->smtp_error("HELO コマンドの送信");
  87. }
  88. }
  89. if (!$this->smtp_putcmd("MAIL", "FROM:;")) {
  90. return $this->smtp_error("sending MAIL FROM command");
  91. }
  92. if (!$this->smtp_putcmd("RCPT", "TO:;")) {
  93. return $this->smtp_error("RCPT TO コマンドを送信中");
  94. }
  95. if (!$this->smtp_putcmd("DATA")) {
  96. return $this->smtp_error("DATA コマンドの送信");
  97. }
  98. if (!$this->smtp_message($header, $ body)) {
  99. return $this->smtp_error("送信中メッセージ");
  100. }
  101. if (!$this->smtp_eom()) {
  102. return $this->smtp_error("送信中 ; ;;.;; [EOM]");
  103. }
  104. if (!$this->smtp_putcmd("QUIT")) {
  105. return $this->smtp_error ("QUIT コマンドを送信中");
  106. }
  107. return true;
  108. }
  109. function smtp_sockopen($address)
  110. {
  111. if ($this->relay_host == "") {
  112. return $this->smtp_sockopen_mx($ address);
  113. } else {
  114. return $this->smtp_sockopen_relay();
  115. }
  116. }
  117. function smtp_sockopen_relay()
  118. {
  119. $this->log_write(" . $this->relay_host . " を試行しています:" . $this->smtp_port . " ");
  120. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this-> time_out);
  121. if (!($this->sock && $this->smtp_ok​​())) {
  122. $this->log_write("エラー: リレー ホストに接続できません " . $this->relay_host 。 " ");
  123. $this->log_write("エラー: " . $errstr . " (" . $errno . ") ");
  124. return false;
  125. }
  126. $this->log_write("リレーに接続しましたhost " . $this->relay_host . " ");
  127. return true;;
  128. }
  129. function smtp_sockopen_mx($address)
  130. {
  131. $domain = ereg_replace("^.+@([^@]+)$" , "1", $address);
  132. if (!@getmxrr($domain, $MXHOSTS)) {
  133. $this->log_write("エラー: MX を解決できません "" . $domain . "" ");
  134. return false;
  135. }
  136. foreach ($MXHOSTS as $host) {
  137. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . " ");
  138. $this- >sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  139. if (!($this->sock && $this->smtp_ok​​( ))) {
  140. $this->log_write("警告: mx ホストに接続できません " . $host . " ");
  141. $this->log_write("エラー: " . $errstr . " (" . $ errno . ") ");
  142. continue;
  143. }
  144. $this->log_write("mx ホストに接続しました " . $host 。 " ");
  145. return true;
  146. }
  147. $this->log_write("エラー: どの mx ホストにも接続できません (" . implode(", ", $MXHOSTS) . ") ");
  148. return false;
  149. }
  150. function smtp_message($header, $body)
  151. {
  152. fputs($this->sock, $header . " " . $body);
  153. $this->smtp_debug(">; " . str_replace(" ", " " . ">; ", $header . " >; " . $body . " >; "));
  154. 関数 smtp_eom()
  155. {
  156. fputs($this- >sock, " . ");
  157. $this->smtp_debug(". [EOM] ");
  158. return $this->smtp_ok​​();
  159. }
  160. function smtp_ok​​()
  161. {
  162. $response = str_replace (" ", "", fgets($this->sock, 512));
  163. $this->smtp_debug($response . " ");
  164. if (!ereg("^[23]", $response )) {
  165. fputs($this->sock, "QUIT ");
  166. fgets($this->sock, 512);
  167. $this->log_write("エラー: リモート ホストが "" を返しました。 $response . "" ");
  168. return false;
  169. }
  170. return true;
  171. }
  172. function smtp_putcmd($cmd, $arg = "")
  173. {
  174. if ($arg != "") {
  175. if ($cmd = = "") $cmd = $arg;
  176. else $cmd = $cmd 。 「」。 $arg;
  177. }
  178. fputs($this->sock, $cmd . " ");
  179. $this->smtp_debug(">gt;; " . $cmd . " ");
  180. return $this-> smtp_ok​​();
  181. }
  182. function smtp_error($string)
  183. {
  184. $this->log_write("Error: " . $string . ". " 中にエラーが発生しました。);
  185. return false;
  186. }
  187. function log_write($message)
  188. {
  189. $this->smtp_debug($message);
  190. if ($this->log_file == "") {
  191. return true;
  192. }
  193. $message = date("M d H:i:s ") 。 get_current_user() 。 「[」。 getmypid() 。 "]: " 。 $message;
  194. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  195. $this->smtp_debug("警告: ログ ファイル "" を開くことができません。 $this->log_file 。 "" ");
  196. return false;;
  197. }
  198. flock($fp, $message);
  199. fclose( $fp);
  200. return true;
  201. }
  202. function Stripe_comment($address)
  203. {
  204. $comment = "([^()]*)";
  205. while (ereg($comment, $address)) {
  206. $address = ereg_replace($comment, "", $address);
  207. }
  208. return $address;
  209. }
  210. function get_address($address)
  211. {
  212. $address = ereg_replace("([ ])+", "", $address );
  213. $address = ereg_replace("^.*;.*$", "1", $address);
  214. return $address;
  215. }
  216. function smtp_debug($message)
  217. {
  218. if ($this->debug) {
  219. echo $message . ";";
  220. }
  221. }
  222. }
  223. ?>
复制幣

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
どのデータをPHPセッションに保存できますか?どのデータをPHPセッションに保存できますか?May 02, 2025 am 12:17 AM

phpssionscanStorestrings、numbers、arrays、andobjects.1.strings:textdatalikeusernames.2.numbers:integersorfloatsforcounters.3.arrays:listslikeshoppingcarts.4.objects:complextructuresthataresialized。

どのようにPHPセッションを開始しますか?どのようにPHPセッションを開始しますか?May 02, 2025 am 12:16 AM

tostartaphpsession、outsession_start()atthescript'sbeginning.1)placeitbe foreanyouttosetthesscookie.2)usesionsionsionsionserdatalikelogintatussorshoppingcarts.3)再生セッションインドストップレベントフィックスアタック

セッションの再生とは何ですか?また、セキュリティをどのように改善しますか?セッションの再生とは何ですか?また、セキュリティをどのように改善しますか?May 02, 2025 am 12:15 AM

セッション再生とは、新しいセッションIDを生成し、セッション固定攻撃の場合にユーザーが機密操作を実行するときに古いIDを無効にすることを指します。実装の手順には次のものが含まれます。1。感度操作を検出、2。新しいセッションIDを生成する、3。古いセッションIDを破壊し、4。ユーザー側のセッション情報を更新します。

PHPセッションを使用する際のパフォーマンスの考慮事項は何ですか?PHPセッションを使用する際のパフォーマンスの考慮事項は何ですか?May 02, 2025 am 12:11 AM

PHPセッションは、アプリケーションのパフォーマンスに大きな影響を与えます。最適化方法には以下が含まれます。1。データベースを使用してセッションデータを保存して応答速度を向上させます。 2。セッションデータの使用を削減し、必要な情報のみを保存します。 3.非ブロッキングセッションプロセッサを使用して、同時実行機能を改善します。 4.セッションの有効期限を調整して、ユーザーエクスペリエンスとサーバーの負担のバランスを取ります。 5.永続的なセッションを使用して、データの読み取り時間と書き込み時間を減らします。

PHPセッションはCookieとどのように異なりますか?PHPセッションはCookieとどのように異なりますか?May 02, 2025 am 12:03 AM

phpsesionsareserver-side、whilecookiesareclient-side.1)Sessionsionsionsoredataontheserver、aremoresecure.2)cookiesstoredataontheclient、cookiestoresecure、andlimitedinsizeisize.sesionsionsionivationivationivationivationivationivationivationivate

PHPはユーザーのセッションをどのように識別しますか?PHPはユーザーのセッションをどのように識別しますか?May 01, 2025 am 12:23 AM

phpidentifiesauser'ssessionsingsinssessionCookiesIds.1)whensession_start()iscalled、phpgeneratesauniquesidstoredsored incoookienadphpsessidontheuser'sbrowser.2)thisidallowsphptortorieSessiondatadata fromthata

PHPセッションを保護するためのベストプラクティスは何ですか?PHPセッションを保護するためのベストプラクティスは何ですか?May 01, 2025 am 12:22 AM

PHPセッションのセキュリティは、次の測定を通じて達成できます。1。session_regenerate_id()を使用して、ユーザーがログインまたは重要な操作である場合にセッションIDを再生します。 2. HTTPSプロトコルを介して送信セッションIDを暗号化します。 3。Session_Save_Path()を使用して、セッションデータを保存し、権限を正しく設定するためのSecure Directoryを指定します。

PHPセッションファイルはデフォルトで保存されていますか?PHPセッションファイルはデフォルトで保存されていますか?May 01, 2025 am 12:15 AM

phpsessionFilesToredInthededirectoryspecifiedBysession.save_path、通常/tmponunix-likesystemsorc:\ windows \ temponwindows.tocustomizethis:1)uesession_save_path()tosetaCustomdirectory、ensuringit'swritadistradistradistradistradistra

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール