基本用法

  1. $mail = new PHPMailer(true);
  2. try {
  3. $mail->SMTPDebug = SMTP::DEBUG_OFF; // Enable verbose debug output
  4. $mail->isSMTP(); // Send using SMTP
  5. $mail->Host = 'smtp.163.com'; // Set the SMTP server to send through
  6. $mail->SMTPAuth = true; // Enable SMTP authentication
  7. $mail->Username = 'xxx@163.com'; // SMTP username
  8. $mail->Password = 'xxx'; // SMTP password
  9. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
  10. $mail->Port = 465; // TCP port to connect to
  11. //Recipients
  12. $mail->setFrom('xxx@163.com', '意见和反馈');
  13. $mail->addAddress('xxx@qq.com'); // Add a recipient
  14. //$mail->addReplyTo('info@example.com', 'Information');
  15. //$mail->addCC('cc@example.com');
  16. //$mail->addBCC('bcc@example.com');
  17. // Attachments
  18. //$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
  19. //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
  20. // Content
  21. $mail->isHTML(true); // Set email format to HTML
  22. $mail->Subject = 'xxx';
  23. $mail->Body = $html;
  24. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  25. $mail->send();
  26. } catch (Exception $e) {
  27. Db::name('error_log')->insert([
  28. 'type' => __CLASS__.'.'.__METHOD__,
  29. 'content' => $e->getMessage().';'.$mail->ErrorInfo,
  30. ]);
  31. throw new \Exception($mail->ErrorInfo);
  32. }