CEmail.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\helpers;
  10. use Yii;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class CEmail
  16. {
  17. public static function getMailer(){
  18. return Yii::$app->mailer;
  19. }
  20. public static function getMailOb($from,$to,$title,$content,$is_html=1){
  21. if(!$from || !$to || !$title || !$content){
  22. return false;
  23. }
  24. # 验证邮箱格式是否正确
  25. if(!self::email_validation($from)){
  26. return false;
  27. }
  28. # 验证邮箱格式是否正确
  29. if(!self::email_validation($to)){
  30. return false;
  31. }
  32. $m = self::getMailer()->compose()
  33. ->setFrom($from)
  34. ->setTo($to)
  35. ->setSubject($title);
  36. if($is_html){
  37. $m->setHtmlBody($content);
  38. }else{
  39. $m->setTextBody($content);
  40. }
  41. return $m;
  42. }
  43. # 1.发送一封邮件
  44. # $from $to $title $content 不能为空。
  45. public static function sendMail($from,$to,$title,$content,$is_html=1){
  46. $m = self::getMailOb($from,$to,$title,$content,$is_html);
  47. if($m){
  48. $m->send();
  49. return true;
  50. }
  51. return false;
  52. }
  53. # 2.批量发送邮件
  54. /*
  55. 参数:数组格式如下:
  56. $arr = [
  57. [
  58. 'from' =>'zqy234@126.com',
  59. 'to' =>'3727@gmail.com',
  60. 'title' =>'my111',
  61. 'content' =>'<div>111</div>',
  62. 'is_html' => 1,
  63. ],
  64. [
  65. 'from' =>'zqy234@126.com',
  66. 'to' =>'terry@gmail.com',
  67. 'title' =>'to tototto',
  68. 'content' =>'hello ddd',
  69. 'is_html' => 0,
  70. ],
  71. ];
  72. forece 代表多送多个邮件,其中一个邮件的格式或者数据为空的情况下,其他符合
  73. 标准的邮件是否发送
  74. force=1,代表其他符合格式的邮件发送
  75. force=0,如果某个邮件内容有问题,全部不发送。
  76. */
  77. public static function sendMultipleEmail($arr,$forece=1){
  78. $messages = [];
  79. foreach ($arr as $one) {
  80. $from = isset($one['from']) ? $one['from'] : '';
  81. $to = isset($one['to']) ? $one['to'] : '';
  82. $title = isset($one['title']) ? $one['title'] : '';
  83. $content = isset($one['content']) ? $one['content'] : '';
  84. $is_html = isset($one['is_html']) ? $one['is_html'] : 0;
  85. $m = self::getMailOb($from,$to,$title,$content,$is_html);
  86. if(!$m){
  87. if(!$forece){ #如果数据格式有问题,force为0,则全部不发送
  88. return false;
  89. }
  90. }else{
  91. $messages[] = $m;
  92. }
  93. }
  94. if(!empty($messages)){
  95. $count = count($messages);
  96. self::getMailer()->sendMultiple($messages);
  97. # 返回发送的邮件的个数。
  98. return $count;
  99. }
  100. return false;
  101. }
  102. # 3.验证邮箱格式是否正确
  103. public static function email_validation($mail)
  104. {
  105. if($mail != '')
  106. {
  107. if(preg_match("/^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$/", $mail))
  108. {
  109. return true;
  110. }
  111. else
  112. {
  113. return false;
  114. }
  115. }
  116. else
  117. {
  118. return false;
  119. }
  120. }
  121. }