扩展安装
在phpstorm下 Terminal 执行语句
composer require phpmailer/phpmailer
举例模板
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // 不开调试设置为0
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // 一或多个邮箱详情,例如smtp.163.com之类
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP 写自己的企业邮箱或者其它邮箱
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, 无其他要求便ssl
$mail->Port = 587; // TCP port to connect to ssl的端口465,其它去查找
//Recipients
$mail->setFrom('from@example.com', 'Mailer'); // 发送邮箱与备注名
$mail->addAddress('joe@example.net', 'Joe User'); // 接受对象的邮箱以及名
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
// Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject'; //这是邮件的title
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; //邮件的内容
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; //这个没必要写
$mail->send(); //可以直接返回这个
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
简单实例
use PHPMailer\PHPMailer\PHPMailer; //这两个一般会自都导入的
use PHPMailer\PHPMailer\Exception;
function mailto($to, $title, $content)
{
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->CharSet = 'utf-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.163.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '你的邮箱账号@163.com'; // SMTP username
$mail->Password = 你的邮箱密码'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('目的邮箱地址', '备注名');
$mail->addAddress($to); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $title;
$mail->Body = $content;
return $mail->send();
} catch (Exception $e) {
exception($mail->ErrorInfo,1001);
}
}
如有疑问,可通过qq:2421164869联系,请注明来源