给新手的指导:用SMTP发送邮件
PHP中有一个非常简单的发送文本邮件的函数,假设你要发送附件或者通过SMTP发送邮件会是怎样一种情况呢?这时候就要用到一个更高级的脚本。这是因为 标准的发送邮件函数只有有限的几个标准功能。有许多理由让你在web应用中利用SMTP协议发送邮件,例如:
实例:
在本例中,我们用SMTP发送一个带有图片附件的纯文本邮件信息。有一个好消息:作为一个有多年php开发经验的人,我会利用已经提供的例子,在很短的时间内做讲解。
我不喜欢Zend framework的文档,因为你需要查看很多页面才能收集到上边的那些代码。你需要创建一个二级对象才能通过SMTP发送邮件。在他们的页面上我找不到提示“send”函数是否运行成功与否的信息。使用这个类并不难,对于新手来说,安装Zend framework是一件令其头疼的事。对于正在用Zend framework的人或者主机提供商已经提供类库,这个类是一个不错的解决方案。
这段代码看起来与Zend mail class有些相似,但是你要创建四个不同的对象:
这段代码看起来与上面的另外两段代码有所不同,并且也不是真正的面向对象。本例中,你只需加载两个文件。我比较喜欢这个类,因为他的文档很完整。
你可以在这三个中选择一个适合你自己的。本文参考以下信息:
原文地址:http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/
/*
*/
- 许多共享主机提供商为了安全 的原因禁止使用PHP的mail()函数。
- 如果你用了简单信息传输协议(SMTP),你的应用程序会更加灵活。这种办法再也不会让你的 发送邮件的函数被服务器端口或者邮件配置文件所限制。
- SMTP功能强大并且安全(用了SSL)
- Zend Framework 框架中 包含的邮件类。(http://framework.zend.com/)
- Swift Mailer (http://swiftmailer.org/)
- PHPMailer (http://phpmailer.worxware.com)
实例:
在本例中,我们用SMTP发送一个带有图片附件的纯文本邮件信息。有一个好消息:作为一个有多年php开发经验的人,我会利用已经提供的例子,在很短的时间内做讲解。
Zend Mail Class
复制内容到剪贴板折叠PHP 代码
- ini_set('include_path', '.:/path2directory/ZendFramework/library/');
- require_once 'Zend/Loader.php';
- Zend_Loader::loadClass('Zend_Mail');
- Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');
- $config = array('auth' => 'login',
- 'username' => 'smtpUser',
- 'password' => 'smtpPassword');
- $transport = new Zend_Mail_Transport_Smtp('smtp.server.com', $config);
- $mail = new Zend_Mail();
- $at = $mail->createAttachment(file_get_contents('path/logo.png'));
- $at->filename = 'logo.png';
- $mail->setBodyText('This is the text inside the mail send by Zend_Mail using SMTP transport.');
- $mail->setFrom('you@mail.com', 'Your Name');
- $mail->addTo('contact@mailservice.us', 'Your friend');
- $mail->setSubject('Mail Subject');
- $mail->send($transport);
我不喜欢Zend framework的文档,因为你需要查看很多页面才能收集到上边的那些代码。你需要创建一个二级对象才能通过SMTP发送邮件。在他们的页面上我找不到提示“send”函数是否运行成功与否的信息。使用这个类并不难,对于新手来说,安装Zend framework是一件令其头疼的事。对于正在用Zend framework的人或者主机提供商已经提供类库,这个类是一个不错的解决方案。
Swift Mailer
复制内容到剪贴板折叠PHP 代码
- require_once 'Swift/lib/swift_required.php';
- $transport = Swift_SmtpTransport::newInstance('smtp.server.com', 25)
- ->setUsername('smtpUser')
- ->setPassword('smtpPassword');
- $mailer = Swift_Mailer::newInstance($transport);
- $message = Swift_Message::newInstance('Wonderful Subject')
- ->setFrom(array('you@mail.com' => 'Your Name'))
- ->setTo(array('contact@mailservice.us' => 'Your friend'))
- ->setBody('This is the text of the mail send by Swift using SMTP transport.');
- $attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');
- $message->attach($attachment);
- $numSent = $mailer->send($message);
- printf("Sent %d messages\n", $numSent);
- 一个SMTP协议的对象(Swift_SmtpTransport)
- 发送邮件信息的对象(Swift_Mailer)
- 所有邮件部分的信息对象(Swift_Message)
- 一个附件的对象(Swift_Attachment)
PHPMailer
复制内容到剪贴板折叠PHP 代码
- require_once 'PHPMailer5/class.phpmailer.php';
- $mail = new PHPMailer();
- $mail->IsSMTP();
- $mail->Host = 'smtp.server.com';
- $mail->Port = 25;
- $mail->SMTPAuth = true;
- $mail->Username = 'smtpUser';
- $mail->Password = 'smtpPassword';
- $mail->SetFrom('you@mail.com', 'Your Name');
- $mail->AddAddress('contact@mailservice.us', 'Your friend');
- $mail->Subject = 'PHPMailer Message';
- $mail->Body = 'This e-mail is sent through PHPMailer.';
- $mail->AddAttachment('path/logo.png', 'logo.png');
- if(!$mail->Send()) {
- echo 'Mailer error: '.$mail->ErrorInfo;
- } else {
- echo 'Message has been sent.';
- }
你可以在这三个中选择一个适合你自己的。本文参考以下信息:
原文地址:http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/
