Socket_PHP チュートリアルに基づいて SMTP を実装して電子メールを送信する PHP メソッド

WBOY
リリース: 2016-07-13 10:04:59
オリジナル
835 人が閲覧しました

PHP を使用してソケットに基づいて電子メールを送信する SMTP を実装する方法

この記事では、主にソケットに基づいて電子メールを送信するための SMTP を実装する PHP の方法を紹介し、PHP を使用して SMTP を実装して送信する原理とテクニックを分析します。ソケットを使用したメールには、特定の参考値があります。必要な友達はそれを参照できます。

この記事の例では、PHP が SMTP を実装してソケットに基づいて電子メールを送信する方法を説明します。参考のためにみんなで共有してください。具体的な分析は次のとおりです:

php はソケットを使用して SMTP 経由で電子メールを送信します。

php の php-sockets 拡張機能を使用すると、プレーン テキストおよび HTML 形式で電子メールを送信できます。コードは次のとおりです:

コードは次のとおりです:


/**
*メール送信クラス
* プレーンテキストメールと HTML 形式メールの送信をサポートします
* @example
* $config = 配列(
* "from" => "*****",
* "to" => "***",
* 「件名」 => 「テスト」、
* "body" => "テスト",
* "ユーザー名" => "***",
* "パスワード" => "****",
* "isHTML" => true
* );
*
* $mail = 新しい MySendMail();
*
* $mail->setServer("smtp.126.com");
*
* $mail->setMailInfo($config);
* if(!$mail->sendMail()) {
* echo $mail->error();
* 1 を返します;
* }
*/
クラス MySendMail {
/**
* @var メール転送エージェントのユーザー名
* @アクセス非公開
*/
プライベート $_userName;
/**
* @var メール転送エージェントのパスワード
* @アクセス非公開
*/
プライベート $_パスワード;
/**
* @var メール転送プロキシサーバーアドレス
* @access protected
*/
保護された $_sendServer;
/**
* @var メール転送プロキシサーバーのポート
* @access protected
*/
保護された $_port=25;
/**
* @var 送信者
* @access protected
*/
$_from から保護されました;
/**
* @var 受信者
* @access protected
*/
保護された $_to;
/**
* @var テーマ
* @access protected
*/
保護された $_subject;
/**
* @var メール本文
* @access protected
*/
保護された $_body;
/**
* @var HTML形式のメールですか? * @access protected
*/
protected $_isHTML=false;
/**
* @var ソケットリソース
* @access protected
*/
保護された $_socket;
/**
* @var エラーメッセージ
* @access protected
*/
保護された $_errorMessage;
public function __construct($from="", $to="", $subject="", $body="", $server="", $username="", $password="",$isHTML=" ", $port="") {
if(!empty($from)){
$this->_from = $from;
}
if(!empty($to)){
$this->_to = $to;
}
if(!empty($subject)){
$this->_subject = $subject;
}
if(!empty($body)){
$this->_body = $body;
}
if(!empty($isHTML)){
$this->_isHTML = $isHTML;
}
if(!empty($server)){
$this->_sendServer = $server;
}
if(!empty($port)){
$this->_port = $port;
}
if(!empty($username)){
$this->_userName = $username;
}
if(!empty($password)){
$this->_password = $password;
}
}
/**
* メール転送エージェントを設定します
* @param string $server プロキシ サーバーの IP またはドメイン名
* @param int $port プロキシサーバーのポート、SMTP デフォルトポート 25
* @param int $localPort ローカルポート
* @return boolean
*/
パブリック関数 setServer($server, $port=25) {
if(!isset($server) || empty($server) || !is_string($server)) {
$this->_errorMessage = "最初のパラメータは無効なパラメータです";
false を返します;
}
if(!is_numeric($port)){
$this->_errorMessage = "最初の 2 つは無効なパラメータです";
false を返します;
}
$this->_sendServer = $server;
$this->_port = $port;
true を返します;
}
/**
*メールを設定します
* @アクセス公開
* @param array $config メール設定情報
* メールの送信者、受信者、件名、内容、メール転送エージェントの確認情報が含まれます
* @return boolean
*/
パブリック関数 setMailInfo($config) {
if(!is_array($config) || count($config) $this->_errorMessage = "パラメータは必須です";
false を返します;
}
$this->_from = $config['from'];
$this->_to = $config['to'];
$this->_subject = $config['subject'];
$this->_body = $config['body'];
$this->_userName = $config['ユーザー名'];
$this->_password = $config['パスワード'];
if(isset($config['isHTML'])){
$this->_isHTML = $config['isHTML'];
}
true を返します;
}
/**
*メールを送信
* @アクセス公開
* @return boolean
*/
パブリック関数 sendMail() {
$command = $this->getCommand();
$this->socket();
foreach ($command を $value) {
if($this->sendCommand($value[0], $value[1])) {
続けます;
}
他{
false を返します;
}
}
$this->close(); //実際には、ここで閉じる必要はありません。smtp コマンド QUIT が発行された後、サーバーは接続を閉じ、ローカル ソケット リソースは自動的に解放されます。 echo 'メール OK!';
true を返します;
}
/**
* エラーメッセージを返す
* @戻り文字列
*/
パブリック関数エラー(){
if(!isset($this->_errorMessage)) {
$this->_errorMessage = "";
}
$this->_errorMessage;
を返す }
/**
* メールコマンドに戻る
* @access protected
* @return 配列
*/
保護された関数 getCommand() {
if($this->_isHTML) {
$mail = "MIME バージョン:1.0rn";
$mail .= "コンテンツタイプ:text/html;charset=utf-8rn";
$mail .= "FROM:test<" . $this->_from . $mail .= "TO:>_to";
$mail .= "件名:" . $this->_subject ."rnrn";
$mail .= $this->_body . "rn.rn";
}
他{
$mail = "FROM:test<" . $this->_from . $mail .= "TO:>_to";
$mail .= "件名:" . $this->_subject ."rnrn";
$mail .= $this->_body . "rn.rn";
}
$command = 配列(
array("HELO sendmailrn", 250),
array("AUTH LOGINrn", 334),
array(base64_encode($this->_userName) . "rn", 334),
array(base64_encode($this->_password) . "rn", 235),
array("MAIL FROM:_from . ">rn", 250),
array("RCPT TO:_to . ">rn", 250),
配列("DATArn", 354),
配列($mail, 250),
配列("QUITrn", 221)
);
$コマンドを返す;
}
/**
* @access protected
* @param string $command サーバーに送信された SMTP コマンド
* @param int $code サーバーから返される応答を期待していますか
* @param ブール値
*/
保護された関数 sendCommand($command, $code) {
echo 'コマンドを送信します:' . $command . '
';
//サーバーにコマンドを送信
試してください{
if(socket_write($this->_socket, $command, strlen($command))){
//サーバーの戻り値を読み取ります
$data = トリム(socket_read($this->_socket, 1024));
echo 'response:' . $data '

';
if($data) {
$pattern = "/^".$code."/";
if(preg_match($pattern, $data)) {
true を返します;
}
他{
$this->_errorMessage = "エラー:" . $data . "|**| コマンド:";
false を返します;
}
}
他{
$this->_errorMessage = "エラー:" .socket_strerror(socket_last_error());
false を返します;
}
}
他{
$this->_errorMessage = "エラー:" .socket_strerror(socket_last_error());
false を返します;
}
}catch(例外 $e) {
$this->_errorMessage = "エラー:" . $e->getMessage();
}
}
/**
* サーバーへのネットワーク接続を確立します
* @アクセス非公開
* @return boolean
*/
プライベート関数ソケット() {
if(!function_exists("socket_create")) {
$this->_errorMessage = "拡張機能 php-sockets を有効にする必要があります";
false を返します;
}
//ソケットリソースを作成します
$this->_socket =ソケット_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
if(!$this->_socket) {
$this->_errorMessage =ソケット_strerror(socket_last_error());
false を返します;
}
//サーバーに接続
if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
$this->_errorMessage =ソケット_strerror(socket_last_error());
false を返します;
}
socket_read($this->_socket, 1024);
true を返します;
}
/**
* ソケットを閉じる
* @アクセス非公開
* @return boolean
*/
プライベート関数 close() {
if(isset($this->_socket) && is_object($this->_socket)) {
$this->_socket->close();
true を返します;
}
$this->_errorMessage = "リソースを近づけることはできません";
false を返します;
}
}
/**************************** テスト *********************** ************/
$config = 配列(
「から」 => 「XXXXX」、
「へ」 => 「XXXXX」、
「件名」 => 「テスト」、
"body" => "テスト",
"ユーザー名" => "XXXXX",
"パスワード" => "******",
//"isHTML" => true
);
$mail = 新しい MySendMail();
$mail->setServer("smtp.126.com");
$mail->setMailInfo($config);
if(!$mail->sendMail()) {
echo $mail->error();
1 を返します;
}

この記事で説明した内容が皆様の PHP プログラミング設計に役立つことを願っています。

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