thinkphp用swiftmailer发邮件demo
QQ邮箱
include_once APPPATH . 'libraries/swiftmailer/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('smtp.qq.com', 465, 'ssl') ->setUsername('11111@qq.com') ->setPassword('asfacasafa'); //QQ邮箱授权码 $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance() ->setSubject('测试') ->setFrom(array( '11111@qq.com' => '牛牛', )) ->setTo(array('22222@qq.com')) ->setContentType('text/html') ->setCharset('utf-8') ->setBody('测试内容'); var_dump($mailer->send($message));
公司邮箱
include_once APPPATH . 'libraries/swiftmailer/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('mail.wifire.net', 587) ->setUsername('cccc@wifire.net') ->setPassword('wvasecae23'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance() ->setSubject('测试') ->setFrom(array( 'cccc@fastweb.com.cn' => '云安全管理员', )) ->setTo(array('3333@qq.com')) ->setContentType('text/html') ->setCharset('utf-8') ->setBody('测试内容'); var_dump($mailer->send($message)); include_once APPPATH . 'libraries/swiftmailer/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('mail.wifire.net', 587) ->setUsername('cccc@wifire.net') ->setPassword('casd2323'); $mailer =Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance() ->setSubject($subject) ->setFrom(array('cccc@fastweb.com.cn' =>'云安全管理员')) ->setTo($to) ->setContentType('text/html') ->setCharset('utf-8') ->setBody($content); $cc != '' && $message->setCc($cc); $bcc != '' && $message->setBcc($bcc); return $mailer->send($message);
163邮箱
注意:163会自动检测内容是否是垃圾内容。测试的时候,由于标题和内容我随便填写了一下,导致邮件发送失败。
错误码参考:http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
include_once APPPATH . 'libraries/swiftmailer/swift_required.php'; $transport = \Swift_SmtpTransport::newInstance('smtp.163.com', 25) ->setUsername('abc@163.com') ->setPassword('xuxu1016'); $mailer = new \Swift_Mailer($transport); $subject = "下午有事"; $send_list = array('asd@qq.com'); $body = "下午有事,不去上班了"; $message = new \Swift_Message(); $message->setSubject($subject) ->setFrom(['abc@163.com' => 'xuxu']) ->setTo($send_list) ->setCharset('utf-8') ->setBody($body, 'text/html'); $success = $mailer->send($message, $errorList); print_r($success);