使用phpmailer从PHP项目发送电子邮件?

时间:2011-02-01 22:12:15

标签: php phpmailer

我想用phpmailer发送电子邮件。

为了做到这一点,我搜索谷歌,我发现了打击链接:

http://phpmailer.worxware.com/

所以我从这里下载他们的库(通过他们网站的下载链接,php 4-5):

http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/

之后我参考这个例子:(使用smtp表示高级)

http://phpmailer.worxware.com/index.php?pg=exampleasmtp

<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

?>

但我找不到此行的class.phpmailer.php文件 - &gt; require_once( '../ class.phpmailer.php');来自下载文件。

我错过了什么吗?

你会帮我的吗?

最好的问候。

3 个答案:

答案 0 :(得分:0)

“../”表示“一个目录级别更高”。这意味着它应该在目录中查找文件class.phpmailer.php,该文件高于执行此脚本的目录。如果那不是你保存class.phpmailer.php相对于你的代码的地方,那么调整路径。

答案 1 :(得分:0)

试试这个。

                <?php
            include "/home/tnehme/public_html/smtpmail/classes/class.phpmailer.php"; // include the class name

            $mail = new PHPMailer(); // the true param means it will throw exceptions on errors, which we need to catch

            $mail->IsSMTP(); // telling the class to use SMTP

            try {
              $mail->Host       = "mail.yourdomain.com"; // SMTP server
              $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
              $mail->SMTPAuth   = true;                  // enable SMTP authentication
              $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
              $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
              $mail->Username   = "yourname@yourdomain"; // SMTP account username
              $mail->Password   = "yourpassword";        // SMTP account password
              $mail->AddReplyTo('name@yourdomain.com', 'First Last');
              $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
              $mail->SetFrom('name@yourdomain.com', 'First Last');
              $mail->AddReplyTo('name@yourdomain.com', 'First Last');
              $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
              $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
              $mail->MsgHTML(file_get_contents('contents.html'));
              $mail->AddAttachment('images/phpmailer.gif');      // attachment
              $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
              $mail->Send();
              echo "Message Sent OK\n";
            } catch (phpmailerException $e) {
              echo $e->errorMessage(); //Pretty error messages from PHPMailer
            } catch (Exception $e) {
              echo $e->getMessage(); //Boring error messages from anything else!
            }

            ?>

答案 2 :(得分:-1)

我使用PHPMailer和5.5.10并且它的工作非常好。

起床和跑步非常容易。将文件复制到目录(与PHP文件一起)

&#39;包括&#39;它在你的脚本中...并使用他们给你的示例代码作为你的电子邮件的基础。

要获取文件,请访问: https://github.com/PHPMailer/PHPMailer

祝你好运。

我也是关于SwiftMailer的好东西。