Mail.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace KIF\Mail;
  3. use KIF\Core\Config;
  4. use KIF\Mail\PHPMailer;
  5. use KIF\Verify;
  6. /**
  7. *
  8. * 邮件发送类。封装KIF\Mail\PHPMailer
  9. * @author gaoxiaogang
  10. *
  11. */
  12. class Mail
  13. {
  14. protected $objPHPMailer;
  15. /**
  16. *
  17. * 用于发smtp邮件的配置信息
  18. * @var array
  19. */
  20. protected $config;
  21. public function __construct(array $options = null)
  22. {
  23. $this->config = Config::getInstance()->get('smtp');
  24. $this->objPHPMailer = new PHPMailer();
  25. $this->objPHPMailer->IsSMTP();
  26. $this->objPHPMailer->SMTPAuth = true;
  27. if(isset($options['HOSTNAME'])){
  28. $this->objPHPMailer->Host = $options['HOSTNAME'];
  29. }else{
  30. $this->objPHPMailer->Host = $this->config['host'];
  31. }
  32. if(isset($options['PORT'])){
  33. $this->objPHPMailer->Port = $options['PORT'];
  34. }else{
  35. $this->objPHPMailer->Port = $this->config['port'];
  36. }
  37. if(isset($options['TIMEOUT'])){
  38. $this->objPHPMailer->Timeout = $options['TIMEOUT'];
  39. }else{
  40. $this->objPHPMailer->Timeout = $this->config['timeout'];
  41. }
  42. if(isset($options['USERNAME'])){
  43. $this->objPHPMailer->Username = $options['USERNAME'];
  44. }else{
  45. $this->objPHPMailer->Username = $this->config['username'];
  46. }
  47. if(isset($options['PASSWORD'])){
  48. $this->objPHPMailer->Password = $options['PASSWORD'];
  49. }else{
  50. $this->objPHPMailer->Password = $this->config['password'];
  51. }
  52. if(isset($options['CHARSET'])){
  53. $this->objPHPMailer->CharSet = $options['CHARSET'];
  54. }else{
  55. $this->objPHPMailer->CharSet = $this->config['charset'];
  56. }
  57. if(isset($options['Encoding'])){
  58. $this->objPHPMailer->Encoding = $options['Encoding'];
  59. }else{
  60. $this->objPHPMailer->Encoding = "base64";
  61. }
  62. }
  63. /**
  64. *
  65. * 发送邮件
  66. * @param array $info 格式:array(
  67. * "from" => "",//发件人邮箱 默认为(noreply@gaojie100.com)
  68. * "fromname" => "",//发件人 默认为(高街时尚网)
  69. * "address" => array( //必填 收件人
  70. * array(
  71. * "mail" => "",//必填 收件人邮箱
  72. * "name" => "",//收件人姓名
  73. * )
  74. * ……
  75. * ),
  76. * "ccaddress" => array( //抄送
  77. * array(
  78. * "mail" => "",//必填 收件人邮箱
  79. * "name" => "",//收件人姓名
  80. * )
  81. * ……
  82. * ),
  83. * "bccaddress" => array( //密送
  84. * array(
  85. * "mail" => "",//必填 收件人邮箱
  86. * "name" => "",//收件人姓名
  87. * )
  88. * ……
  89. * ),
  90. * "attachment" => array(
  91. * "",//附件1 服务器文件全路径 /var/tmp/file.tar.gz
  92. * "",//附件2
  93. * ),
  94. * "ishtml" => true|false,//yes or no, send as HTML
  95. * "subject" => "",//必填 邮件主题
  96. * "body" => "",//当为空时使用subject的值
  97. * );
  98. * @param int $retry_times 发送失败时的重试次数,默认为0,即不重试
  99. * @param int $retry_frequency 重试频率,单位:秒。默认为1秒
  100. * @return bool
  101. */
  102. public function send($info, $retry_times = 0, $retry_frequency = 1)
  103. {
  104. $from = isset($info['from'])?$info['from']:'';
  105. if($from){
  106. if(!Verify::email($from)){
  107. echo '邮箱格式不真确';
  108. exit;
  109. }
  110. $this->objPHPMailer->From = $from;
  111. }else{
  112. $this->objPHPMailer->From = $this->config['from'];
  113. }
  114. $fromname = isset($info['fromname'])?$info['fromname']:'';
  115. if($fromname){
  116. $this->objPHPMailer->FromName = $fromname;
  117. }else{
  118. $this->objPHPMailer->FromName = $this->config['fromname'];
  119. }
  120. if(!$info["address"]){
  121. echo '收件人邮箱不能为空';
  122. exit;
  123. }
  124. if (is_array($info["address"])){
  125. foreach($info["address"] as $address){
  126. if(!$address['mail']){
  127. echo '收件人邮箱不能为空';
  128. exit;
  129. }
  130. $this->objPHPMailer->AddAddress($address["mail"], $address["name"]);
  131. }
  132. }else{
  133. $address = explode(',', $info["address"]);
  134. foreach($address as $add){
  135. if(!$add) continue;
  136. $this->objPHPMailer->AddAddress(trim($add));
  137. }
  138. }
  139. if (is_array($info["ccaddress"])){
  140. foreach($info["ccaddress"] as $address){
  141. if(!$address["mail"]){
  142. echo '抄送收件人邮箱不能为空';
  143. exit;
  144. }
  145. $this->objPHPMailer->AddCC($address["mail"], $address["name"]);
  146. }
  147. }else{
  148. $address = explode(',', $info["ccaddress"]);
  149. foreach($address as $add){
  150. if(!$add) continue;
  151. $this->objPHPMailer->AddCC(trim($add));
  152. }
  153. }
  154. if (is_array($info["bccaddress"])){
  155. foreach($info["bccaddress"] as $address){
  156. if(!$address["mail"]){
  157. echo '密送收件人邮箱不能为空';
  158. exit;
  159. }
  160. $this->objPHPMailer->AddBCC($address["mail"], $address["name"]);
  161. }
  162. }else{
  163. $address = explode(',', $info["bccaddress"]);
  164. foreach($address as $add){
  165. if(!$add) continue;
  166. $this->objPHPMailer->AddBCC(trim($add));
  167. }
  168. }
  169. $attachment = isset($info["attachment"])?$info["attachment"]:'';
  170. if($attachment)
  171. {
  172. $attachmenta = explode(',', $attachment);
  173. foreach($attachmenta as $tmpV)
  174. {
  175. if(!$tmpV) continue;
  176. $this->objPHPMailer->AddAttachment($tmpV);
  177. }
  178. }
  179. $ishtml = isset($info["ishtml"])?$info["ishtml"]:'';
  180. if($ishtml){
  181. $this->objPHPMailer->IsHTML($ishtml);
  182. }else{
  183. $this->objPHPMailer->IsHTML(true);
  184. }
  185. if(!$info["subject"]){
  186. echo '主题不能为空!';
  187. exit;
  188. }
  189. $this->objPHPMailer->Subject = $info["subject"];
  190. $body = isset($info["body"])?$info["body"]:'';
  191. if(!$body){
  192. $this->objPHPMailer->Body = $info["subject"];
  193. }else{
  194. $this->objPHPMailer->Body = $body;
  195. }
  196. return $this->objPHPMailer->Send();
  197. while (true) {
  198. $tmpSendResult = $this->objPHPMailer->Send();
  199. if ($tmpSendResult) {
  200. return $tmpSendResult;
  201. }
  202. $retry_times--;
  203. if ($retry_times < 0) {
  204. break;
  205. }
  206. sleep($retry_frequency);
  207. }
  208. }
  209. }