composer require phpmailer/phpmailer
<?php header('content-type:text/html;charset=utf-8;'); set_time_limit(3600); require "vendor/autoload.php"; $send_res = sendEmail('主题', '内容', 'jianlong@123.io');die; // phpmailer 的使用 // sendEmail('主题', '内容', '收件邮箱', '附件'); function sendEmail($subject, $contents, $to_email, $attach_file='') { $mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->isSMTP(); $mail->CharSet = 'utf8'; //设定邮件编码 $mail->Host = 'smtp.exmail.qq.com'; //SMTP服务器 $mail->SMTPAuth = true; $mail->Username = 'noreply@jianlong.io'; //邮箱用户名 $mail->Password = '密码或者授权码'; //密码或者授权码 $mail->SMTPSecure = "ssl"; $mail->Port = 465; //服务器端口 25 或者465 具体要看邮箱服务器支持 $mail->IsHTML(true); $mail->setFrom("noreply@jianlong.io", "jianlong"); //发件人 //$mail->addReplyTo('xxxx@163.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致 $mail->addAddress($to_email); //$mail->addAddress('ellen@example.com'); // 可添加多个收件人 //$mail->addCC('cc@example.com'); //抄送 //$mail->addBCC('bcc@example.com'); //密送 if (!empty($attach_file)) { $file_name = basename($attach_file); $mail->AddAttachment($attach_file, $file_name); // 发送附件并且重命名 } $mail->Subject = $subject; $mail->Body = $contents; //$mail->AltBody = '如果邮件客户端不支持HTML则显示此内容'; if (!$mail->send()) { trace($mail->ErrorInfo, 'error'); return 0; } else { return 1; } }