如何使用phpmailer发送联系表单

时间:2016-07-07 15:05:18

标签: php email

      <html> 
      <form method="post" action="email.php">
   Email: <input name="email name" id="email" type="text" /><br/>
      Message:<br/><text area name="message" id="message" rows="15" cols="40</text area><br>
       <input type="submit" value="Submit" />
       </form>
     <html/>

我有这些代码,我想在联系表单上发送电子邮件到myemail@gmail.com,但它说输入至少一个收件人,当我输入电子邮件到$ mail-&gt;添加地址($ email)如果我更改为$ mail-&gt; addadress(someone@gmail.com)即使表单上的电子邮件与默认情况不同,也会发送到someone@gmail.com请帮助我。

<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require("PHPMailerAutoload.php");
require "class.phpmailer.php";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = "tls"; 
$mail->Port       = 587;   

$mail->Host = "smtp.gmail.com";  
$mail->Username = "myemail@gmail.com";  // SMTP username
$mail->Password = "*************"; // SMTP password
$mail->AddAddress=$email;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$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";
?>

1 个答案:

答案 0 :(得分:2)

AddAddress是一种方法,而不是属性。

$mail->AddAddress=$email;

应该是:

$mail->AddAddress($email);