PHPMailer:从表单发送电子邮件

时间:2015-09-05 16:45:50

标签: html-email phpmailer

我想实现发送电子邮件功能,我希望允许我开发的系统的管理员能够从HTML表单发送电子邮件给所需的成员。

我在这里搜索并尝试了所有可能的解决方案,但仍然无法使其正常工作。我没有收到电子邮件,SMTPDebug也没有告诉我任何事情。我也关闭了Gmail不太安全的应用程序。

有人可以为我指出它为什么不起作用吗?

这是我的表格:

<form name="compose" method="POST" action="sendmail.php">
  <table>
    <tr>
      <thead>Compose New Message</thead>
    </tr>
    <tr>
      <td>To</td>
      <td>:</td>
      <td>
        <input type="text" id="to" size="57">
      </td>
    </tr>
    <tr>
      <td>Subject</td>
      <td>:</td>
      <td>
        <input type="text" id="subject" size="57">
      </td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>
        <textarea cols="39" rows="10" id="content"></textarea>
      </td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>
        <input type="submit" id="submit" value="Send Message">
      </td>
    </tr>
  </table>
</form>

这是我的sendmail.php:

<?php
require 'includes/PHPMailer/PHPMailerAutoload.php';
require 'includes/PHPMailer/class.smtp.php';

if(isset($_POST['submit']))
{
    $recipient = $_POST['to'];
    $subject = $_POST['subject'];
    $body = $_POST['content'];

    $mail = new PHPMailer;
    $mail->isSMTP();

    //smtp debugging
    $mail->SMTPDebug = 2;
    $mail->DebugOutput = 'html';

    $mail->Host = 'smtp.gmail.com';
    $mail->Host = gethostbyname('smtp.gmail.com');

    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true;
    $mail->SMTPSecure = 'tls';

    $mail->Username = "myemail";
    $mail->Password = "mypassword";

    $mail->setFrom('myemail', 'Azie');
    $mail->AddAddress = $recipient;
    $mail->Subject = $subject;
    $mail->Body = $body;

    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
}
?>

0 个答案:

没有答案