I'd like to use the latest PHPMailer library with require_once()
instead of messing around with Composer. I'd like a pure xcopy deployment with minimal fuss.
Here's what I'm attempting to do:
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!";
}
I get the error message: Fatal error: Class PHPMailer not found in [....]\EmailTester.php on line 21
Line 21 is this: $mail = new PHPMailer;
This line is just a guess on my part: require_once("src/PHPMailer.php");
- clearly I need to include some file or files, but I can't tell which.
I'm working from the gmail example on github which is also not included in the zip download. But I can navigate to it in github. In that example file it begins like this:
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$mail = new PHPMailer;
I see no autoload.php
file in the zip download, and after googling all over I see this implies using Composer. But there must be some way to simply do an include and get the files I need.
A few things puzzle me about this PHPMailer library and perhaps github in general:
autoload.php
which doesn't exist in the zip download?PHPMailerAutoload.php
. Why am I getting completely different files than he gets? That video was published March 4, 2017 -- so, less than 1 year ago -- has it really changed so much since then?In summary: How can I get PHPMailer working without external dependencies and installations such as Composer, and instead use require_once()
to get what I need?
The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.
PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.
Here's the full working example (though you see a few variables that must be defined and set):
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!";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With