使用PHPMailer导致致命的致命错误

时间:2014-10-21 16:32:33

标签: php phpmailer

我正在尝试在页面中注册时创建电子邮件确认。我在提交时收到此错误。

Catchable fatal error: Object of class PHPMailer could not be converted to string in C:\wamp\www\includes\reg.php on line 90

这是我的代码,

<?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>");

}


?>

这一行是得到错误的一行,

$mail->Body = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";

我不知道使用$ email是否有效,我需要向用户提交的电子邮件发送电子邮件。请帮忙谢谢。

2 个答案:

答案 0 :(得分:2)

如果您有&code=$mail";,请查看错误行的结尾。您之前已将$mail定义为PHPMailer,此类可能不会提供__toString()功能。

__toString()是一个神奇的函数,当对象必须转换为字符串时调用该函数。在您的情况下,当$mail(一个PHPMailer对象)必须作为字符串中的串联插入时,它会尝试调用__toString()但不提供任何内容。

答案 1 :(得分:0)

而不是使用$mail->Body = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";

尝试使用$message = "Confirm Your Email, Click link to verify your account,,<br /><br />http://localhost/includes/emailconfirm.php?email=$_POST[email]&code=$mail";

然后$ mail-&gt; Body = $ message;