简体   繁体   English

PHP联系表不发送电子邮件

[英]PHP Contact Form Not Sending Email

I'm writing to create a contact form on my website and then get the information sent to my inbox, however it is not working whatsoever.我写信是为了在我的网站上创建一个联系表格,然后将信息发送到我的收件箱,但是它无论如何都不起作用。 Please take a look at my code below (PHP is not my thing) and let me know where i've gone wrong.请看看我下面的代码(PHP 不是我的事),让我知道我哪里出错了。

Here's the PHP script:这是 PHP 脚本:

<?php

$to = 'example@gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];
$project = $_POST['project'];
$range1 = $_POST['range1'];
$range2 = $_POST['range2'];

$body = <<<EMAIL

Hi, my name is $name.

I'd like to discuss the possibility of working together on a $project.

My budget for the project is £$range1 and I would like to complete the project within $range2 Month(s).

Additional Information:
$message

Regards, $name

<hr>
$name
$email
$tel

EMAIL;

$header = "From: $email";

if($_POST['send']){
mail($to, $subject, $body, $header);
$feedback = 'Thank you for your message, we will get back to you shortly.';
}

?>

And here's the HTML form:这是 HTML 表单:

<form id="form_id" name="form_name" action="" method="post">

  <div>
<input type="text" name="name" id="name" placeholder="Name" required/>
  </div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
  </div>
<div>
<input type="tel" name="tel" id="tel" placeholder="Phone" required/>
  </div>

<div>
  <select required id="project">
<option selected>Select type of project…</option>
<option value="Responsive Website">Responsive Web Design</option>
<option value="Graphic Design">Graphic Design</option>
<option value="Motion Graphics">Motion Graphics</option>
  </select> 
</div>

  <div>
<label for="range1">Budget: </label>
<input type="range" name="range1" id="range1" min="400" max="2000" step="50" value="6" required onchange="rangevalue.value=value"><output id="rangevalue">400</output>
  </div>

<div>
<label for="range2">Timeframe: </label>
<input type="range" name="range2" id="range2" min="1" max="12" step=".5" value="1" required onchange="rangevalue1.value=value"><output id="rangevalue1">1</output>
  </div>

<div>
<label for="message">Additional Information: </label><br/>
<p>(Please use this space to tell us about your company, the main objectives of the proposed website and anything else you think might be useful)</p>
<textarea name="message" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
  </div>

<div>
<input type="submit" name="submit" value="submit" />
  </div>

</form>
<p id="feedback"><?php echo $feedback; ?></p>

Thanks for the help.谢谢您的帮助。 FYI this can be achieved easily with WordPress through Contact Form 7 (or a similar plugin).仅供参考,这可以通过联系表格 7(或类似插件)使用 WordPress 轻松实现。

I think you should take a look at this or this , be aware that despite W3Schools may serve as a basics tutorial because of the friendly-user examples, always complement with other resources look here why .我认为你应该看看这个这个,请注意,尽管 W3Schools 可能因为友好的用户示例而作为基础教程,但总是补充其他资源,看看这里为什么

Then you can take a look at this answers, this is just because you need some more basis to understand everything you are doing.然后你可以看看这个答案,这只是因为你需要更多的基础来理解你所做的一切。

I can't see your $_POST['send'] name in the form, or it's just too late and I'm tired:我在表单中看不到您的 $_POST['send'] 名称,或者太晚了,我累了:

Don't know, but maybe, you want this in your form before the submit:不知道,但也许,您希望在提交之前将其包含在表单中:

<input type="hidden" name="send" >

Then:然后:

I already posted this answer HERE , but here it is:我已经在这里发布了这个答案,但这里是:

First i think your missing some headers look at those首先,我认为您缺少一些标题,请查看这些标题

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

2nd check if its true or false when calling the mail function第二次在调用邮件函数时检查它是真还是假

