Reputation: 191
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 = "[email protected]";
$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 = "[email protected]";
$mail->FromName = "Your Name";
$mail->addAddress("[email protected]","User 1");
$mail->addAddress($email,"User 2");
$mail->addCC("[email protected]","User 3");
$mail->addBCC("[email protected]","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.
Upvotes: 0
Views: 1994
Reputation: 49
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;
Upvotes: 0
Reputation: 927
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.
Upvotes: 2