Error Using PHPMailer After New Installation [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I just downbloaded the PHPMailer zipped file, saved it to my host server and installed / extracted the PHPMailer files. When I added a require statement to use the PHPMailer.php file, I got the following error message:
Parse error: syntax error, unexpected '[' in D:\InetPub\vhosts\ ******.com\ *********.com\language\PHPMailer\src\PHPMailer.php on line 288
Doesn't this mean that there is an error in the PHPMailer.php file that was downloaded? If yes, is there a corrected file that I can use in its place? Here's the php code that I am trying to run:
<?php
ini_set('include_path','D:\inetpub\vhosts\******.com\*********.com\included_files;D:\inetpub\vhosts\******.com\*********.com\referenced_files;D:\inetpub\vhosts\******.com\*********.com\language;D:\inetpub\vhosts\******.com\*********.com\language\PHPMailer;D:\inetpub\vhosts\******.com\*********.com\language\PHPMailer\language;D:\inetpub\vhosts\******.com\*********.com\language\PHPMailer\src');
include("incPHP1.inc");
require 'Exception.php';
require 'PHPMailer.php';
require 'SMTP.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 = $mailHost; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $mailUser; // SMTP username
$mail->Password = $mailPassword; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('*******#rlchealth.com', 'Mailer');
$mail->addAddress('****#rlchealth.net', 'Joe User'); // Add a recipient
$mail->addAddress('*********#rlchealth.com'); // Name is optional
$mail->addReplyTo('********#rlchealth.com', 'Information');
$mail->addCC('************#*****.com');
$mail->addBCC('************r*****l.com');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = '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 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

This issue is of your PHP version i guess u are using php 5.3 and phpMiller is for 5.6.
Please change ur php version and try again.

Related

how to send email using localhost xampp in windows10 64 bit [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
How to configure XAMPP to send mail from localhost?
(11 answers)
Closed 3 years ago.
I am trying to send email using xampp.but the mail function is only for windows 32.So how can i fix this problem? Is there have any existing method for send mail from localhost/xampp?
I configure php.ini and sendmail.ini but it doesn't works on windows 64.my error log show that connection time out.
If you can use PHPMailer, this is a simple example:
<?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';
// 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 = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // 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 = 'Here is the subject';
$mail->Body = '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 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Source

PHPMailer Error: Could Not Instantiate Mail Function Is not working [duplicate]

This question already has answers here:
Phpmailer error "Could not instantiate mail function"
(18 answers)
Closed 3 years ago.
I have an issue in sending mail from PHPMailer. When I try to send a message, I get an error message which is
Message could not be sent. Mailer Error:Could not instantiate mail function.
My SMTP is correct, I can't find the problem. Can anyone help me find the problem? Thanks
<?php
require "autoload.php";
if(isset($_POST["submit"])){
$mail = new PHPMailer(true);
$sender = "demo#gmail.com";
//SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = demo#gmail.com; // SMTP username
$mail->Password = '1234567'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS
$mail->Port = 587; // TCP port to connect
$mail->From = "demo#gmail.com";
$mail->FromName = 'Mailer';
$mail->setFrom(demo#gmail.com, 'Mailer');
$mail->addAddress($_POST["receiver"]); // Name is optional
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
You have set various SMTP-related properties, but you have not told PHPMailer to use SMTP!
Add this line:
$mail->isSMTP();
You're also enabling exceptions (passing true to the constructor), but have not wrapped your code in a try/catch block. See the examples provided with PHPMailer for how to deal with that.
please recheck your configuration like , user/password, PORT number
Connect to smtp.gmail.com on port 465, if you're using SSL. Else use 587.
Also make sure less secure app is turn on
https://support.google.com/accounts/answer/6010255?hl=en

How to include PHPmailer in functions.php properly (Wordpress)

I'm trying to include PHPmailer in functions.php
my code:
add_action('wp_ajax_nopriv_test_mailer', 'test_mailer');
function test_mailer () {
try {
require_once(get_template_directory('/includes/mail/PHPMailer.php'));
require_once(get_template_directory('/includes/mail/Exception.php'));
$mail = new PHPMailer(true); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test#gmail.com'; // SMTP username
$mail->Password = 'dummypassword!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('test#gmail.com', 'Mailer Test');
$mail->addAddress('john.doe#gmail.com', 'John User'); // Add a recipient
$mail->addReplyTo('test#gmail.com');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject testing';
$mail->Body = '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 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
wp_die();
}
I also tried to put require_once out of the try catch still the same error here is the snippet about the error
"PHP Fatal error: Uncaught Error: Class 'PHPMailer' not found"
I use betheme template and I stored the files PHPmailer in betheme/includes/mail.
As BA_Webimax pointed out, you should be using Wordpress' built-in email functions, though due to WP's reliance on outdated PHP versions, you will end up using a very old version of PHPMailer with it.
Back to your current problem: It's not your require_once statements that are failing, it's that you have not imported the namespaced PHPMailer classes into your namespace. Add these at the top of your script:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
Alternatively, use the FQCN when creating your instance:
$mail = new PHPMailer\PHPMailer\PHPMailer;
Note that this applies to the Exception class too, so you'd need to say:
catch (PHPMailer\PHPMailer\Exception $e) {
get_template_directory() returns the absolute path of the theme and doesn't take any params.
Try this for your includes:
require_once(get_template_directory().'/includes/mail/PHPMailer.php');
require_once(get_template_directory().'/includes/mail/Exception.php');

How to configure and run PHPmailer in PHP

this query may sounds silly but it has become a headache for me. i am using ampps... I need to implement mail notification in my project. i have downloaded the PHPmailer rar file and extracted to my project folder.
It contains,
*get_oauth_token.php
*src-Exception.php-OAuth.php-PHPMailer.php-POP3.php-SMTP.php
I got only this files in that folder.
<?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 = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // 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 = 'Here is the subject';
$mail->Body = '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 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Here i am getting an error message saying,
Warning: require(vendor/autoload.php): failed to open stream: No such
file or directory in C:\Program Files
(x86)\Ampps\www\emailpro\email.php on line 8
By googling I got to know that by composer we can download classes. I tried that also. >composer require phpmailer/phpmailer – ERROR- 'composer' is not recognized as an internal or external command, operable program or batch file.
I am totally confused pls anyone tell me how to implement this PHPmailer to the project.
Please help how to configure from scratch i have googled lot and i didnt got the required answer.
If you don't want to install composer then use this code for php mailer to work. It is working in my projects. You can also remove the part of code which you don't need like attachments. One more thing if you want to hide the debugging code errors and notifications remove or comment this line $mail->SMTPDebug = 2;
For more help visit this link https://github.com/PHPMailer/PHPMailer
<?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;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.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 = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
//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 = 'Here is the subject';
$mail->Body = '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 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}`enter code here`

PHPmailer not working recipient

I have a problem, with my code beneath I get this error message:
Message could not be sent.Mailer Error: SMTP Error: The following recipients failed: !Censored!
I have looked into the Host, Port, Username, Password, Recipient and all is correct, what is the problem? Thank you!
Could you please explain too cuz Im new to PHP-coding
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['amne'];
$message = $_POST['message'];
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '!Censored!'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '!Censored!'; // SMTP username
$mail->Password = '!Censored!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('!Censored!'); // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
EDIT: I found the problem, the problem was not in the php code itself. It was in the contact form. The problem started when I put the variables as names and emails. If the email is not verified, it does not work.
This error can be caused by a few different things. You can get a better idea of the reason by adding the following line to your PHPMailer script:
$mail->SMTPDebug = 2; //<-- For debugging
Once you do that, you can check the following things that may be causing the error:
1.) A corrupt class.phpmailer.php file.
2.) The error may be caused by protection put in place by your ISP. Check with them.
3.) It could be a problem with the recipient's/sender's email addresses.
4.) Set SMTPAuth to true for PHPMailer class.
5.) Comment out the following line in your PHPMailer script: $mail->isSMTP();
Mostly, There is possibility that Your phpmailer class file is corrupted.
Download the latest version: https://github.com/PHPMailer/PHPMailer

Categories