Linux環境でphpでメールを送信するようにsmtpを設定する方法

WBOY
リリース: 2016-07-25 08:54:43
オリジナル
1600 人が閲覧しました
  1. include_once("class.phpmailer.php");
  2. /**
  3. * 電子メールモジュール構成情報を定義します
  4. */
  5. define("SMTP_HOST","smtp.mail.yahoo.com) "); // SMTP ホスト
  6. define("SMTP_MAIL"," XXXX@yahoo.cn"); // SMTP ユーザーの電子メール
  7. define("SMTP_PASS"," XXXX"); // SMTP のパスワード
  8. define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP ユーザーのメールアドレス

  9. define("SERVICE_NAME","PHPBOOK Email Test") // 使用される SMTP 名< p>/**
  10. * phpmailer を使用してメールモジュールを送信します
  11. *
  12. * @param string $email
  13. * @param string $user
  14. * @param string $subject
  15. * @param string $body
  16. * @return bool
  17. */
  18. function sendMail($email,$user,$subject,$body)
  19. {
  20. $mail = new PHPMailer();
  21. //$this;
  22. $mail ->IsSMTP (); // SMTP を設定します
  23. $mail->Host = SMTP_HOST; // SMTP サーバーのアドレスを設定します
  24. $mail->SMTPAuth = true; // SMTP 権限の検証をオンにします
  25. $mail-> ; // SMTP ユーザー名
  26. $mail->Password = SMTP_PASS; // SMTP サーバーのパスワード

  27. $mail->From = SERVICE_MAIL; // 送信者のアドレスを設定します= SERVICE_NAME // 送信者の名前を設定します

  28. $mail->AddAddress($email, $user) // 受信者のアドレスを追加します
  29. $mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); // 返信アドレスを設定しますp>
  30. $mail->WordWrap = 50; // 表示形式を設定します

  31. $mail->IsHTML(true); // HTML をサポートするようにメールを設定します
  32. $mail->Subject = $subject;
  33. $mail->Body = $body;
  34. $mail->AltBody = ""; // テキスト型 mail

  35. if(!$mail- >Send()) {

  36. return $mail->ErrorInfo;
  37. }
  38. return true;
  39. }

  40. //テストメールの送信開始: fsockopen() [function.fsockopen ]: php_network_getaddresses: getaddrinfo failed: Name or /var/www/xiehui/admin/mail/class.smtp.php の 89 行目でサービスが不明です

  41. $tomail = " XXXX@126.com";
  42. $user = " XXXXlinux ";
  43. $_mailSubject = "メール テストexample!"; // ユーザーに送信されるメール件名グループ
  44. $_mailBody = "Sina"; // メールコンテンツチーム
  45. sendMail($tomail,$user ,$_mailSubject,$_mailBody);
  46. ?>

コードをコピー

Yahoo の SMTP は非常に使いやすいことが実験で証明されましたが、いわゆる Sina の SMTP は実際には使いにくいです。

