php [php:PHPMailer示例] php库“PHPMailer”示例。 #PHP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [php:PHPMailer示例] php库“PHPMailer”示例。 #PHP相关的知识,希望对你有一定的参考价值。

// GmailのSMTP使って送信

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "name@gmail.com";                 
$mail->Password = "super_secret_password";                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                           

//From email address and name
$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("recepient1@example.com", "Recepient Name");
$mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("reply@yourdomain.com", "Reply");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

//Attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional


if(!$mail->send()) 
{
    Log::info("Mailer Error: " . $mail->ErrorInfo);
} 
else 
{
    Log::info("Message has been sent successfully");
}
<?php

// CakePHP的なコンポネントクラスにしてみる
namespace App\Component;
use PHPMailer;

/**
 * use PHPMailer
 * install: $ composer require phpmailer/phpmailer
 */
class SendMail { 
  
  public $params = [
    'to' => null,
    'subject' => null,
    'body' => null,
    'from' => null,
    'fromname' => null,
  ];
  
  private $results = [
    'status' => false,
    'msg'    => '',
  ];
  
  public function send(){
    $isNull = false;
    foreach ($this->params as $value) {
      if (is_null($value)) $isNull = true;
    }
    if ($isNull) {
      $this->results['status'] = false;
      $this->results['msg'] = "please input all params.";
    }else{    
      mb_language("japanese");  //言語
      mb_internal_encoding("UTF-8");  //内部ENC
      $mail = new PHPMailer();
      $mail->CharSet = "iso-2022-jp";  //文字コード設定
      $mail->Encoding = "7bit";  //エンコーディング
      $mail->AddAddress($this->params['to']);  //宛先(To)をセット
      $mail->From = $this->params['from'];  //差出人(From)をセット
      $mail->FromName = mb_encode_mimeheader(mb_convert_encoding($this->params['fromname'],"JIS","UTF-8"));  //差出人(From名)をセット
      $mail->Subject = mb_encode_mimeheader(mb_convert_encoding($this->params['subject'],"JIS","UTF-8"));   //件名(Subject)をセット
      $mail->Body  = mb_convert_encoding($this->params['body'],"JIS","UTF-8");  //本文(Body)をセット
      if (!$mail->Send()) { /* メール送信 */
        $this->results['status'] = false;
        $this->results['msg'] = "Failed to send mail. Error: ".$mail->ErrorInfo;
      } else {
        $this->results['status'] = true;
        $this->results['msg'] = "Send mail Ok.";
      }
    }
    return $this->results;
  }


/*
 * Controllerからの呼び出し例
 *
 * public function thanks(){
 *   $SendMail = new SendMail();
 *   $SendMail->params['to'] = "dest@address.com";
 *   $SendMail->params['subject'] = "メールフォームから問合せがありました。";
 *   $SendMail->params['from'] = "no-reply@domain.com"
 *   $SendMail->params['fromname'] = "domain administrator";
 *   $SendMail->params['body'] = "WEBサイトより、以下内容でお問合せがありました。\n\n";
 *   // 略
 *   if($SendMail->send()){
 *     echo "お問合せありがとうございました。\n";
 *   }else{
 *     echo "メール送信に失敗しました。お手数ですが、フォーム内容を確認し最初からやり直してください。\n";
 *   }
 * }
 */

}

以上是关于php [php:PHPMailer示例] php库“PHPMailer”示例。 #PHP的主要内容,如果未能解决你的问题,请参考以下文章

php 邮件发送利器 PHPMailer

php使用PHPMailer发送邮件

修改后的class.phpmailer.php

PHP 使用 PHPMailer 发送邮件

使用PHPMAILER实现PHP发邮件功能

php 邮件mailto phpmailer电子邮件php邮件