PHPMailer seems to not install correctly: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer - phpmailer

I am getting the error above when I run my code. Following the instructions on the PHPMAiler Github page, I added "phpmailer/phpmailer": "^6.0" to my composer.json but PHPMailer seems not to load.
I tried using $mail = new PHPMailer\PHPMailer\PHPMailer(true) instead of $Mail = new PHPMailer(true). I went to the Composer IRC channel to see if my problem was with Composer. I have also tried using the full path in the require 'vendor/autoload.php'; statement.
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//other code...
//Create a new PHPMailer instance
$mail = new PHPMailer(true);
I expect PHPMailer to run. Where did I go wrong?

When I had difficulty running PHPMailer on my local XAMPP server and the composer, I ended up installing the file manually by loading it into my directory
htdocs
-index.php
-Other_Program_to_run_with_PHPMailer.php
-folder(PHPMailer)
<?php
$messege=""
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'insert Username'; // SMTP username
$mail->Password = 'insert Password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('insert username', 'Information');
$mail->addAddress('who it is being sent to', 'PHP Management'); // Add a recipient
$mail->addReplyTo('who they can reply to', 'PHP Management');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = ;
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'The following Message has been sent';
echo $message;
}
?>
From there I have been copy and pasting onto my server and it works out pretty good.

Scott
Your problem may be solved if you would have read the documentations. Use the below code at first.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

just download the phpMailer Class from that GIT repo link
https://github.com/PHPMailer/PHPMailer
and extract/copy three files PHPMailer.php, Extension.php and SMTP.php files put them in a folder name : (PHPMailer) like in the image bellow :-
https://i.stack.imgur.com/T6DXW.png
and just add these lines of code at the top of your file
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
// require 'vendor/autoload.php';
require('PHPMailer/Exception.php');
require('PHPMailer/SMTP.php');
require('PHPMailer/PHPMailer.php');

Related

PHPMailer not working when function is within and included file

