New PHPMailer required PHPMailerAutoload.php? - php

I'm working on a project, And i need send email after enter email and password by user. They can activate them account click that send email. But i got error when i using PHPMailer. Error was that like
Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer' not found in
C:\xampp\htdocs\E-Commerce-New-2\registration.php:20 Stack trace: #0
{main} thrown in C:\xampp\htdocs\E-Commerce-New-2\registration.php on
line 20
In new PHPMailer version use PHPMailerAutoload.php file? I think answer is no.
I tried use that code $mail = new PHPMailer\PHPMailer(); and also this code $mail = new PHPMailer(); No one is worked yet. I put my file structure down below
And here i'm past my codes lines.
<?php
session_start();
include("includes/db.php");
include("functions/functions.php");
include('PHPMailer/PHPMailer.php');
?>
<?php
// If the values are posted, insert them into the database.
if (isset($_POST['email']) && isset($_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
$verification_key = md5($email);
$query = "INSERT INTO `customers` (customer_pass,customer_email,verification_key) VALUES ('$password', '$email','$verification_key')";
$result = mysqli_query($con, $query);
if($result){
$mail = new PHPMailer\PHPMailer();
$mail->setFrom('noreplay#clickshop.com');
$mail->addAddress($email,'test');
$mail->Subject = "Verify Your Account";
$mail->isHTML(true);
$mail->Body = ' Please verify your email address to activate your account by clicking on this link <br>
<a href="http://localhost/E-Commerce-New-2/verify.php?key="'. $verification_key.'>Click Here</a>';
if(!$mail->send()) {
$fmsg= 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} else {
$smsg = 'User Registration successfull. Please Check your email to Activate Account!';
}
}else{
$fmsg = "User registration failed";
}
}
?>
Can anybody give me a suggestion?

In the new PHPMailer v6.0+, you will now need to import the PHPMailer classes into the global namespace at the very top of your PHP script.
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();
$mail->IsSMTP();
$mail->Host = "mail.example.com";
$mail->SetFrom("$from", "$from");
$mail->AddAddress("$to");
$mail->Subject = "$subject";
$mail->Body = "$message";
$mail->Send();

Related

After sending email to reset password using PHP many info lines appear to user [duplicate]

This question already has answers here:
how to disable phpmailer showing configuration details on browser
(2 answers)
Closed 4 months ago.
I am using the code below to send a link to the user email to reset the password, the code works fine and sends the link. The problem is after sending the link the page will show many lines of information and a success message at the end.
What is wrong with the code below to avoid these lines? Here is the code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'database.php';
if(isset($_POST["email"])){
$emailTo = $_POST["email"];
$code = uniqid(true);
$sql = "INSERT INTO reset(code, email) VALUES('$code', '$emailTo');";
$run = mysqli_query($conn, $sql);
if(!$run){
exit("Error");
}
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myEmail.com';
$mail->Password = 'xxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('myqmial.com', 'Hello');
$mail->addAddress($emailTo);
$mail->addReplyTo('no-reply#gmail.com', 'No reply');
// Content
$url = "http://" .$_SERVER["HTTP_HOST"] . dirname($_SERVER["PHP_SELF"]) . "/resetPassword.php?code=$code";
$mail->isHTML(true);
$mail->Subject = 'Reset Password';
$mail->Body = "<h1>You asked to reset your passwordŘŚ </h1>
Click <a href='$url'>this link</a> Please click on the link";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'The link has been successfully sent to your email';
}
catch (Exception $e) {
echo "Message could not be sent. Mail Error: {$mail->ErrorInfo}";
}
exit();
}
?>
<form method='POST'>
<input type='text' name='email' placeholder='Email'>
<input type='submit' name='submit' value='Reset password'>
</form>
I am attaching an image with the results:
I do not know what mistake I am doing, so how can I just show the message (The link has been successfully sent to your email) without all these lines?
Because, you have opened debug mode of PHPMailer. You need to disable debug mode like this:
$mail->SMTPDebug=0;
For additionally, you may visit https://mailtrap.io/blog/phpmailer/

I want to send a qrCode to an email using PHPMailer

