繁体   English   中英

联系表格-提交表格后未发送电子邮件

[英]Contact Form - Email not sent when form submitted

我正在尝试为我的网站实施联系表单,但是当我提交表单时,我的电子邮件地址中没有电子邮件。 验证工作正常。 这是PHP文件:

<?php
if(isset($_POST['email'])){

    $email_to = "you@yourdomain.com";
    $email_subject = "AHU Enquiry";

    function died($error){
        echo "We are very sorry, but there are error(s) found with the form you submitted.";
        echo "These error(s) appear below.<br/><br/>";
        echo $error."<br/><br/>";
        echo "Please go back and fix these error(s).<br/><br/>";
        die();  
    }

    if(!isset($_POST['first_name'])||
    !isset($_POST['last_name'])||
    !isset($_POST['email'])||
    !isset($_POST['telephone'])||
    !isset($_POST['comments'])){
        died('We are sorry, but there appears to be a problem with the form you submitted.');   
    }

    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email_from = $_POST['email'];
    $telephone = $_POST['telephone'];
    $comments = $_POST['comments'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!preg_match($email_exp, $email_from)){
        $error_message .= 'The Email address you entered does not appear to be valid.<br/>';        
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp, $first_name)){
        $error_message .= 'The Firt Name you entered does not appear to be valid.<br/>';
    }

    if(!preg_match($string_exp, $last_name)){
        $error_message .= 'The Last Name you entered does not appear to be valid.<br/>';
    }

    if(strlen($comments)<2){
        $error_message .= 'The Comments you entered does not appear to be valid.<br/>';
    }

    if(strlen($error_message)>0){
        died($error_message);
    }

    $email_message = "Form details below.\n\n";

    function clean_string($string){
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

    $headers = 'From: $first_name $last_name<$email_from>\r\n';

    mail($email_to, $email_subject, $email_message, $headers);

?>

Thank you for contacting us. We will be in touch with you very soon.

<?php       

    }
?>

有人可以指出正确的方向吗?

这是错误的屏幕截图。 在此处输入图片说明

我想您通过$ post提供的信息中有错误,或者您没有邮件服务器设置

尝试注释掉整个代码块,并尝试在php mail API网站上使用示例2

http://php.net/manual/zh/function.mail.php

如果这有效,那么您的代码有问题,如果不起作用,则是您的邮件服务器有问题

错误日志也应提供信息。

我没有设置邮件服务器。 提交联系表单后,每次您希望发送电子邮件时,都应进行传出(SMTP)邮件服务器设置,以便将电子邮件实际发送到所需的电子邮件地址。 最简单的方法是使用Swift Mailer PHP代码。 它比使用传统的mail()函数要简单得多。 这是示例代码:

require_once 'SwiftMailer/lib/swift_required.php';

if(isset($_POST['email'])){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];

$email_message = "Form details below.<br/><br/>";
$email_message .= "First Name: ".$first_name."<br/>";
$email_message .= "Last Name: ".$last_name."<br/>";
$email_message .= "Email: ".$email_from."<br/>";
$email_message .= "Telephone: ".$telephone."<br/>";
$email_message .= "Comments: ".$comments."<br/>";

ini_set('max_execution_time', 300);

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
        ->setUsername('youremail@gmail.com')
        ->setPassword('yourpassword');

    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance('AHU Enquiry')
        ->setFrom(array('youremail@gmail.com'=>'Sales Enquiries'))
        ->setTo(array('youremail@gmail.com'=>'Lead Recipients'))
        ->setSubject('Enquiry')
        ->setBody($email_message, 'text/html');
$result = $mailer->send($message);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM