使用phpMailer通过Gmail发送联系表单

时间:2013-08-14 14:36:40

标签: php

我是一名初级网页设计师/开发者,我目前正在尝试修改网站的联系表单。该网站是在一段时间之前创建的,联系表格已被破坏。我正在尽力修复联系表格,而不是改变其他任何事情,但我很难这样做。

我认为所有文件都是html文件,都以.asp结尾,而不是.html。所以,我听说你可以使用PHP发送邮件,所以我下载了一个教程(我实际看了大约30个不同的教程,找到一个好的教程),并认为我得到了它的工作。我没有。

我的目标是从表单,php文件,我的Gmail,然后到我的客户端发送信息。这都是使用 phpMailer 。我的phpMailer在服务器上的同一文件夹中,我的php文件和联系表单在;你知道,所以他们可以看到对方。但是,我无法让这件事发挥作用。我一直收到500服务器错误。

在服务器上启用了PHP,并且没有阻止php邮件。

我的代码有问题吗?或者它是我不知道的服务器事物。无论如何,我非常困惑和沮丧。

包含html表单的.asp文件

<form method="post" action="contact_form.php">
             <p>
                <label for="name">Name:</label>
                <input name="name" type="text" class="txt" id="name" size="30" />
            </p>
             <p>
                <label for="company">Company:</label>
                <input name="company" type="text" class="txt" id="company" size="30" />
             </p>
              <p>
                <label for="city">Email:</label>
                <input type="text" name="email" id="email" class="txt" />
              </p>
             <p>
                <label for="comments">Comments:</label>
                <textarea name="comments" cols="30" rows="5" class="txtarea" id="comments"></textarea>
             </p>
             <p>
                <label for="blank">&nbsp;</label>
                <input type="submit" name="btn_submit" id="btn_submit" class="btn" value="Send"/>
             </p>
    </form>

发送电子邮件的php文件的代码

<?php

$name = trim($_POST['name']);
$company = $_POST['company'];
$email = $_POST['email'];
$comments = $_POST['comments'];

$site_owners_email = 'support@macnx.com'; // Replace this with your own email address
$site_owners_name = 'Landin'; // replace with your name

if (strlen($name) < 2) {
    $error['name'] = "Please enter your name";  
}

if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address"; 
}

if (strlen($comments) < 3) {
    $error['comments'] = "Please leave a comment.";
}

if (!$error) {

    require_once('phpMailer/class.phpmailer.php');
    $mail = new PHPMailer();

    $mail->From = $email;
    $mail->FromName = $name;
    $mail->Subject = "Website Contact Form";
    $mail->AddAddress($site_owners_email, $site_owners_name);
    $mail->Body = $company, $comments;

    // GMAIL STUFF

    $mail->Mailer = "smtp";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587;
    $mail->SMTPSecure = "tls"; 

    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "inquiry@getgearhead.com"; // SMTP username
    $mail->Password = "B******A"; // SMTP password

    $mail->Send();

    echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";

} # end if no error
else {

    $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
    $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
    $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;

    echo $response;
} # end if there was an error sending

?>

非常感谢任何帮助!

修改
我收到要发送的电子邮件,有语法错误,它已修复。
虽然,在电子邮件发送后它仍然给我500服务器错误,即使电子邮件发送正确。它与.php底部的回声响应有关。

echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";

} # end if no error
else {

    $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
    $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
    $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;

    echo $response;
} # end if there was an error sending


就像我之前说过的那样,我从一个教程中得到了这个,并且那个人正在使用各种javascript和css,我试图忽略其中的大部分内容,因此我的一些代码并不准确。
有没有更好的方法告诉提交信息的人已发送电子邮件?我只是需要它去一个单独的页面,或者显示在某个地方,它现在所做的就是给出500服务错误。

感谢。

1 个答案:

答案 0 :(得分:0)

这条线似乎被打破了:

$mail->Body = $company, $comments;

也许你想说

$mail->Body = $company . $comments;