php 我如何使用 PHPMailer?我在网上找不到简单像样的教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1318861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use PHPMailer? I can't find a simple decent tutorial online
提问by Michael Waterfall
I'm trying to send out a Plain/HTML multipart email out and I'm currently using PHP's mail() function. Many people have recommended PHPMailer so I thought I'd give it a go.
我正在尝试发送普通/HTML 多部分电子邮件,并且我目前正在使用 PHP 的 mail() 函数。很多人都推荐了 PHPMailer,所以我想我会试一试。
However, as everything seems to be nowadays, it appears very complicated. I downloaded it and it talks about installing it and configuring MySQL connections and SMTP connections!? All I want to do is use a nice class that will build the MIME emails for me and send them! I understand the SMTP possibilities but it all seems so complex!
然而,正如现在一切似乎一样,它看起来非常复杂。我下载了它,它谈到了安装和配置 MySQL 连接和 SMTP 连接!?我想要做的就是使用一个很好的类,它将为我构建 MIME 电子邮件并发送它们!我理解 SMTP 的可能性,但这一切似乎都很复杂!
Is there some way of simply just using it, for example, include a php file (no server installation or re-compiling PHP!) and then just using the class to build and send the email?
是否有某种方法可以简单地使用它,例如,包含一个 php 文件(无需安装服务器或重新编译 PHP!),然后仅使用该类来构建和发送电子邮件?
I'd be very grateful if someone could explain things simply! I'm sure it's possible and I can't believe after my hours of searching there's no really good, simple article about it online. Everything's TOO complicated when I know it doesn't need to be!
如果有人能简单地解释一下,我将不胜感激!我确信这是可能的,而且我无法相信经过数小时的搜索,网上没有关于它的真正好的、简单的文章。当我知道它不需要时,一切都太复杂了!
采纳答案by Zed
Try SwiftMailerinstead.
试试SwiftMailer。
回答by karim79
Pretty way (from this link), first extend PHPMailer and set the defaults for your site :
漂亮的方式(来自此链接),首先扩展 PHPMailer 并为您的站点设置默认值:
require("class.phpmailer.php");
class my_phpmailer extends phpmailer {
// Set default variables for all new objects
var $From = "[email protected]";
var $FromName = "Mailer";
var $Host = "smtp1.example.com;smtp2.example.com";
var $Mailer = "smtp"; // Alternative to IsSMTP()
var $WordWrap = 75;
// Replace the default error_handler
function error_handler($msg) {
print("My Site Error");
print("Description:");
printf("%s", $msg);
exit;
}
// Create an additional function
function do_something($something) {
// Place your new code here
}
}
Then include the above script where needed (in this example it is named mail.inc.php) and use your newly created my_phpmailerclass somewhere on your site:
然后在需要的地方包含上面的脚本(在这个例子中它被命名为mail.inc.php)并my_phpmailer在你的站点上的某个地方使用你新创建的类:
require("mail.inc.php");//or the name of the first script
// Instantiate your new class
$mail = new my_phpmailer;
// Now you only need to add the necessary stuff
$mail->AddAddress("[email protected]", "Josh Adams");
$mail->Subject = "Here is the subject";
$mail->Body = "This is the message body";
$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip"); // optional name
if(!$mail->Send())
{
echo "There was an error sending the message";
exit;
}
echo "Message was sent successfully";
回答by mooware
I don't know anything about PHPMailer, but I recommend using Zend_Mail. Here's a simple example with an attachment:
我对 PHPMailer 一无所知,但我建议使用Zend_Mail。这是一个带附件的简单示例:
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->createAttachment($myImage,
'image/gif',
Zend_Mime::DISPOSITION_INLINE,
Zend_Mime::ENCODING_8BIT);
$mail->setFrom('[email protected]', 'Some Sender');
$mail->addTo('[email protected]', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
It probably does everything you want (Attachments, HTML, SMTP-Configuration, ...). By default it uses sendmail, like the mail()function, so you don't have to configure anything like SMTP if you don't need it.
它可能会做您想要的一切(附件、HTML、SMTP 配置,...)。默认情况下,它使用sendmail, 就像mail()函数一样,所以如果你不需要它,你不必配置像 SMTP 之类的东西。
It also has very good documentation, so you won't have trouble finding examples.
它也有非常好的文档,因此您可以轻松找到示例。

