Heim  >  Artikel  >  Backend-Entwicklung  >  TP5 implementiert die Kapselung des E-Mail-Versanddienstes und Beispiele für das Senden von Anhängen

TP5 implementiert die Kapselung des E-Mail-Versanddienstes und Beispiele für das Senden von Anhängen

黄舟
黄舟Original
2017-10-31 09:04:073744Durchsuche

Das Beispiel in diesem Artikel teilt den spezifischen Code des ThinkPHP5-Pakets Mail-Versand als Referenz. Der spezifische Inhalt lautet wie folgt:

1. Composer installiert phpmailer

composer require phpmailer/phpmailer

2. Die Mail-Service-Klasse ist in ThinkPHP gekapselt

Ich kapsele sie in die Datei extension/Mail.php des Erweiterungsverzeichnisses, der Inhalt lautet wie folgt:

<?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;
    }
  }
}

Hinweis: Beim Versenden von Anhängen wird empfohlen, den englischen Pfad zu verwenden. Chinesische Pfade können dazu führen, dass Anhänge nicht gesendet werden und E-Mails ohne Anhang nicht empfangen werden.

Einige der oben erforderlichen Konfigurationsparameter habe ich in die Datei Erweiterung Konfigurationsverzeichnis application/extra/mail.php eingefügt. Der Inhalt lautet wie folgt:

<?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;,         // 回复邮箱人名称。留空取发件人名称
]; 

Hinweis: Im Allgemeinen ist der Standardport 25. Wenn das Sicherheitsprotokoll SSL verwendet wird, ist die Portnummer normalerweise 465 oder 587. Zum Beispiel 126 Postfach. Es wird empfohlen, ein sicheres Protokoll zu verwenden, da Alibaba Cloud-Server Port 25 nicht sicherer Protokolle verbieten.

Weitere Konfigurationsparameter finden Sie im Quellcode: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

3 .Test

Fügen Sie in der Methode im Controller den Testcode hinzu:

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);
}
  

Hier verwende ich 126 Mailbox, Sicherheitsprotokoll SSL, Portnummer 465, senden HTML-Inhalt, Testerfolg:

Das obige ist der detaillierte Inhalt vonTP5 implementiert die Kapselung des E-Mail-Versanddienstes und Beispiele für das Senden von Anhängen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn