在php ci框架中使用swiftmailer

<?php
require_once 'SwiftMailer/swift_required.php';

class M_mailer extends CI_Model {

	/*
	*@send mail
	*@package 郵件信息
	*
	*@package['mail_myname']
	*@package['mail_username']
	*@package['mail_password']
	*@package['mail_smtp']
	*@pagkage['mail_port']
	*@package['to_list'] => array( $email => $name ,  $email2 => $name2);
	*@package['title']
	*@package['content']
	*/
	public function send_mail( $package) {

		$transport = Swift_SmtpTransport::newInstance( $package['mail_smtp'], $package['mail_port']);
 		$transport->setUsername( $package['mail_username']);
 		$transport->setPassword( $package['mail_password']);
 
 		$mailer = Swift_Mailer::newInstance( $transport);
 
 		$message = Swift_Message::newInstance();
 		$message->setFrom(array( $package['mail_username'] => $package['mail_myname']));
 		$message->setTo( $package['to_list']);
 		$message->setSubject( $package['title']);
 		$message->setBody( $package['content'], 'text/html', 'utf-8');
 		//$message->attach( Swift_Attachment::fromPath('pic.jpg', 'image/jpeg')->setFilename('rename_pic.jpg'));
 		try{
  			echo $mailer->send($message);
 		}catch (Swift_ConnectionException $e){
  			echo 'There was a problem communicating with SMTP: ' . $e->getMessage();
 		}
	}
寫在了model裏面,swiftmailer發郵件效率比phpmailer高不少,使用起來很方便,若是不喜歡CI自帶的郵件功能,能夠集成swiftmailer帶CI,方法也很簡單,如上代碼
相關文章
相關標籤/搜索