先在GitHub找到PHPMailer 并下载
https://github.com/PHPMailer/PHPMailer //PHPMailer的GitHub地址
将下载的PHPMailer放在ThinkPHP/Library/Vendor目录下
在config.php下添加邮件发送配置信息
在Common/function.php下编写mailSend(array $mailInfo)函数
/** * 邮件发送,可以多图片,多附件发送 * @param $mailInfo $mailInfo = array( //二维数组 * 'address' => 'xx@xx.com', //收件人邮箱 * 'subject' => '这里是邮件主题', * 'body' => "<h2>你好</h2> 这是一个邮件, * <a href='#'></a> * <hr><img alt='这是一张图片' src='https://www.jb51.net/cid:test_id1'>",//图片src对应图片标识 * 'img' => array( * 'path' => 'test/1.jpg', //可以使用相对路径 * 'cid' => 'test_id1', //附件内容标识 * 'name' => '1.jpg' * ), * 'attachment'=> array( * 'path' => 'text/text.txt', * 'name' => '附件.txt' * ), * ); * @return bool * @throws Exception * @throws phpmailerException */ function mailSend($mailInfo){ //date_default_timezone_set('Asia/Shanghai');//设置时区 vendor('PHPMailer.class#phpmailer'); $mail = new \PHPMailer(); $mailConfig = C('MAIL_CONF');//获取mail配置 //dump($mailConfig);exit; //配置项 $mail->isSMTP(); $mail->Host = $mailConfig['MAIL_HOST']; $mail->Port = $mailConfig['MAIL_PORT'];//端口 $mail->SMTPAuth = $mailConfig['MAIL_SMTPAUTH'];//启用SMTP认证 $mail->CharSet = $mailConfig['MAIL_CHARSET']; $mail->Encoding = $mailConfig['MAIL_ENCODING']; $mail->Username = $mailConfig['MAIL_NAME']; //发送邮箱 $mail->Password = $mailConfig['MAIL_PWD']; $mail->FromName = $mailConfig['MAIL_FROMNAME']; //发件人名字 //内容 $mail->addAddress($mailInfo['address']);//收件人邮箱 $mail->Subject = $mailInfo['subject']; //邮件主题 //图片以及附件 $mail->isHTML(true); //支持html格式内容 //最后一个参数可不写,默认为原文件名 $mail->addEmbeddedImage($mailInfo['img']['path'],$mailInfo['img']['cid'],$mailInfo['img']['name']);//图片路径,图片cid,图片名称 $mail->addAttachment($mailInfo['attachment']['path'],$mailInfo['attachment']['name']);//添加附件,并指定名称 //邮件主体 $mail->Body = $mailInfo['body'];//发送 return $mail->send()?true:false; }