php联系表单有效,但不会发送到一个特定的电子邮件

时间:2015-08-19 03:11:11

标签: php email phpmailer contact-form

我有这个联系表格与我的电子邮件一起使用但是当我在客户端发送电子邮件时它不会发送邮件,我们都使用相同的godaddy工作区电子邮件程序和我的想法!任何人都可以帮助我。

P.S

我正在使用带有此代码的PHP Mailer脚本

<?php
    /* Form Handling Code */

    // Check if form has been submitted.
    // if the $_POST["email"] variable exists,
    // then the form HAS been submitted already.

    $errors = array( "unsubmitted" => TRUE );

    if( isset( $_POST[ "email" ] ) )
    {
        if( strlen( $_POST[ "email" ] ) > 0 )
        {
            // there was something typed in the email field
            if( strlen( $_POST[ "message" ] ) > 0 )
            {
                // there was something typed in the message field

                require_once( "includes/class.phpmailer.php" );

                // create an instance of the PHPMailer object
                $mail = new PHPMailer();

                // send the email, everything is OK.

                $subject = "Contact Form Email";

                $to = "info@companyname.com";

                $message = "Name: ".mb_strtolower($_POST['name'])." <br /> Telephone:    ".$_POST['telephone']." <br /> E-Mail:    ".$_POST['email']." <br /> Enquiry:    ".$_POST['enquiry']." <br /> Message:    ".$_POST['message']."";

                $enquiry = "Enquiry: {$_POST["enquiry"]}";

                $telephone = "Telephone: {$_POST["telephone"]}";

                $from = filter_var( $_POST[ "email" ], 
                                        FILTER_VALIDATE_EMAIL );

                if( !$from )
                {
                    $errors[ "email" ] = "<p class=\"error\">
                                            Please enter a valid email address.
                                          </p>";
                }

                if( count( $errors ) < 2 )
                {
                    $mail->AddAddress( $to, "Fun Guy" );
                    $mail->SetFrom( $_POST["email"] );
                    $mail->AddReplyTo( $_POST["email"] );
                    $mail->Subject = $subject;
                    $mail->MsgHTML( $message );
                    $mail->AltBody = $message ;

                    if( !$mail->Send() )
                    {
                        $sendError = "<p>{$mail->ErrorInfo}</p>";
                    }
                    else
                    {
                        // clear the first "default" error
                        unset( $errors[ "unsubmitted" ] );
                    }
                }
            }
            else
            {
                $errors[ "message" ] = "<p class=\"error\">
                                            Please enter a message.
                                        </p>";
            }
        }
        else
        {
            $errors[ "email" ] = "<p class=\"error\">
                                    Please enter an email address
                                  </p>";
        }
    }
?>






<div id="contactpageleft">
        <p class="title">Send Us A Message</p>
        <div id="contact-form" class="clearfix">
    <?php if( count( $errors ) > 0 ): ?>
    <?php echo $sendError; ?>
    <form id="formID" action="" class="formular" method="post">
        <?php echo $_POST["name"]; ?>
        <label for="name">Name:</label>
        <input value="<?php echo $_POST["name"]; ?>" type="text" id="name" name="name" value="" placeholder="John Doe" required autofocus />

        <?php echo $errors[ "email" ]; ?>
        <label for="email">Email Address:</label>
        <input value="<?php echo $_POST["email"]; ?>"  type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required />

        <?php echo $errors[ "telephone" ]; ?> 
        <label for="telephone">Telephone: </label>
        <input value="<?php echo $_POST["telephone"]; ?>" type="telephone" id="telephone" name="telephone" value="" />

        <?php echo $errors[ "enquiry" ]; ?> 
        <label for="enquiry">Enquiry: </label>
        <select type="enquiry" id="enquiry" name="enquiry" value="" id="enquiry" name="enquiry">
            <option value="General">General</option>
            <option value="Sales">Sales</option>
            <option value="Support">Support</option>
        </select>

        <?php echo $errors[ "message" ]; ?> 
        <label for="message">Message: </label>
        <textarea id="message" name="message" placeholder="Your message must be greater than 20 charcters" required data-minlength="20"><?php echo $_POST["message"]; ?></textarea>


        <input class="submit" type="submit" value="SEND EMAIL" />

    </form>
    <?php else: ?>
        <h2>Your email was sent successfully!</h2>
    <?php endif; ?>
</div>
    </div>

1 个答案:

答案 0 :(得分:1)

首先,您已将您的代码基于一个过时的示例,并且可能正在使用旧版本的PHPMailer,因此get the latest

您正在伪造From地址,这会阻止许多系统上的传送,因为它会导致SPF失败。将您自己的地址放在发件人地址中,并将提交者的地址放在回复中。

您正在拨打msgHTML,因此也无需设置AltBody

GoDaddy因阻止出站SMTP而闻名。