Email.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\common\util;
  3. use PHPMailer\PHPMailer\Exception;
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. /**
  6. * 动态邮箱类
  7. */
  8. class Email
  9. {
  10. public function sendEmail($toEmail, $tp): int
  11. {
  12. $mail = new PHPMailer();
  13. $mail->isSMTP();// 使用SMTP服务
  14. $mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
  15. $mail->Host = "smtp.qq.com";// 发送方的SMTP服务器地址
  16. $mail->SMTPAuth = true;// 是否使用身份验证
  17. $mail->Username = "845178954@qq.com";// 发送方的qq邮箱用户名,就是你申请qq的SMTP服务使用的qq邮箱
  18. $mail->Password = "dptntlmpifkqbfgb";// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
  19. $mail->SMTPSecure = "ssl";// 使用ssl协议方式
  20. $mail->Port = 465;// 163邮箱的ssl协议方式端口号是465/994
  21. try {
  22. $mail->setFrom("845178954@qq.com", "皮编程");
  23. } catch (Exception $e) {
  24. }// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
  25. try {
  26. $mail->addAddress($toEmail, '教务');
  27. } catch (Exception $e) {
  28. }// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
  29. try {
  30. $mail->addReplyTo("845178954@qq.com", "皮编程");
  31. } catch (Exception $e) {
  32. }// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
  33. //$mail->addCC("xxx@163.com");// 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址(这个人也能收到邮件)
  34. //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
  35. //$mail->addAttachment("bug0.jpg");// 添加附件
  36. $mail->isHTML();
  37. $mail->Subject = "【商品发货通知】";// 邮件标题
  38. try {
  39. $mail->MsgHTML($tp);
  40. } catch (Exception $e) {
  41. }// 邮件正文
  42. //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用
  43. if (!$mail->send()) {// 发送邮件
  44. // return $this->api("Mailer Error: ".$mail->ErrorInfo, '发送失败~', 400);
  45. return 400;
  46. } else {
  47. return 200;
  48. }
  49. }
  50. public function getTp($phone, $goodsname, $goodstype, $num, $price, $total_amount, $status_text, $password = ''): string
  51. {
  52. return '<!DOCTYPE html>
  53. <html lang="">
  54. <head>
  55. <title></title>
  56. <meta charset="utf-8">
  57. <meta name="viewport" content="width=device-width, initial-scale=1">
  58. <style>
  59. table{
  60. border: 1px solid red;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="container mt-3">
  66. <h2>订单信息: </h2>
  67. <h5>账号: ' . $phone . '</h5>
  68. <h5>商品: ' . $goodsname . '</h5>
  69. <h5>类型: ' . $goodstype . '</h5>
  70. <h5>数量: ' . $num . '</h5>
  71. <h5>单价: ' . $price . '</h5>
  72. <h5>总金额: ' . $total_amount . '</h5>
  73. <h5>状态: ' . $status_text . '</h5>
  74. <h5>卡密: ' . $password . '</h5>
  75. </div>
  76. </body>
  77. </html>';
  78. }
  79. }