Something is mighty odd. I created a basic test file with the code from PHPMailer.
# Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'includes/PHPMailer/PHPMailer.php';
require 'includes/PHPMailer/SMTP.php';
require 'includes/PHPMailer/Exception.php';
function sendCampaignEmail ($email, $firstname, $lastname)
{
$mail = new PHPMailer(true); // Create a new PHPMailer instance. Passing `true` enables exceptions.
try {
//Server settings
# $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output. SMTP::DEBUG_SERVER = client and server messages
# $mail->SMTPDebug = SMTP::DEBUG_CLIENT; // SMTP::DEBUG_CLIENT = client messages
$mail->SMTPDebug = SMTP::DEBUG_OFF; // SMTP::DEBUG_OFF = off (for production use)
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'myhostxxxx.co.uk'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'oobaclub#xxxxxxxxx.co.uk'; // SMTP username
$mail->Password = 'xxxxxmyEmailPasswordxxxxx'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('myfromemail.co.uk', 'My Name');
$mail->addAddress($email, $firstname . ' ' . $lastname); // Add a recipient
$mail->addReplyTo('myfromemail.co.uk', 'My Name');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'Dear ' . $firstname . ' ' . $lastname . '<br/><br/>This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo '<br/>Message has been sent to ' . $email . ' (' . $firstname . ' ' . $lastname . ')';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
$names= array();
$id=0;
$names[$id]['email'] = "atestemail#gmail.com";
$names[$id]['first_name'] = "Bob";
$names[$id]['last_name'] = "gMail"; $id++;
$names[$id]['email'] = "anothertest#testingmyemail.co.uk";
$names[$id]['first_name'] = "Sid";
$names[$id]['last_name'] = "Smith"; $id++;
$count=0;
while ($count < count($names))
{
sendCampaignEmail ($names[$count]['email'], $names[$count]['first_name'], $names[$count]['last_name']);
$count++;
}
THIS CODE ABOVE WORKS FINE.
So... Then, I took the function and put it into an Included file (where all my functions are: "globalfunctions.php").... And now it says "Fatal error: Uncaught Error: Class 'PHPMailer' not found"
So, now, at the top of my index.php, I have:
## PHP MAILER - # Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'includes/PHPMailer/PHPMailer.php';
require_once 'includes/PHPMailer/SMTP.php';
require_once 'includes/PHPMailer/Exception.php';
require_once 'includes/connect.php';
require_once 'includes/globalfunctions.php';
I am confused as everything else works. All my other functions work. I thought namespaces were global... But, I tried adding the "use" code into the function... but, as expected, that didn't work either....
I am stumped.
PHP use declarations are local aliases that apply only to the file they appear in. You have to add a use statement in every file that uses that class; you can't put all your declares in one file and then include it from somewhere else.
Now might be a good time to learn how to use composer as it takes care of a lot of this.

problems with server SMTP PHPMailer

I have a problem with phpmailer, I have the correct SMPT server data and also the username and password, but it still marks me an error in the credentials, this is my code
<?php
include ("class.phpmailer.php");
include ("../vendor/phpmailer/phpmailer/PHPMailer.php");
include ("../vendor/phpmailer/phpmailer/src/SMTP");
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/phpmailer/phpmailer/src/Exception.php';
require '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '../vendor/phpmailer/phpmailer/src/SMTP.php';
require '../vendor/autoload.php';
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Load Composer's autoloader
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'admin#ucarolina.mx'; // SMTP username
$mail->Password = '******'; // SMTP password
$mail->SMTPSecure = 'TSL'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('luribe#qvoz.com');
$mail->addAddress($email, $name); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $contact_message;
$mail->AltBody = $contact_message;
$mail->send();
echo "Mensaje enviado";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
and this is the error that marks me
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
There is a line where SMTPSecure has to be specified. You have that line, but tls has been mistyped as TSL. Let's change that:
Change
$mail->SMTPSecure = 'TSL';
to
$mail->SMTPSecure = 'tls';
Additionally, you should add an if/then surrounding $mail->send(); like so:
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
If the mail is not sent, your if statement will catch that and report the error. See an example of such error handling in PHPMailer example: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

EOF caught while checking if connected

I would like to send an email from localhost by using PHP Mailer.
i have already look for others question and that solve some of the error that i got.
Except for this last error.i did not find the answer that fit to solve the error. I am really hope for help to solve this error.
This is what being diplayed after try to run the code.
2018-08-03 04:57:44 SERVER -> CLIENT:
2018-08-03 04:57:44 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
Email could not be sent. Mailer Error: SMTP Error: Could not authenticate.
This is my code
<html>
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('test#gmail.com', 'Mailer');
$mail->addAddress('test#gmail.com', 'Naura'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email testing';
$mail->Body = 'Hi. This is a test email.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Email has been sent';
} catch (Exception $e) {
echo 'Email could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
</html>

PHP mailer throws error in php 7 when tried send mail

I'm trying to send mail using PHP Mailer but I'm getting an error.
Kindly check my code let me know the fix.
Here is my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer;
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxx ';
$mail->Password = 'xxxxxxxxx';
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('xxxxxxx', 'shiva');
$mail->addAddress('siva.sing.sivan#gmail.com', 'SP'); // Add a recipient
$mail->addAddress('senthil.mca2008#gmail.com', 'SK'); // Add a recipient
//\\$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('opensourcesivaprakash#gmail.com', 'Information');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test mail form SP';
$mail->Body = 'This is the HTML message <b>From SP!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}else{
echo 'Message has been sent';
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
My Error:
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Please correct my code to fix the issue.
Hey #shiva use this $mail->SMTPSecure = 'tls' and use port as $mail->Port = 587 that's it!
Tell me it worked for you, if not then make sure to enable "Less Secure Apps" in your account settings, Thanks!
Let me know it worked for you or not?
upgrade the php version to 7.2.x and try it has inbuilt openssl 1.1.1

Signing PHP emails on shared hosting

I am on a shared hosting plan, and when I send emails with PHP, email clients (like Gmail) will add a little via bit to my from field, with my host's domain in there.
So instead of my emails being just from my domain:
From: me#mydomain.com
It's from two domains:
From: me#mydomain.com via host13.myhost.com
Clearly, this is confusing to people receiving email and is poor branding. Since I'm on a shared hosting plan, I don't think I'm likely to have access to the configurations settings of PHP or whatever it uses to mail. Is it possible for me to digitally sign my PHP emails, or is this not possible on shared hosting?
Here is what I'm doing now:
$header = "From: me#mydomain.com";
mail("you#yourdomain.com", "subject", "body", $header);
You can try this, you need to download the PHP Mailer class from Here and your code will be like this:
<?php
include "PHP MAILER CLASS";
$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->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "example#gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->AddAddress("Reciever Email", "Reciever Name");
$mail->SetFrom('Sender Email', 'Sender Name');
$mail->Subject = "Subject";
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML("Message Body");
$mail->Send();
} catch (phpmailerException $e) {
$e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
$e->getMessage(); //Boring error messages from anything else!
}
?>
The default mail function is at the mercy of your server setup and rarely looks like regular mail to the reciever. You should use a library either SwitfMailer or pear MAIL that can send the mail through your own mail server via SMTP. You can use your normal email account or setup a new one for your web service.

Resources