This is my qr_code.php file:
require_once 'phpqrcode/qrlib.php';
function create_QrCode($name){
$path ='images/';
$file=$path.uniqid().".png";
$text=$name;
QRcode::png($text, $file);
}
?>
This is my mail.php file:
require('qr_code.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\htdocs\contact_form\vendor\autoload.php';
#Initialize PHPMailer
function Send_email($mailFrom,$User_name,$phone,$city,$facebook,$qrCode){
$mail= new PHPMailer();
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail ->isSMTP();
$mail ->SMTPAuth =true;
$mail ->SMTPDebug =3;
$mail ->SMTPSecure="tls";
$mail ->Port=587;
#Connection settings
$mail ->Host= "smtp.gmail.com";
$mail ->Username="email#gmail.com";
$mail ->Password="password";
$mailTo ='email#gmail.com';
$mail-> IsHTML(true);
$mail ->SetFrom($mailFrom,$User_name);
$mail ->addAddress($mailTo);
$mail->AddReplyTo($mailFrom, $User_name);
#Create email
$mail->Subject ='I would like to come at the party ! ';
$mail->Body ="Name: $User_name<br>
Phone No: $phone<br>
City: $city<br>
Facebook: $facebook<br>
Email id: $mailFrom<br>
QrCode: $qrCode";
return $mail->Send();
}
This is my functionalities.php file (where I want to send the email):
$qrCode=create_QrCode($name);
if(!Send_email($email,$name,$phone,$city,$facebook,$qrCode))
{
echo "Mail NOT SENT";
}
else
{
echo"Mail Sent !";
}
header('Location:redirecting_page.html');
}
I have tried different modes, but I don't know how to figure it out. I know in the QRCode function, I am generating it to a file, but this is what I succesfully created until now. I know the QRCode function is the problem, but I don't know how to fix it. Can somebody please help me?

send form details to a mail as well as store in database(mysql) in PHP

I want to implement a feature on form submission It should be able to mail as well It should be stored in Database simultaneously(mail will send both mail id which is specified in form as well as website email).Before asking here I made some of the research but I can find both action individually.I am able to save in Database not able to send mail getting error as SMTP ERROR: Failed to connect to server: (0). For sending a mail I am using PHP mailer. please provide me some guidelines to do it.
code I tried So Far:
//database connection
<?php include_once"config.php"; ?>
<?php
use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";
$mail = new PHPMailer();
if (isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$query = "INSERT INTO mail_db(first_name,last_name,email) VALUES
('$fname','$lname','$email');";
$result= mysqli_query($connection,$query);
// print_r($result);
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "mail.website.com";
$mail->Port = 465;
$mail->Username = 'sender#website.com';
$mail->Password ='mypassword';
$mail->setFrom("sender#website.com");
$mail->Subject = 'testForm';
$mail->addAddress("$email");
$mail->Body =
"FirstName"."$fname"."\n".
"LasttName"."$lname"."\n".
"email"."$email"."\n";
$mail->send();
$mail->clearAddresses();
$mail->addAddress("recieve#website.com");
$mail->Body = 'New Registration Details'."\n".
"FirstName"."$fname"."\n".
"LasttName"."$lname"."\n".
"email"."$email"."\n";
$mail->send();
if($mail->send()){
header("Location: thanks.html");
}
else{
echo $mail->ErrorInfo;
}
}
?>

Catchable fatal error using PHPMailer

I am trying to create an email confirmation when registering in a page. and i am receiving this error when submiting it.
Catchable fatal error: Object of class PHPMailer could not be converted to string in C:\wamp\www\includes\reg.php on line 90
and here is my code,
<?php
session_start();
require '../class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$email = $_POST['email'];
$mail->Username = "XXX#gmail.com";
$mail->Password = "XXX";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
$mail->From = "XXX#gmail.com";
$mail->FromName = "Your Name";
$mail->addAddress("XXX#live.com","User 1");
$mail->addAddress($email,"User 2");
$mail->addCC("user.3#ymail.com","User 3");
$mail->addBCC("user.4#in.com","User 4");
$mail->Subject = "Confirm your Account!!";
$mail->Body = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
header('refresh: 0; url=../index.php#openModal');
$message = "You are now Registered, Please Sign In.";
echo("<script type='text/javascript'>alert('$message');</script>");
}
?>
this one was the line getting the error,
$mail->Body = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";
and I dont know if using $email works, i need to send email to the email the user has submitted. please help thank you.
Look at the end of the line with error where you have &code=$mail";. You previously defined $mail as PHPMailer, where probably this class doesn't provide __toString() function.
__toString() is a magic function which is called upon when the object has to be converted to string. In your situation, when $mail (a PHPMailer object) has to be inserted as a concatenation in a string, it tries to call __toString() but none is provided.
Instead of using $mail->Body = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";
try using $message = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";
then $mail->Body = $message;

php mailer Mailer Error: Language string failed to load: connect_host?

I keep getting this error..
below is my code:
<?php
ini_set("include_path", "phpmailer/");
require 'phpmailer/class.phpmailer.php';
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->Port='465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = 'mail#gmail.com'; // Change this to your gmail adress
$mailer->Password = '****'; // Change this to your gmail password
$mailer->From = 'maile#gmail.com'; // This HAVE TO be your gmail adress
$mailer->FromName = 'Belmer'; // This is the from name in the email, you can put anything you like here
$mailer->Body = 'This is the main body of the email';
$mailer->Subject = 'This is the subject of the email';
$mailer->AddAddress('another#ymail.com'); // This is where you put the email adress of the person you want to mail
if(!$mailer->Send())
{
echo "Message was not sent<br/ >";
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
Anyone knows this error? Your reply is greatly appreciated.
Got it! Thanks guys for viewing.
There's actually no problem with the code, it has to be some settings on my local server. My solutions is i uploaded the files to my live webserver and everything is working fine.

Categories