如何在没有作曲家的情况下使用PHPMailer?

Her*_*der 7 php github phpmailer

我想使用最新的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!";
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

我正在使用github上gmail示例,它也没有包含在zip下载中.但我可以在github中导航到它.在该示例文件中,它开始如下:

use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$mail = new PHPMailer;
Run Code Online (Sandbox Code Playgroud)

我看到autoload.phpzip下载中没有文件,经过谷歌搜索后我发现这意味着使用Composer.但必须有一些方法来简单地做一个包含并获取我需要的文件.

关于这个PHPMailer库和一般github的一些事情让我很困惑:

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

在此输入图像描述

  1. 为什么他们引用autoload.phpzip下载中不存在的内容?
  2. 显然,我不了解github的一些内容,但为什么不提供一个工作代码示例而不是引用下载中不存在的依赖项,迫使人们在别处找到它并希望他们能够弄清楚如何回来并插入它正确吗?
  3. 在这个名为" 使用PHP和Gmail发送电子邮件"的 YouTube视频中,他下载了我从同一个地方下载的相同的zip,但他的zip包含不同的文件,包括PHPMailerAutoload.php.为什么我得到的文件与他得到的完全不同?该视频于2017年3月4日发布 - 所以,不到1年前 - 从那时起它真的改变了吗?

总结:如何让PHPMailer在没有外部依赖和安装(如Composer)的情况下工作,而是require_once()用来获取我需要的东西?

Her*_*der 17

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

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!";
}
Run Code Online (Sandbox Code Playgroud)

  • 根据我所听到的内容以及使用的一点经验,注释掉$ mail-> isSMTP();行有时对于使事情正常进行是必要的。 (2认同)

SNS*_*que 5

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);
Run Code Online (Sandbox Code Playgroud)