给新手的指导:用SMTP发送邮件

类别:译文      发布日期:2010-04-29

PHP中有一个非常简单的发送文本邮件的函数,假设你要发送附件或者通过SMTP发送邮件会是怎样一种情况呢?这时候就要用到一个更高级的脚本。这是因为 标准的发送邮件函数只有有限的几个标准功能。有许多理由让你在web应用中利用SMTP协议发送邮件,例如:
  • 许多共享主机提供商为了安全 的原因禁止使用PHP的mail()函数。
  • 如果你用了简单信息传输协议(SMTP),你的应用程序会更加灵活。这种办法再也不会让你的 发送邮件的函数被服务器端口或者邮件配置文件所限制。
  • SMTP功能强大并且安全(用了SSL)
本文将选取三个比较大型的允许用SMTP协议发送邮件及其附件的PHP项目做一下对比。
  1. Zend Framework 框架中 包含的邮件类。(http://framework.zend.com/)
  2. Swift Mailer (http://swiftmailer.org/)
  3. PHPMailer (http://phpmailer.worxware.com)
我们重温了一下这三个PHP类,因为他们是为PHP5而写的并且经常更新。在回顾的时候我们会提供例子跟文档。可以肯定的是这三个类功能很强大并提供了若干适合绝大多数的web应用的函数。由于本文是针对初级PHP开发人员的,所以只列举其中的几个函数。
实例:
在本例中,我们用SMTP发送一个带有图片附件的纯文本邮件信息。有一个好消息:作为一个有多年php开发经验的人,我会利用已经提供的例子,在很短的时间内做讲解。

Zend Mail Class

复制内容到剪贴板折叠PHP 代码
  1. ini_set('include_path''.:/path2directory/ZendFramework/library/');  
  2. require_once 'Zend/Loader.php';  
  3. Zend_Loader::loadClass('Zend_Mail');  
  4. Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');  
  5.    
  6. $config = array('auth' => 'login',   
  7.               'username' => 'smtpUser',  
  8.               'password' => 'smtpPassword');  
  9.    
  10. $transport = new Zend_Mail_Transport_Smtp('smtp.server.com'$config);  
  11. $mail = new Zend_Mail();  
  12. $at = $mail->createAttachment(file_get_contents('path/logo.png'));  
  13. $at->filename = 'logo.png';                                    
  14. $mail->setBodyText('This is the text inside the mail send by Zend_Mail using SMTP transport.');  
  15. $mail->setFrom('you@mail.com''Your Name');  
  16. $mail->addTo('contact@mailservice.us''Your friend');  
  17. $mail->setSubject('Mail Subject');  
  18. $mail->send($transport);  

我不喜欢Zend framework的文档,因为你需要查看很多页面才能收集到上边的那些代码。你需要创建一个二级对象才能通过SMTP发送邮件。在他们的页面上我找不到提示“send”函数是否运行成功与否的信息。使用这个类并不难,对于新手来说,安装Zend framework是一件令其头疼的事。对于正在用Zend framework的人或者主机提供商已经提供类库,这个类是一个不错的解决方案。

Swift Mailer

复制内容到剪贴板折叠PHP 代码
  1. require_once 'Swift/lib/swift_required.php';  
  2.    
  3. $transport = Swift_SmtpTransport::newInstance('smtp.server.com', 25)  
  4.   ->setUsername('smtpUser')  
  5.   ->setPassword('smtpPassword');  
  6.    
  7. $mailer = Swift_Mailer::newInstance($transport);  
  8. $message = Swift_Message::newInstance('Wonderful Subject')  
  9.   ->setFrom(array('you@mail.com' => 'Your Name'))  
  10.   ->setTo(array('contact@mailservice.us' => 'Your friend'))  
  11.   ->setBody('This is the text of the mail send by Swift using SMTP transport.');  
  12. $attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');    
  13. $message->attach($attachment);  
  14. $numSent = $mailer->send($message);  
  15. printf("Sent %d messages\n"$numSent); 
这段代码看起来与Zend mail class有些相似,但是你要创建四个不同的对象:
  1. 一个SMTP协议的对象(Swift_SmtpTransport)
  2. 发送邮件信息的对象(Swift_Mailer)
  3. 所有邮件部分的信息对象(Swift_Message)
  4. 一个附件的对象(Swift_Attachment)
这个类的配置及安装都比 Zend Framework要容易得多。只需要引入一个文件就可以用这个类了。如果你喜欢面向对象的格式,那么这个类可能会适合你。

PHPMailer

复制内容到剪贴板折叠PHP 代码
  1. require_once 'PHPMailer5/class.phpmailer.php';  
  2.    
  3. $mail = new PHPMailer();  
  4. $mail->IsSMTP();   
  5. $mail->Host = 'smtp.server.com';  
  6. $mail->Port = 25;  
  7. $mail->SMTPAuth = true;  
  8. $mail->Username = 'smtpUser';  
  9. $mail->Password = 'smtpPassword';  
  10. $mail->SetFrom('you@mail.com''Your Name');  
  11. $mail->AddAddress('contact@mailservice.us''Your friend');  
  12. $mail->Subject = 'PHPMailer Message';  
  13. $mail->Body = 'This e-mail is sent through PHPMailer.';  
  14. $mail->AddAttachment('path/logo.png''logo.png');  
  15. if(!$mail->Send()) {  
  16.   echo 'Mailer error: '.$mail->ErrorInfo;  
  17. else {  
  18.   echo 'Message has been sent.';  
  19. }  
这段代码看起来与上面的另外两段代码有所不同,并且也不是真正的面向对象。本例中,你只需加载两个文件。我比较喜欢这个类,因为他的文档很完整。

你可以在这三个中选择一个适合你自己的。本文参考以下信息:


原文地址:http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/
/* */
暂无留言
留 言 板
您的鼎鼎大名:
您的金玉良言:
验证码: 验证码