SendEmail.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Tool\ShanTaoTool;
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use PHPMailer\PHPMailer\SMTP;
  5. use Tool\ShanTaoTool\Bean\SendEmail\SendEmailByCustomeParamBean;
  6. use Tool\ShanTaoTool\Exception\SendEmailException;
  7. class SendEmail
  8. {
  9. /**
  10. * 发送邮件
  11. * @param $title 邮件标题
  12. * @param $name 发送人名称
  13. * @param $email 收件人邮件账号
  14. * @param $content 邮件内容html格式代码
  15. */
  16. public static function sendEmail($title='',$name='',$email='',$content='')
  17. {
  18. if($title=='' || $name=='' || $email=='' || $content==''){
  19. return "缺少参数";
  20. }
  21. $mail = new PHPMailer(true);
  22. try {
  23. //Server settings
  24. // $mail->SMTPDebug = 2; //启用详细调试输出
  25. $mail->isSMTP(); //使用SMTP
  26. $mail->Host = 'smtp.qq.com'; //将SMTP服务器设置为通过
  27. $mail->SMTPAuth = true; //启用SMTP验证
  28. $mail->Username = '1956694751@qq.com'; // SMTP用户名
  29. $mail->Password = 'latgklkiojvfeicj'; // SMTP密码
  30. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //启用TLS加密;`的PHPMailer :: ENCRYPTION_SMTPS`鼓励
  31. $mail->Port = 465; //要连接的TCP端口,在上面的PHPMailer :: ENCRYPTION_SMTPS中使用465
  32. //Recipients
  33. $mail->setFrom('1956694751@qq.com',$name);
  34. $mail->addAddress($email); //Name is optional
  35. // $mail->addReplyTo('info@example.com', 'Information');
  36. // $mail->addCC('cc@example.com');
  37. // $mail->addBCC('bcc@example.com');
  38. //Attachments
  39. // $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
  40. // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
  41. //Content
  42. $mail->isHTML(true); //Set email format to HTML
  43. $mail->Subject = $title;
  44. $mail->Body = $content;
  45. $mail->send();
  46. return true;
  47. } catch (\Exception $e) {
  48. return false;
  49. }
  50. }
  51. /**
  52. * 发送邮件
  53. * @param SendEmailByCustomeParamBean $sendEmailByCustomeParamBean
  54. * @return bool
  55. * @throws SendEmailException
  56. */
  57. public static function sendEmailByCustome(SendEmailByCustomeParamBean $sendEmailByCustomeParamBean)
  58. {
  59. if(!$sendEmailByCustomeParamBean->getTitle()){
  60. throw new SendEmailException("缺少参数");
  61. }
  62. if(!$sendEmailByCustomeParamBean->getContent()){
  63. throw new SendEmailException("缺少参数");
  64. }
  65. if(!$sendEmailByCustomeParamBean->getEmail()){
  66. throw new SendEmailException("缺少参数");
  67. }
  68. if(!$sendEmailByCustomeParamBean->getName()){
  69. throw new SendEmailException("缺少参数");
  70. }
  71. if(!$sendEmailByCustomeParamBean->getServiceName()){
  72. throw new SendEmailException("缺少参数");
  73. }
  74. if(!$sendEmailByCustomeParamBean->getServicePassword()){
  75. throw new SendEmailException("缺少参数");
  76. }
  77. $mail = new PHPMailer(true);
  78. try {
  79. //Server settings
  80. // $mail->SMTPDebug = 2; //启用详细调试输出
  81. $mail->isSMTP(); //使用SMTP
  82. $mail->Host = 'smtp.qq.com'; //将SMTP服务器设置为通过
  83. $mail->SMTPAuth = true; //启用SMTP验证
  84. $mail->Username = $sendEmailByCustomeParamBean->getServiceName(); // SMTP用户名
  85. $mail->Password = $sendEmailByCustomeParamBean->getServicePassword(); // SMTP密码
  86. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //启用TLS加密;`的PHPMailer :: ENCRYPTION_SMTPS`鼓励
  87. $mail->Port = 465; //要连接的TCP端口,在上面的PHPMailer :: ENCRYPTION_SMTPS中使用465
  88. //Recipients
  89. $mail->setFrom($sendEmailByCustomeParamBean->getServiceName(),$sendEmailByCustomeParamBean->getName());
  90. $mail->addAddress($sendEmailByCustomeParamBean->getEmail()); //Name is optional
  91. // $mail->addReplyTo('info@example.com', 'Information');
  92. // $mail->addCC('cc@example.com');
  93. // $mail->addBCC('bcc@example.com');
  94. //Attachments
  95. // $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
  96. // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
  97. //Content
  98. $mail->isHTML(true); //Set email format to HTML
  99. $mail->Subject = $sendEmailByCustomeParamBean->getTitle();
  100. $mail->Body = $sendEmailByCustomeParamBean->getContent();
  101. $mail->send();
  102. return true;
  103. } catch (\Exception $e) {
  104. return false;
  105. }
  106. }
  107. }