if( mail($email_to, $email_subject, $email_message, $headers)!==true)
    {
        die('Fail to send');


    }
    die('Success');

    }

You should take a look at the isset() function to make sure all your variables are set from the form.您应该查看isset()函数以确保所有变量都是从表单设置的。

<input type="submit" name="submit" value="submit" />

You have to change this line to:您必须将此行更改为:

<input type="submit" name="send" value="submit" />



  if($_POST['send']){ 

actually checking if submit button is clicked...实际检查提交按钮是否被点击...

And, yes - if html and php are on different pages, you have to set proper form action link...而且,是的 - 如果 html 和 php 在不同的页面上,您必须设置正确的表单操作链接...

I have a similar issue and cannot get my head around it.我有一个类似的问题,无法解决它。 Any help would be greatly appreciated.任何帮助将不胜感激。

here is my PHP:这是我的PHP:

<?php 
//////////////////////////
//Specify default values//
//////////////////////////

//Your E-mail
$your_email = 'help@nabdm.com';

//Default Subject if 'subject' field not specified
$default_subject = 'From NABDM Contact Form';

//Message if 'name' field not specified
$name_not_specified = 'Please type a valid name';

//Message if e-mail sent successfully
$email_was_sent = 'Thanks, your message successfully sent';

//Message if e-mail not sent (server not configured)
$server_not_configured = 'Sorry, mail server not configured (function "mail()" disabled on your server?)';


///////////////////////////
//Contact Form Processing//
///////////////////////////
$errors = array();

//"name" field required by this PHP script even if 
// there are no 'aria-required="true"' or 'required' 
// attributes on this HTML input field
if(isset($_POST['name'])) {
    
    if(!empty($_POST['name']))
        $sender_name  = stripslashes(strip_tags(trim($_POST['name'])));
    
    if(!empty($_POST['message']))
        $message      = stripslashes(strip_tags(trim($_POST['message'])));
    
    if(!empty($_POST['email']))
        $sender_email = stripslashes(strip_tags(trim($_POST['email'])));
    
    if(!empty($_POST['subject']))
        $subject      = stripslashes(strip_tags(trim($_POST['subject'])));


    //Message if no sender name was specified
    if(empty($sender_name)) {
        $errors[] = $name_not_specified;
    }

    $from = "MIME-Version: 1.0" . "\r\n" ;
    $from .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
    $from .= (!empty($sender_email)) ? 'From: '.$sender_email : '';

    $subject = (!empty($subject)) ? $subject : $default_subject;


    //sending message if no errors
    if(empty($errors)) {
        
        //duplicating email meta (from and subject) to email message body
        $message_meta = '';
            //From name and email
        $message_meta .= 'From: '. $sender_name . ' ' . $sender_email . "<br>";
            //Subject or default subject
        $message_meta .= 'Subject: '. ( $subject ? $subject : $default_subject ) . "<br>";

        //adding another CUSTOM contact form fields that added by user to email message body
        foreach ($_POST as $key => $value) {
            //checking for standard fields 
            if ($key == 'name' || $key == 'message' || $key == 'subject' || $key == 'email'  ) {
                continue;
            }
            //adding key-value pare to email message body
            $message_meta .= stripslashes(strip_tags(trim($key))) . ': ' . stripslashes(strip_tags(trim($value))) . "<br>";
        }

        $message = $message_meta . "<br>" . 'Message:' . "<br>" . $message;
        $message = wordwrap($message, 70);
    
        if (mail($your_email, $subject, $message, $from)) {
            echo $email_was_sent;
        } else {
            $errors[] = $server_not_configured;
            echo '<span class="form-errors">' . implode('<br>', $errors ) . '</span>';
        }
    } else {
        echo '<span class="form-errors">' . implode('<br>', $errors ) . '</span>';
    }
} else {
    // if "name" var not send ('name' attribute of contact form input field was changed or missing)
    echo '"name" variable were not received by server. Please check "name" attributes for your input fields';
}
?>

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

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