【问题标题】:PHP email form does not send emails [duplicate]PHP电子邮件表单不发送电子邮件[重复]
【发布时间】:2017-03-10 22:31:40
【问题描述】:

我只涉足了一点 PHP,我正在努力回忆我去年所做的事情。我无法让它工作 - 用户被重定向到 index.php 但没有任何反应。没有收到电子邮件,也没有验证“电子邮件已发送/未发送”等。

我确信这可能是一个愚蠢的错误。

任何帮助将不胜感激。

contact.html

<form method="post" action="index.php">


<label>Name</label>
<input name="name" placeholder="Type Here">

<label>Email</label>
<input name="email" type="email" placeholder="Type Here">

<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>

<label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

<input id="submit" name="submit" type="submit" value="Submit">

</form>

index.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email']; // GET EMAIL ADDRESS FROM FORM
$to = 'annie.palmer@outlook.com'; 
$subject = 'Website enquiry from' .$name;
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$headers = "From: Annie<$from>\r\nReturn-path: $from" . "\r\n";
$headers .= "Content-type:text/text;charset=ISO-8859-1"; 

?>
<?php
if ($_POST['submit'] && ($_POST['human'] == '4') {
/* Anything that goes in here is only performed if the form is submitted */
if (mail ($to, $subject, $body, $headers, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

【问题讨论】:

  • 您的 php.ini 是否已正确配置以发送电子邮件?您可以将该文件添加到您的问题中吗? This 可能会有所帮助。
  • 如果根本没有 no 输出,那么听起来它甚至还没有到达尝试发送电子邮件的地步。检查 PHP 错误日志,打开错误报告,添加一些额外的 echo 语句只是为了跟踪代码在做什么,等等。
  • mail() 使用 4 个参数,而不是 5 个。RTM php.net/manual/en/function.mail.php
  • @RuhulAmin: "$_POST['human'] == '4' 为什么是 4,是字符串还是数字?你应该使用 $_POST['human'] == 4" - 不,你不应该。通过 GET 或 POST 接收的表单值总是为字符串类型。

标签: php html email


【解决方案1】:

我将此作为社区 wiki 发布;我觉得这不应该有任何代表,我也不想要代表。

mail() 使用 4 个参数,而不是 5 个。

有一个 5th 但它并没有做你期望你的 5th 做的事情。

所以从这里删除, $from

if (mail ($to, $subject, $body, $headers, $from))
                                        ^^^^^^^

From: 属于标头,需要一个电子邮件地址。

手册中的示例:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

【讨论】:

    【解决方案2】:

    尝试使用 PHPMailer。上传到您的网站后,发送电子邮件很容易。

    require ('../mail/PHPMailerAutoload.php');
    require ('../mail/class.phpmailer.php');
    
    //set up your email
    
    $mail = new PHPMailer();
    $mail->isSMTP(); 
    $mail->SMTPAuth   = true;                
    $mail->SMTPSecure = 'tbs'; //set as required 
    $mail->Host       = "enter your email host here"; 
    $mail->Port       = 25;                  
    $mail->Username   = "enter your mail account username here"; 
    $mail->Password   = "enter your mail account password here";
    $mail->From      = "the from email address";
    $mail->FromName  = "the from name";
    $mail->Subject   = "the subject goes here";
    $mail->IsHTML(true); //your choice
    $body = "the body of you email - html or plain text";
    $mail->Body = $body;
    
    $mail->AddAddress("add the email address(es) of recipients");
    $mail->Send();
    

    【讨论】:

    • 坚持发布的内容怎么样?错误在这里很明显。此外,他们还必须下载和配置整个库。
    • 只提供一直被建议作为首选选项的内容。 op的选择。这样做可能会消除错误。也许让他们决定?
    • 当然,但这让他们争先恐后地找出在哪里下载 phpmailer 库以及如何正确配置它。
    • 就像我说的,让他们决定。不完全困难。并且无需配置 - 上传即可。它非常好。你可以在这里下载它sourceforge.net/projects/phpmailer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多