使用Socket傳送郵件

2021-08-18 00:09:01 字數 3410 閱讀 2886

之前寫過一篇《使用php傳送郵件》,方法是利用nette/mail元件傳送郵件。

以下內容整理自《php核心技術與最佳實踐》。

php有乙個自帶的mail()函式,但是要想使用smtp協議傳送郵件,需要安裝smtp伺服器。如果不想安裝,可以使用socket傳送郵件。smtp協議建立在tcp協議之上,所以原則上按照smtp協議的規範,使用socket跟smtp伺服器進行互動。

smtp連線與傳送過程如下:

1)建立tcp連線。

2)客戶端傳送helo命令以標識發件人自己的身份,客戶端傳送mail命令。伺服器以ok作為響應,表明準備接收。

3)使用auth命令登陸smtp伺服器,輸入使用者名稱和密碼(注意,使用者名稱和密碼都需要base64加密)。

4)客戶端傳送rcpt命令,標識該電子郵件的計畫接收人,可以有多個rcpt行。伺服器以ok作為響應,表示願意為收件人傳送郵件。

5)協商結束後,使用data命令傳送。

6)以」.」號表示結束,輸入內容一起傳送出去,結束此次傳送,用quit命令退出。

例如,使用telnet建立乙個smtp會話,其中s代表伺服器,c代表客戶端,**如下:

c: open smtp.qq

.com

25s:

220 esmtp4.qq

.com esmtp qq mail server

c: helo smtp qq.com

s:250 esmtp4.qq

.com

c: auth login

s:334 vxnlcm5hbwu6

c: 這裡輸入使用base64加密過的使用者名稱

s:334 ugfzc3dvcmq6

c: 這裡輸入使用base64加密過的密碼

s:235 authentication successful

c: mail from:.com>

s:250 sender .com> ok

c: rcpt to:@163

.com>

s:250 recipient @163

.com> ok

c: data

s:354 enter mail,end with "." on a line by itself

c: this is example from smtp protocol

c:.s:

250 message sent

c: quit

s:221 goodbye

本來想用qq郵箱發郵件,但是qq郵箱開啟smtp後頁面老是出錯,使用163郵箱可以正常傳送郵件,郵箱需開啟pop3/smtp服務。

**:

smtp_mail.php

<?php

class smtp_mail

//取得伺服器資訊

$response

=fgets($this

->sock);

//若包含220,表示已經成功連線到伺服器

if (strstr($response,"220")===

false)

}//將命令傳送到伺服器,然後取得伺服器反饋資訊,判斷命令是否執行成功

private function do_command($cmd,$return_code)

return

true;

}//傳送郵件

public function send_mail($from,$to,$subject,$content)

//判斷主題內容是否為空

if (empty($subject) or empty($content))

//整合郵件資訊,傳送郵件主體時要以\r\n.\r\n作為結尾

$detail

="from:"

.$from.

"\r\n";

$detail.

="to:"

.$to.

"\r\n";

$detail.

="subject:"

.$subject.

"\r\n";

if ($this

->mail_format==

1)else

$detail.

="charset=utf-8\r\n\r\n";

$detail.

=$content;

//此處應該有判斷命令是否執行成功

$this

->do_command("helo smtp.qq.com\r\n",250);

$this

->do_command("auth login\r\n",334);

$this

->do_command("$this->user\r\n",334);

$this

->do_command("$this->pwd\r\n",235);

$this

->do_command("mail from:<"

.$from.

">\r\n",250);

$this

->do_command("rcpt to:<"

.$to.

">\r\n",250);

$this

->do_command("data\r\n",354);

$this

->do_command($detail.

"\r\n.\r\n",250);

$this

->do_command("quit\r\n",221);

return

true;

}//判斷是否為合法郵箱

private function is_email($emial)else

}//顯示除錯資訊

private function show_debug($message)

}}

index.php

<?php

include_once

"smtp_mail.php";

$host="smtp.163.com";

$port=25;

$user="你的賬戶名@163.com";

$pwd="授權碼";

$from="你的賬戶名@163.com";

$to="目標郵箱";

$subject="test smtp mail";

$content="this is example email for you.";

$mail=new smtp_mail($host,$port,base64_encode($user),base64_encode($pwd),1,true);

$mail->send_mail($from,$to,$subject,$content);

PHP使用socket傳送郵件

smtp協議建立在tcp協議之上,所以原則上按照smtp協議的規範,使用socket跟smtp伺服器進行互動。使用fsockopen 函式代替socket 類函式。fsockopen 函式的好處是把socket連線繫結到乙個流上,然後使用各種操作流的函式操作這個socket連線。使用fsockope...

使用Socket撰寫郵件傳送程式

首先,我們簡單介紹一下帶驗證的 tp伺服器如何使用auth原語進行身份驗證,其詳細的定義可以參考rfc2554。具體如下 1 首先,需要使用ehlo而不是原先的helo。2 ehlo成功以後,客戶端需要傳送auth原語,與伺服器就認證時使用者名稱和密碼的傳遞方式進行協商。3 如果協商成功,伺服器會返...

用socket來傳送郵件

以前用vb時,記得有個mail控制項,後來接觸到了cdo.messages這個玩意,發郵件是蠻方便,那還是在vbs的情況下,後來看了下php,perl,發現發郵件乙個函式就可以了,呵呵,那麼這些背後的細節是什麼呢,還是用socket來揭示下吧 郵件傳送離不開一樣東西,smtp,即簡單郵件傳輸協議,對...