在php中对非对象调用成员函数MsgHTML()

在php中对非对象调用成员函数MsgHTML(),php,email,Php,Email,我使用php创建了从localhost发送的简单邮件 她的密码是: HTML: 电子邮件: 消息: EMAIL.PHP: <?php require_once('class.phpmailer.php'); $mail->MsgHTML($body); $body ='A sample email'; $mailer->IsSMTP(); $mailer->SMTPDebug = 0; $mailer->SMTPAuth = true; $mailer-

我使用php创建了从localhost发送的简单邮件

她的密码是:

HTML:


电子邮件:
消息:

EMAIL.PHP:

<?php

require_once('class.phpmailer.php');


$mail->MsgHTML($body);
$body ='A sample email';
$mailer->IsSMTP();
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mailer->Host = 'smtp.gmail.com';
$mailer->Username = 'YYYYYYYYYY@gmail.com';
$mailer->Password = 'XXXXXXXXX';
?>

运行此代码时

显示以下错误:

致命错误:对非联机对象调用成员函数MsgHTML()

注意:从何处获得phpmailer.php源代码

我是php新手,但我想知道,尤其是这一节

谁能帮我解决这个问题


提前感谢,

也许
$mail
没有实例化,拼写错误(您的意思是
$mailer
?)。另外,您应该在
MsgHTML($body)
之前设置
$body
。从中,您可能需要添加此项

$mailer = new PHPMailer;
并进行如下更改:

$mailer = new PHPMailer;
$body ='A sample email';

//$mail->MsgHTML($body);
$mailer->MsgHTML($body);

$mailer->IsSMTP();
...

以下是一个示例:

HTML

<form method="post" action="email.php">
  Email: <input name="email" id="email" type="text" /><br />

  Message:<br />
  <textarea name="message" id="message" rows="15" cols="40"></textarea><br />

  <input type="submit" value="Submit" />
</form>

电子邮件:
消息:

mail.php

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require_once('class.phpmailer.php');

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_PHPMailer@bradm.inmotiontesting.com";  // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("bradm@inmotiontesting.com", "Brad Markle");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

$mail未实例化?您需要完成代码。我使用PHPMailer添加了一个工作示例的链接。复制并粘贴,根据需要修改。我复制并发布到答案中的示例应该足够了。很抱歉,我们已经解决了“致命错误:在非对象联机6上调用成员函数MsgHTML()”的问题。关于让PHPMailer发送电子邮件的更多问题,本帖不再讨论。请尝试取得一些进展,并在进行过程中发布更多问题。我们需要把问题集中在一个主题上,以便对未来的读者有用。。需要哪些文件?。
<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require_once('class.phpmailer.php');

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_PHPMailer@bradm.inmotiontesting.com";  // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("bradm@inmotiontesting.com", "Brad Markle");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>