PHPMailer:尝试分配非对象的属性

时间:2017-06-05 07:50:38

标签: php phpmailer

我很沮丧地解决这个问题。 有一次我做到了。第二天,一个错误让我很恼火:

Warning: Attempt to assign property of non-object in C:\XAMPP\htdocs\HAF\includes\sendmail.php on line 356

帮我解决这个问题:(

这是我的代码:

sendmail.php

class SendMail {
    function notification($recipient, $name, $subject, $message) {
        global $email;

        $email->Host = "smtp.gmail.com";
        $email->SMTPAuth = true;
        $email->Username = "******@gmail.com";
        $email->Password = "**********";
        $email->SMTPSecure = "tls";
        $email->Port = 465;

        $email->setFrom('admin@gmail.com', 'My WebApp');

        $email->addAddress($recipient);

        $email->isHTML(true);

        $email->Subject = $subject;
        $email->Body = $message;

        if(!$email->send()) {
            return false;
        } else {
            return true;
        }
    }
}

的index.php

$email = "jaydenjames@gmail.com";
$name = "Jayden James";
$message = "Welcome {$name}!";
$SendMail->notification($email, $name, 'Welcome guest!', $message);

1 个答案:

答案 0 :(得分:0)

使用gmail配置更新答案....

 $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet="UTF-8";
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->Username = 'MyUsername@gmail.com';
    $mail->Password = 'valid password';
    $mail->SMTPAuth = true;

    $mail->From = 'MyUsername@gmail.com';
    $mail->FromName = 'Mohammad Masoudian';
    $mail->AddAddress('anotherValidGmail@gmail.com');
    $mail->AddReplyTo('phoenixd110@gmail.com', 'Information');

    $mail->IsHTML(true);
    $mail->Subject    = "PHPMailer Test Subject via Sendmail, basic";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
    $mail->Body    = "Hello";

    if(!$mail->Send())
    {
      echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
      echo "Message sent!";
    }