PHPMailer不使用SMTP服务器

时间:2014-08-09 18:16:03

标签: php html email phpmailer

我尝试使用PHPMailer通过SMTP服务器从联系表单发送电子邮件。当我点击"提交"页面变成空白,并且网址中有一个#标签符号,当我在服务器上尝试时,该页面以简单的html文本显示"出现服务器错误"或类似的东西。我可能已经通过phpmailer经历了超过20个堆栈溢出线程,我无法弄明白。我删除了所有的$ mail代码,包括$ mail = new PHPMailer();文本,它会工作,并将我发送到主页,但它不会明显发送电子邮件,以便错误的位置。

PHP和HTML在一个页面上以防万一有人想知道。

PHP

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $firstname = trim($_POST["firstname"]);
    $lastname = trim($_POST["lastname"]);
    $email = trim($_POST["email"]);
    $number = trim($_POST["number"]);
    $message = trim($_POST["message"]);


    if ($firstname == "" OR $lastname == "" OR $email == "" OR $number == "" OR $message == "") {
        echo "You must specify a value for name, email address, and message.";
        exit;
    }

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            echo "There was a problem with the information you entered.";    
            exit;
        }
    }

    if ($_POST["address"] != "") {
        echo "Your form submission has an error.";
        exit;
    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = "smtp.postmarkapp.com";
    $mail->Port = 2525;
    $mail->Username = "**********";
    $mail->Password = "**********";

    $mail->SetFrom('site-admin@example.com', 'Web App');
    $mail->Subject = "Contact Form Submission | " . $name;

    $email_body = "";
    $email_body = $email_body . "First Name: " . $firstname . "<br>";
    $email_body = $email_body . "Last Name: " . $lastname . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Number: " . $number . "<br>";
    $email_body = $email_body . "Message: " . $message;

    if($mail->Send()) {
        echo "Message sent!";
    } else {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    header("Location: index.php");
    exit;
}
?>

HTML

        <div class="contact-form">

                <form action="#" method="post">
                    <p>
                        <label for="firstname">First Name</label>
                        <input id="firstname" name="firstname" type="text">
                    </p>
                    <p>
                        <label for="lastname">Last Name</label>
                        <input id="lastname" name="lastname" type="text">
                    </p>
                    <p>
                        <label for="email">Your Email</label>
                        <input id="email" name="email" type="email">
                    </p>
                    <p>
                        <label for="number">Your Phone Number</label>
                        <input id="number" name="number" type="text">
                    </p>
                    <p>
                        <label for="message">Message</label>
                        <textarea id="message" name="message" type="message"></textarea>
                    </p>
                    <p class="message">
                        <input type="submit" value="SUBMIT" id="submit">
                    </p>
                    <p class="addressvalidation">
                        <label for="address">Address</label>
                        <input id="address" name="address" type="text">
                        <p class="displaytext">Please leave this field blank.</p>
                    </p>
                </form>

1 个答案:

答案 0 :(得分:1)

首先,改变:

<?php 

<?php

error_reporting(E_ALL);
ini_set('display_errors','1');

显示所有错误。

提交表单,您应该已经显示错误。

如果它无法确保在:

require_once("inc/phpmailer/class.phpmailer.php");
也应该 使用正确的路径(在Linux路径上区分大小写)。

你也可以评论一下:

header("Location: index.php");

用于测试以显示任何信息。现在您显示错误,但在它之后您尝试进行重定向。在重定向之前,您不应该显示任何内容,否则它将无法正常工作。