【问题标题】:How to use PHPMailer without composer?如何在没有作曲家的情况下使用 PHPMailer?
【发布时间】:2018-06-16 03:39:20
【问题描述】:

我想将最新的 PHPMailer 库与 require_once() 一起使用,而不是使用 Composer。我想要一个简单的纯 xcopy 部署。

这是我正在尝试做的事情:

require_once("src/PHPMailer.php");
$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); 
$mail->AltBody = 'HTML messaging not supported';

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

我收到错误消息:Fatal error: Class PHPMailer not found in [....]\EmailTester.php on line 21

第 21 行是这样的:$mail = new PHPMailer;

这一行只是我的猜测:require_once("src/PHPMailer.php"); - 显然我需要包含一些文件,但我不知道是哪个。

我正在使用gmail example on github 工作,它也不包含在 zip 下载中。但我可以在 github 中导航到它。在该示例文件中,它的开头是这样的:

use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$mail = new PHPMailer;

我在 zip 下载中没有看到 autoload.php 文件,在谷歌搜索后我发现这意味着使用 Composer。但是必须有一些方法可以简单地进行包含并获取我需要的文件。

关于这个 PHPMailer 库和一般的 github 有几件事让我感到困惑:

  1. 当我从 GitHub 下载 PHP Mailer 时,为什么下载的 zip 文件中没有包含这么多列出的文件和文件夹?

  1. 为什么他们引用的autoload.php 在 zip 下载中不存在?
  2. 显然我对github的一些事情不太了解,但是为什么不提供一个工作代码示例而不是引用下载中不存在的依赖项,迫使人们在其他地方找到它并希望他们能弄清楚如何来返回并正确插入?
  3. 在这个标题为 Send Emails with PHP & Gmail 的 YouTube 视频中,他从同一个地方下载了我下载的同一个 zip,但他的 zip 包含不同的文件,包括 PHPMailerAutoload.php。为什么我得到的文件与他得到的完全不同?该视频发布于 2017 年 3 月 4 日——也就是说,不到一年前——从那时起它真的发生了很大变化吗?

总而言之:我怎样才能让 PHPMailer 在没有外部依赖项和安装(例如 Composer)的情况下工作,而是使用 require_once() 来获得我需要的东西?

【问题讨论】:

  • github.com/PHPMailer/PHPMailer 有关于如何在没有作曲家的情况下使用它的明确说明。 “或者,如果您不使用 composer,请将 PHPMailer 文件夹的内容复制到 PHP 配置中指定的 include_path 目录之一,并手动加载每个类文件:[...]”
  • 另外,在那个页面上也说,“如果你不会说 git 或者只是想要一个 tarball,请单击 GitHub 中项目页面右侧的 'zip' 按钮,但请注意文档和示例不包含在 tarball 中。”
  • CBroe 是完全正确的。您缺少的另一件事是命名空间支持 - 您仍然需要那些 use 语句,这就是导致找不到类错误的原因。
  • 专注于使用 composer 的一般原因是它是如此巨大的胜利。即使在您的简单示例中,它也已经比使用 composer 更复杂。许多人抱怨 tar 包包含太多用于生产的文件,因此使用 git config 选项排除了文档和示例。
  • 作曲家应该下地狱!

标签: php github phpmailer


【解决方案1】:

这是完整的工作示例(尽管您会看到一些必须定义和设置的变量):

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported';
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

【讨论】:

  • 从我所听到的和一点点使用经验来看,有时需要注释掉 $mail->isSMTP(); 行才能让事情正常工作。
  • 好评。我仔细按照 AWS 的说明进行操作,但脚本不起作用。评论这条线就可以了。谢谢。
  • 我使用相同的方式,但它会杀死我的页面 :( 我使用的是最新版本!可能是什么问题?
  • 我设法修复了它。 (其实PHPmailer没问题)问题是我的服务器端口没有打开。
【解决方案2】:

这对我有用,我也从这个地址下载了 phpmailer

https://sourceforge.net/projects/phpmailer/

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require '/source/PHPMailer2/src/Exception.php';
require '/source/PHPMailer2/src/PHPMailer.php';
require '/source/PHPMailer2/src/SMTP.php';

【讨论】:

    【解决方案3】:

    PHP Mailer 原始源码: PHP Mailer In gitHub

    // for use PHP Mailer without composer : 
    
    // ex. Create a folder in root/PHPMAILER
    // Put on this folder this 3 files find in "src" 
    // folder of the distribution : 
    // PHPMailer.php , SMTP.php , Exception.php
      
      // include PHP Mailer
      use PHPMailer\PHPMailer\PHPMailer;
      use PHPMailer\PHPMailer\Exception;
      include dirname(__DIR__) .'/PHPMAILER/PHPMailer.php';
      include dirname(__DIR__) .'/PHPMAILER/SMTP.php';
      include dirname(__DIR__) .'/PHPMAILER/Exception.php';
    
      // i made a function 
    
       /*
        *
        * Function send_mail_by_PHPMailer($to, $from, $subject, $message);
        * send a mail by PHPMailer method
        * @Param $to -> mail to send
        * @Param $from -> sender of mail
        * @Param $subject -> suject of mail
        * @Param $message -> html content with datas
        * @Return true if success / Json encoded error message if error 
        * !! need -> classes/Exception.php - classes/PHPMailer.php - classes/SMTP.php
        *
        */
        function send_mail_by_PHPMailer($to, $from, $subject, $message){
    
              // SEND MAIL by PHP MAILER
              $mail = new PHPMailer();
              $mail->CharSet = 'UTF-8';
              $mail->isSMTP(); // Use SMTP protocol
              $mail->Host = 'your_host.com'; // Specify  SMTP server
              $mail->SMTPAuth = true; // Auth. SMTP
              $mail->Username = 'my_mail@your_host.com'; // Mail who send by PHPMailer
              $mail->Password = 'your_passord_of_your_box'; // your pass mail box
              $mail->SMTPSecure = 'ssl'; // Accept SSL
              $mail->Port = 465; // port of your out server
              $mail->setFrom($from); // Mail to send at
              $mail->addAddress($to); // Add sender
              $mail->addReplyTo($from); // Adress to reply
              $mail->isHTML(true); // use HTML message
              $mail->Subject = $subject;
              $mail->Body = $message;
    
              // SEND
              if( !$mail->send() ){
    
                  // render error if it is
                  $tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
                  echo json_encode($tab);
                  exit;
              }
              else{
                  // return true if message is send
                  return true;
              }
    
        }
        /*
        *
        * END send_mail_by_PHPMailer($to, $from, $subject, $message)
        * send a mail by PHPMailer method
        *
        */
      
        // use function :
        send_mail_by_PHPMailer($to, $from, $subject, $message);
    

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2018-10-21
      • 2018-10-01
      • 1970-01-01
      • 2019-07-09
      • 2018-01-15
      相关资源
      最近更新 更多