ホームページ  >  記事  >  バックエンド開発  >  TP5 は、電子メール送信サービスのカプセル化と添付ファイルの送信例を実装します。

TP5 は、電子メール送信サービスのカプセル化と添付ファイルの送信例を実装します。

黄舟
黄舟オリジナル
2017-10-31 09:04:073744ブラウズ

この記事の例では、ThinkPHP5 パッケージ メール送信サービスの具体的なコードを共有します。具体的な内容は次のとおりです

1. Composer は phpmailer

composer require phpmailer/phpmailer

2 をインストールします。

拡張ディレクトリのextend/Mail.phpファイルにカプセル化して配置しました。内容は次のとおりです:

<?php
/**
* 邮件服务类
*/
class Mail extends \PHPMailer
{
  function construct()
  {
    date_default_timezone_set(&#39;PRC&#39;);             // 默认时区设置
 
    $this->CharSet = config(&#39;mail.charset&#39;);          // 邮件编码设置
    $this->isSMTP();                      // 启用SMTP服务
    $this->SMTPDebug = config(&#39;mail.smtp_debug&#39;);       // Debug模式级别
    $this->Debugoutput = config(&#39;mail.debug_output&#39;);     // Debug输出类型
    $this->Host = config(&#39;mail.host&#39;);             // SMTP服务器地址
    $this->Port = config(&#39;mail.port&#39;);             // 端口号
    $this->SMTPAuth = config(&#39;mail.smtp_auth&#39;);        // SMTP登录认证
    $this->SMTPSecure = config(&#39;mail.smtp_secure&#39;);      // SMTP安全协议
    $this->Username = config(&#39;mail.username&#39;);         // SMTP登录邮箱
    $this->Password = config(&#39;mail.password&#39;);         // SMTP登录密码
    $this->setFrom(config(&#39;mail.from&#39;), config(&#39;mail.from_name&#39;));      // 发件人邮箱和名称
    $this->addReplyTo(config(&#39;mail.reply_to&#39;), config(&#39;mail.reply_to_name&#39;)); // 回复邮箱和名称
  }
 
  /**
   * 发送邮件
   * @param [type] $toMail   收件人地址
   * @param [type] $toName   收件人名称
   * @param [type] $subject   邮件主题
   * @param [type] $content   邮件内容,支持html
   * @param [type] $attachment 附件列表。文件路径或路径数组
   * @return [type]       成功返回true,失败返回错误消息
   */
  function sendMail($toMail, $toName, $subject, $content, $attachment = null)
  {
    $this->addAddress($toMail, $toName);
    $this->Subject = $subject;
    $this->msgHTML($content);
     
    if($attachment) { // 添加附件
      if(is_string($attachment)){
        is_file($attachment) && $this->AddAttachment($attachment);
      }
      else if(is_array($attachment)){
        foreach ($attachment as $file) {
          is_file($file) && $this->AddAttachment($file);
        }
      }   
    }
 
    if(!$this->send()){ // 发送
      return $this->ErrorInfo;
    }
    else{
      return true;
    }
  }
}

注: 添付ファイルを送信する場合は、英語のパスを使用することをお勧めします。中国語のパスを使用すると、添付ファイルの送信に失敗したり、添付ファイルなしで電子メールを受信したりする可能性があります。

上記で必要ないくつかの設定パラメータは、拡張機能設定ディレクトリ application/extra/mail.php ファイルに配置されています。内容は次のとおりです:

<?php
/**
 * 邮件服务相关配置
 */
return [
  &#39;charset&#39; => &#39;utf-8&#39;,         // 邮件编码
  &#39;smtp_debug&#39; => 0,           // Debug模式。0: 关闭,1: 客户端消息,2: 客户端和服务器消息,3: 2和连接状态,4: 更详细
  &#39;debug_output&#39; => &#39;html&#39;,       // Debug输出类型。`echo`(默认),`html`,或`error_log`
  &#39;host&#39; => &#39;smtp.126.com&#39;,       // SMTP服务器地址
  &#39;port&#39; => 465,             // 端口号。默认25
  &#39;smtp_auth&#39; => true,          // 启用SMTP认证
  &#39;smtp_secure&#39; => &#39;ssl&#39;,        // 启用安全协议。&#39;&#39;(默认),&#39;ssl&#39;或&#39;tls&#39;,留空不启用
  &#39;username&#39; => &#39;yourname@example.com&#39;, // SMTP登录邮箱
  &#39;password&#39; => &#39;yourpassword&#39;,     // SMTP登录密码。126邮箱使用客户端授权码,QQ邮箱用独立密码
  &#39;from&#39; => &#39;from@example.com&#39;,     // 发件人邮箱
  &#39;from_name&#39; => &#39;name&#39;,         // 发件人名称
  &#39;reply_to&#39; => &#39;&#39;,           // 回复邮箱的地址。留空取发件人邮箱
  &#39;reply_to_name&#39; => &#39;&#39;,         // 回复邮箱人名称。留空取发件人名称
]; 

注: 通常、デフォルトのポートは 25 です。セキュリティ プロトコル ssl が使用されている場合、ポート番号は通常 465 または 587 です。たとえば、126 メールボックスです。 Alibaba Cloud サーバーでは、安全でないプロトコルのポート 25 が禁止されているため、安全なプロトコルを使用することをお勧めします。

その他の構成パラメータについては、ソース コードを確認してください: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

3. のメソッド内でテストします。コントローラー、テスト コードを追加します:

public function mail()
{
  $mail = new \Mail;
  $ok = $mail->sendMail(&#39;xxxxxxxxx@qq.com&#39;, &#39;mingc&#39;, &#39;邮件来了&#39;, &#39;<p style="color: #f60; font-weight: 700;">恭喜,邮件成功!</p>&#39;, &#39;C:/Users/Administrator/Desktop/body.bmp&#39;);
  var_dump($ok);
}
  

ここでは 126 メールボックス、セキュリティ プロトコル ssl、ポート番号 465 を使用し、HTML コンテンツを送信します。テストは成功しました:

以上がTP5 は、電子メール送信サービスのカプセル化と添付ファイルの送信例を実装します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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