码迷,mamicode.com
首页 > 编程语言 > 详细

php发邮件:swiftmailer, php邮件库——swiftmailer

时间:2017-10-16 18:07:24      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:etc   端口   参数   ade   发送邮件   性能   receipt   htm   class   

php发邮件:swiftmailer, php邮件库——swiftmailer

最近看到一个好的php邮件库,与phpmailer作用一样,但性能比phpmailer好,尤其是在处理附件的能力上,发送邮件成功的几率也高。

github地址:https://github.com/swiftmailer/swiftmailer.git

 

require_once ("lib/swift_required.php");

// 创建Transport对象,设置邮件服务器和端口号,并设置用户名和密码以供验证
$transport = Swift_SmtpTransport::newInstance(‘smtp.163.com‘, 25)
->setUsername(‘username@163.com‘)
->setPassword(‘password‘);

// 创建mailer对象
$mailer = Swift_Mailer::newInstance($transport);

// 创建message对象
$message = Swift_Message::newInstance();

// 设置邮件主题
$message->setSubject(‘这是一份测试邮件‘)

// 设置邮件内容,可以省略content-type
->setBody(
    ‘<html>‘ .
    ‘ <head></head>‘ .
    ‘ <body>‘ .
    ‘ Here is an image <img src="‘ . // 内嵌文件
    $message->embed(Swift_Image::fromPath(‘image.jpg‘)) .
    ‘" alt="Image" />‘ .
    ‘ Rest of message‘ .
    ‘<a href="http://www.baidu.com">百度</a>‘.
    ‘ </body>‘ .
    ‘</html>‘,
    ‘text/html‘
);

// 创建attachment对象,content-type这个参数可以省略
$attachment = Swift_Attachment::fromPath(‘image.jpg‘, ‘image/jpeg‘)
->setFilename(‘cool.jpg‘);

// 添加附件
$message->attach($attachment);

// 用关联数组设置收件人地址,可以设置多个收件人
$message->setTo(array(‘to@qq.com‘ => ‘toName‘));

// 用关联数组设置发件人地址,可以设置多个发件人
$message->setFrom(array(
    ‘from@163.com‘ => ‘fromName‘,
));

// 添加抄送人
 $message->setCc(array(
      ‘Cc@qq.com‘ => ‘Cc‘
 ));

// 添加密送人
$message->setBcc(array(
      ‘Bcc@qq.com‘ => ‘Bcc‘
));

// 设置邮件回执
$message->setReadReceiptTo(‘receipt@163.com‘);

// 发送邮件
$result = $mailer->send($message);

  

测试代码,测试例子:

 $Requests = __DIR__ . ‘/../../../vendor/swiftmailer/swiftmailer/lib/swift_required.php‘;
        require_once ($Requests);
        //Requests::register_autoloader ();

		// 创建Transport对象,设置邮件服务器和端口号,并设置用户名和密码以供验证
		$transport = \Swift_SmtpTransport::newInstance(‘smtp.exmail.qq.com‘, 25)
		->setUsername(‘business@VariFlight.com‘)
		->setPassword(‘Youxikaishi04‘);

		// 创建mailer对象
		$mailer = \Swift_Mailer::newInstance($transport);

		// 创建message对象
		$message = \Swift_Message::newInstance();

		// 设置邮件主题
		$message->setSubject(‘这是一份测试邮件‘)->setBody(‘aaaa‘);
		

		// 用关联数组设置收件人地址,可以设置多个收件人
		$message->setTo(array(‘muyang@variflight.com‘ => ‘muyang‘));

		// 用关联数组设置发件人地址,可以设置多个发件人
		$message->setFrom(array(
			‘business@VariFlight.com‘ => ‘shandongair‘,
		));		

		// 发送邮件
		$result = $mailer->send($message);


       echo "aaa";
	   exit;

  

 

php发邮件:swiftmailer, php邮件库——swiftmailer

标签:etc   端口   参数   ade   发送邮件   性能   receipt   htm   class   

原文地址:http://www.cnblogs.com/achengmu/p/7677799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!