ホームページ >バックエンド開発 >PHPチュートリアル >PHP は、HTML 形式の電子メールを送信するためのクラスの送信をサポートしています

PHP は、HTML 形式の電子メールを送信するためのクラスの送信をサポートしています

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBオリジナル
2016-07-25 08:43:011093ブラウズ
  1. /**
  2. * メール送信クラス
  3. * プレーンテキストメールと HTML 形式メールの送信をサポート
  4. * @example
  5. * $config = array(
  6. * "from" => "*****",
  7. * "to" = > ; "***",
  8. * "件名" => "テスト",
  9. * "テスト",
  10. * "ユーザー名" => *",
  11. * "パスワード" => "****",
  12. * "isHTML" => true
  13. * );
  14. *
  15. * $mail = new MySendMail();
  16. *
  17. * $mail -> setServer("smtp.126.com");
  18. *
  19. * $mail->setMailInfo($config);
  20. * if(!$mail->sendMail()) {
  21. * echo $mail->error ();
  22. * 1 を返します。 }
  23. */
  24. class MySendMail{
  25. /**
  26. * @var メール転送エージェントのユーザー名
  27. * @access private
  28. */
  29. private $_userName;
  30. /**
  31. * @var メール転送エージェントのパスワード
  32. * @access private
  33. */
  34. プライベート $_password;
  35. /**
  36. * @var メール転送プロキシサーバーのアドレス
  37. * @access protected
  38. */
  39. protected $_sendServer;
  40. /**
  41. * @var メール転送プロキシサーバーのポート
  42. * @access protected
  43. */
  44. protected $_port=25;
  45. /**
  46. * @var 送信者
  47. * @access protected
  48. */
  49. protected $_from;
  50. /**
  51. * @var 受信者
  52. * @access protected
  53. */
  54. protected $_to;
  55. /**
  56. * @var テーマ
  57. * @access 保護されています
  58. */
  59. protected $_subject;
  60. /**
  61. * @var メール本文
  62. * @access protected
  63. */
  64. protected $_body;
  65. /**
  66. * @var HTML形式のメールかどうか
  67. * @access protected
  68. */
  69. protected $_isHTML=true;
  70. /**
  71. * @var ソケットリソース
  72. * @access 保護されています
  73. */
  74. protected $_socket;
  75. /**
  76. * @var エラーメッセージ
  77. * @access 保護されています
  78. */
  79. protected $_errorMessage;
  80. パブリック関数 __construct($from="", $to="", $subject="", $body="", $server="", $username="", $password="",$isHTML ="", $port="") {
  81. if(!empty($from)){
  82. $this->_from = $from;
  83. }
  84. if(!empty($to)){
  85. $this->_to = $to;
  86. }
  87. if(!empty($subject)){
  88. $this->_subject = $subject;
  89. }
  90. if(!empty($body)){
  91. $this->_body = $body;
  92. }
  93. if(!empty($isHTML)){
  94. $this->_isHTML = $isHTML;
  95. }
  96. if(!empty($server)){
  97. $this->_sendServer = $server;
  98. }
  99. if(!empty($port)){
  100. $this->_port = $port;
  101. }
  102. if(!empty($username)){
  103. $this->_userName = $username;
  104. }
  105. if(!empty($password)){
  106. $this->_password = $password;
  107. }
  108. }
  109. /**
  110. * メール転送プロキシを設定します
  111. * @param string $server プロキシ サーバーの IP またはドメイン名
  112. * @param int $port プロキシ サーバーのポート、SMTP デフォルト ポート 25
  113. * @param int $localPortローカルポート
  114. * @return boolean
  115. */
  116. public function setServer($server, $port=25) {
  117. if(!isset($server) || empty($server) || !is_string($ server)) {
  118. $this->_errorMessage = "最初のパラメータは無効なパラメータです";
  119. false を返します。
  120. }
  121. if(!is_numeric($port)){
  122. $this->_errorMessage = "最初の 2 つは無効なパラメーターです";
  123. false を返します。
  124. }
  125. $this->_sendServer = $server;
  126. $this->_port = $port;
  127. true を返します。
  128. }
  129. /**
  130. * 電子メールを設定します
  131. * @access public
  132. * @param array $config 電子メール設定情報
  133. * 電子メールの送信者、受信者、件名、内容、およびメール転送エージェントの検証情報が含まれます
  134. * @return boolean
  135. */
  136. public function setMailInfo($config) {
  137. if(!is_array($config) || count($config) $this->_errorMessage = "パラメータは必須です。」
  138. false を返します。 $this->_from = $config['from'];
  139. $this->_to = $config['to'];
  140. $this->_subject = $config['subject'];
  141. $this->_body = $config['body'];
  142. $this->_userName = $config['ユーザー名'];
  143. $this->_password = $config['パスワード'];
  144. if(isset($config['isHTML'])){
  145. $this->_isHTML = $config['isHTML'];
  146. }
  147. true を返します。
  148. }
  149. /**
  150. * メールを送信
  151. * @access public
  152. * @return boolean
  153. */
  154. public function sendMail() {
  155. $command = $this->getCommand();
  156. $this->socket();
  157. foreach ($command as $value) {
  158. if($this->sendCommand($value[0], $value[1])) {
  159. 続行;
  160. }
  161. else{
  162. false を返します。
  163. }
  164. }
  165. $this->close(); //此処も不要关闭,smtp コマンド:QUIT発行之後,サーバー就关闭了接続,本地ソケット资源会自动释放
  166. echo 'Mail OK!';
  167. true を返します。
  168. }
  169. /**
  170. * エラーメッセージを返す
  171. * @return string
  172. */
  173. public function error(){
  174. if(!isset($this->_errorMessage)) {
  175. $this->_errorMessage = "";
  176. }
  177. return $this->_errorMessage;
  178. }
  179. /**
  180. * メール返信コマンド
  181. * @access protected
  182. * @return 配列
  183. */
  184. protected function getCommand() {
  185. if($this->_isHTML) {
  186. $mail = "MIME-Version:1.0rn";
  187. $mail .= "コンテンツタイプ:text/html;charset=utf-8rn";
  188. $mail .= "FROM:test_from 。 ">rn";
  189. $mail .= "宛先:_to 。 ">rn";
  190. $mail .= "件名:" . $this->_subject ."rnrn";
  191. $mail .= $this->_body . "rn.rn";
  192. }
  193. else{
  194. $mail = "FROM:test_from 。 ">rn";
  195. $mail .= "宛先:_to 。 ">rn";
  196. $mail .= "件名:" . $this->_subject ."rnrn";
  197. $mail .= $this->_body . "rn.rn";
  198. }
  199. $command = array(
  200. array("HELO sendmailrn", 250),
  201. array("AUTH LOGINrn", 334),
  202. array(base64_encode($this->_userName) . "rn", 334),
  203. array(base64_encode($this->_password) . "rn", 235),
  204. array("MAIL FROM:_from . ">rn", 250),
  205. array( "RCPT TO:rn", 250),
  206. array($mail, 250),
  207. array("QUITrn", 221)
  208. );
  209. $command を返します。
  210. }
  211. /**
  212. * @access protected
  213. * @param string $command サーバーに送信された SMTP コマンド
  214. * @param int $code サーバーから返される応答を期待しますか
  215. * @param boolean
  216. */
  217. protected function sendCommand($command, $code) {
  218. echo 'コマンドを送信:' 。 $コマンド 。 '、予期されるコード:' 。 $コード 。 '
    ';
  219. //送信コマンド给服务器
  220. try{
  221. if(socket_write($this->_socket, $command, strlen($command))){
  222. //读取服务器を返す
  223. $data = trim(socket_read($this ->_ソケット、1024));
  224. '応答:' をエコーし​​ます。 $data 。 '

    ';
  225. if($data) {
  226. $pattern = "/^".$code."/";
  227. if(preg_match($pattern, $data)) {
  228. true を返します。
  229. }
  230. else{
  231. $this->_errorMessage = "エラー:" . $data 。 "|**| コマンド:";
  232. false を返します。
  233. }
  234. }
  235. else{
  236. $this->_errorMessage = "エラー:" .ソケット_strerror(socket_last_error());
  237. false を返します。
  238. }
  239. }
  240. else{
  241. $this->_errorMessage = "エラー:" .ソケット_strerror(socket_last_error());
  242. false を返します。
  243. }
  244. }catch(Exception $e) {
  245. $this->_errorMessage = "エラー:" . $e->getMessage();
  246. }
  247. }
  248. /**
  249. * サーバーへのネットワーク接続を確立します
  250. * @access private
  251. * @return boolean
  252. */
  253. プライベート関数ソケット() {
  254. if(!function_exists("socket_create")) {
  255. $this->_errorMessage = "拡張 php-sockets を有効にする必要があります" ;
  256. false を返します。
  257. }
  258. //创建ソケット资源
  259. $this->_socket =ソケット_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  260. if(!$this->_socket) {
  261. $this->_errorMessage =ソケット_strerror(socket_last_error());
  262. false を返します。
  263. }
  264. //接続サービス
  265. if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
  266. $this->_errorMessage =ソケット_strerror(socket_last_error() );
  267. false を返します。
  268. }
  269. socket_read($this->_socket, 1024);
  270. true を返します。
  271. }
  272. /**
  273. * 关闭ソケット
  274. * @access private
  275. * @return boolean
  276. */
  277. プライベート関数 close() {
  278. if(isset($this->_socket) && is_object($this->_socket)) {
  279. $this->_socket ->close();
  280. true を返します。
  281. }
  282. $this->_errorMessage = "リソースを近づけることはできません";
  283. false を返します。
  284. }
  285. }
  286. /**************************** テスト *********************** ************/
  287. $config = array(
  288. "from" => "********@163.com",
  289. "to" => "******@163.com"、
  290. "件名" => "テスト"、
  291. "本文" => "テスト b>",
  292. "ユーザー名" => "******",
  293. "パスワード" => "パスワード",
  294. );
  295. $mail = 新しい MySendMail();
  296. $mail->setServer("smtp.163.com");
  297. $mail->setMailInfo($config);
  298. if(!$mail->sendMail()){
  299. echo $mail->error();
  300. 1 を返します。
  301. }
复制代

PHP、HTML


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。