使用pear:Net_SMTP類發送郵件的例子

來源:互聯網
上載者:User
  1. require 'Net/SMTP.php';

  2. $host = '126.com';//smtp伺服器的ip或網域名稱

  3. $username= 'arcow';//登陸smtp伺服器的使用者名稱
  4. $password= 'secret';//登陸smtp伺服器的密碼
  5. $from = 'jbxue@126.com'; //誰發的郵件
  6. $rcpt = array('test@test.com', 'jbxue@126.com');//可設多個接收者
  7. $subj = "Subject: 你是誰\n";//主題
  8. $body = "test it";//郵件內容

  9. /* 建立一個類 */

  10. if (! ($smtp = new Net_SMTP($host))) {
  11. die("無法初始化類Net_SMTP!\n");
  12. }

  13. /* 開始串連SMTP伺服器*/

  14. if (PEAR::isError($e = $smtp->connect())) {
  15. die($e->getMessage() . "\n");
  16. }

  17. /* smtp需要身分識別驗證 */

  18. $smtp->auth($username,$password,"PLAIN");

  19. /*設定寄件者郵箱 */

  20. if (PEAR::isError($smtp->mailFrom($from))) {
  21. die("無法設定寄件者郵箱為 <$from>\n");
  22. }

  23. /* 設定內送郵件者 */

  24. foreach ($rcpt as $to) {
  25. if (PEAR::isError($res = $smtp->rcptTo($to))) {
  26. die("郵件無法投遞到 <$to>: " . $res->getMessage() . "\n");
  27. }
  28. }

  29. /* 開始發送郵件內容 */

  30. if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) {
  31. die("Unable to send data\n");
  32. }

  33. /* 中斷連線 */

  34. $smtp->disconnect();
  35. echo "發送成功!";
  36. ?>

複製代碼

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.