9

我正在尝试发送普通/HTML 多部分电子邮件,我目前正在使用 PHP 的 mail() 函数。很多人都推荐了 PHPMailer,所以我想我会试一试。

然而,就像现在一切似乎一样,它看起来非常复杂。我下载了它,它谈到了安装它和配置 MySQL 连接和 SMTP 连接!?我想做的就是使用一个不错的类,它会为我构建 MIME 电子邮件并发送它们!我了解 SMTP 的可能性,但这一切似乎都如此复杂!

是否有某种方法可以简单地使用它,例如,包含一个 php 文件(无需服务器安装或重新编译 PHP!),然后只使用该类来构建和发送电子邮件?

如果有人能简单地解释一下,我将不胜感激!我确信这是可能的,我不敢相信在我搜索了几个小时之后,网上没有关于它的真正好的、简单的文章。当我知道没有必要时,一切都太复杂了!

4

3 回答 3

8

漂亮的方式(来自此链接),首先扩展 PHPMailer 并为您的站点设置默认值:

require("class.phpmailer.php");

class my_phpmailer extends phpmailer {
    // Set default variables for all new objects
    var $From     = "from@example.com";
    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
    }
}

然后在需要的地方包含上面的脚本(在这个例子中它被命名)并在你的站点的某个地方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("josh@example.com", "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";
于 2009-08-23T16:15:41.037 回答
4

我对 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('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();

它可能会满足您的所有需求(附件、HTML、SMTP 配置……)。默认情况下它使用sendmail, 就像mail()函数一样,所以如果你不需要它,你不必配置任何像 SMTP 的东西。

它也有很好的文档,所以你不会很难找到例子。

于 2009-08-23T16:08:52.050 回答
0

改用SwiftMailer

于 2009-08-23T15:57:18.000 回答