メソッド4、ソケットによって書かれたプログラムに与えられます ソケットを使用して電子メールを送信するためのカプセル化クラス:

  1. class sendmail{
  2. var $lastmessage; // 返された最後の応答メッセージを記録します
  3. var $lastact; // HELO で使用されます。ユーザー歓迎
  4. var $debug; //デバッグ情報を表示するかどうか
  5. var $smtp; //smtp サーバー
  6. var $port; // ソケットハンドル
  7. // メール送信関数
  8. function send_mail($smtp, $welcome="", $debug=false) {
  9. if(empty($smtp)) die("SMTP を空にすることはできません!");
  10. $this->smtp=$smtp;
  11. if(empty($welcome)) {
  12. $this->welcome=gethostbyaddr("localhost");
  13. }else
  14. $this->welcome= $welcome;
  15. $this->debug=$debug;
  16. $this->lastmessage="";
  17. $this->lastact="";
  18. $this->port="25";
  19. }
  20. //表示调试情報
  21. function show_debug($message, $inout) {
  22. if ($this->debug) {
  23. if($inout=="in"){ //响应情報
  24. $m='< ;< ';
  25. }else
  26. $m='>> ';
  27. if(!ereg("n$", $message))
  28. $message .= "
    ";
  29. $message= nl2br($message);
  30. echo "${m}${message}";
  31. }
  32. }
  33. //実行传递的コマンド
  34. function do_command($command, $code) {
  35. $this->lastact=$command;
  36. $this->show_debug($this->lastact, "out");
  37. fputs ( $this->fp, $this->gt; lastact );
  38. $this->lastmessage = fgets ( $this->fp, 512 );
  39. $this->show_debug($this->lastmessage, "in");
  40. if(!ereg(" ^$code", $this->lastmessage))
  41. return false;
  42. else
  43. return true;
  44. }
  45. //邮件送信处理
  46. function send( $to,$from,$subject,$message) {
  47. / /接続サービス
  48. $this->lastact="connect";
  49. $this->show_debug("SMTP サーバーへの接続: ".$this->smtp, "out");
  50. $this->fp = fsockopen ( $this->smtp, $this->port );
  51. if ( $this->fp ) {
  52. $this->set_socket_blocking( $this->fp, true );
  53. $this ->lastmessage=fgets($this->fp,512);
  54. $this->show_debug($this->lastmessage, "in");
  55. if (! ereg ( "^220", $this->lastmessage ) ) {
  56. return false;
  57. }else{
  58. $this->lastact="HELO " . $this->ようこそ。 "n";
  59. if(!$this->do_command($this->lastact, "250")){
  60. fclose($this->fp);
  61. return false;
  62. }
  63. $this-> ;lastact="メール送信元: $from" 。 "n";
  64. if(!$this->do_command($this->lastact, "250")){
  65. fclose($this->fp);
  66. return false;
  67. }
  68. $this-> ;lastact="RCPT TO: $to" 。 "n";
  69. if(!$this->do_command($this->lastact, "250")){
  70. fclose($this->fp);
  71. return false;
  72. }
  73. //开始送信邮件正文
  74. $this->lastact="DATAn";
  75. if(!$this->do_command($this->lastact, "354")){
  76. fclose($this->fp);
  77. return false;
  78. }
  79. //开始処理邮件主题头
  80. $head="Subject: $subjectn";
  81. if(!empty($subject) && !ereg($head, $message)){
  82. $message = $ head.$message;
  83. }
  84. //处処理邮件From头
  85. $head="From: $fromn";
  86. if(!empty($from) && !ereg($head, $message)) {
  87. $message = $head.$message;
  88. }
  89. //開始処理邮件To头
  90. $head="To: $ton";
  91. if(!empty($to) && !ereg($head, $message)) {
  92. $message = $head.$message;
  93. }
  94. //处理结束串
  95. if(!ereg("n.n", $message))
  96. $message .= "n.n";
  97. $this->show_debug($message , "out");
  98. fputs($this->fp, $message);
  99. $this->lastact="QUITn";
  100. if(!$this->do_command($this->lastact, "250")){
  101. fclose($this->fp);
  102. return false;
  103. }
  104. }
  105. return true;
  106. }else{
  107. $this->show_debug("连接失败!!", "in ");
  108. return false;
  109. }
  110. }
  111. }
  112. ?>
复制代

ソケットを使用したメール送信の例:

  1. include ("./sendmail.class.php");
  2. $mail = new sendmail();
  3. $email = "こんにちは、これはテストメールです!";
  4. $ sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //調整情報を表示
  5. if($mail->send("XXXX@126.com", "XXXX@ 126) .com"、"SOCKET メールのテスト"、$email)) {
  6. echo "送信に成功しました!
    ";
  7. }else{
  8. echo "送信に失敗しました!
    ";
  9. }
  10. ?>
コードをコピー


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