简体   繁体   English

PHP mail()函数没有发送

[英]PHP mail() function is not sending

I am trying to send an email using PHP I'm using this form: 我正在尝试使用PHP发送电子邮件我正在使用此表单:

<form class="pure-form pure-form-aligned" action="contact.php">
    <fieldset>
        <input type="Text" placeholder="Nome" name="Name">
        <input type="email" placeholder="Email" name="email">
         </fieldset>
         <fieldset>
        <button type="Reset" class="pure-button pure-button-primary">Limpar</button>
        <button type="submit" class="pure-button pure-button-primary">Enviar</button>
        </fieldset>

</form>

and this php code 这个PHP代码

<?php
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];

$mail_to = 'nelson.maia@geralucros.com';
$subject = 'GeraRelax: '.$field_name;

$body_message = 'De: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";

$headers = 'De: '.$field_email."\r\n";
$headers .= 'Responder para: '.$field_email."\r\n";

$mail_status = @mail($mail_to, $subject, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Obrigado pela contacto.');
        window.location = 'index.php';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Envio falhado');
        window.location = 'index.php';
    </script>
<?php
}
?>

I have used it before but now it doesn't work. 我之前使用过它,但现在它不起作用。 I'm guessing this have to do with server configurations? 我猜这与服务器配置有关? Can someone enlighten me on this? 有人可以开导我这个吗?

You need to add the form METHOD! 您需要添加表格METHOD!

<form class="pure-form pure-form-aligned" action="contact.php" METHOD="POST">

Also missing . 也错过了。 to add to headers. 添加到标题。

$headers .= 'De: '.$field_email."\r\n";

Also type in NOME should be NAME 同样输入NOME应该是NAME

<input type="Text" placeholder="Nome" name="Name">

TYPO IN VARIABLE NAMES TYPO in VARIABLE NAMES

$field_name = $_POST['name'];
$field_email = $_POST['Email'];

Your problem is your have created mail incorrectly, if you view the PHP manual: http://php.net/manual/pt_BR/function.mail.php // http://php.net/manual/en/function.mail.php 您的问题是您已经错误地创建了mail ,如果您查看PHP手册: http//php.net/manual/pt_BR/function.mail.php // http://php.net/manual/en/function.mail .PHP

you will see that mail($to, $usbject, $message, $headers); 你会看到那mail($to, $usbject, $message, $headers); and your version does not do this, you have no $message value in your mail item. 并且您的版本没有这样做,您的邮件项目中没有$message值。

Also add 还添加

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

To the top of your code to display error messages which will give you more feedback as to why your email sending is failing. 在代码的顶部显示错误消息,这将为您提供有关您的电子邮件发送失败原因的更多反馈。 And remove the @ from your mail function until you are sure it works. 并从您的邮件功能中删除@ ,直到您确定它有效。


You should have 你应该有

 error_reporting(E_ALL);
 ini_set('display_errors', 1);
 ...
 $mail_status = mail($mail_to, $subject, $body_message, $headers); 

Recommendation: 建议:

  • Use UTF-8 character set, or UTF-16 if possible. 如果可能,请使用UTF-8字符集或UTF-16。

  • Use a dedicated Mailer extension such as PHPMailer or Swift Mailer 使用专用的Mailer扩展,例如PHPMailerSwift Mailer

  • Add error tracking (as exampled) across your code until you're sure it works. 在代码中添加错误跟踪(如示例),直到您确定它有效。

  • Eat five fresh fruit and vegetables a day. 每天吃五种新鲜水果和蔬菜。

  • Also noted from PHP Mail: 还从PHP邮件中注意到:

When sending mail, the mail must contain a From header. 发送邮件时,邮件必须包含From标头。 This can be set with the additional_headers parameter, or a default can be set in php.ini. 这可以使用additional_headers参数设置,或者可以在php.ini中设置默认值。

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. 如果不这样做将导致类似于警告的错误消息:mail():“sendmail_from”未在php.ini中设置或自定义“From:”标头丢失。 The From header sets also Return-Path under Windows. From标头在Windows下也设置了Return-Path。

Add the form method here: 在这里添加表单方法:

<form class="pure-form pure-form-aligned" action="contact.php">

Add the submit button name here: 在此处添加提交按钮名称:

 <button type="submit" name="submit" class="pure-button pure-button-primary">Enviar</button>

Change the php code to this : 将PHP代码更改为:

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

   // your code here..
    .
    .
    .
   // change the mail headers..
   $headers .= 'De: '.$field_email."\r\n";    
} ?>

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

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