PHP电子邮件表单不会发送电子邮件

时间:2017-03-10 22:31:41

标签: php html email

我只涉及PHP一点,我想记住我去年做了什么。我无法让它工作 - 用户被重定向到index.php但没有任何反应。没有收到任何电子邮件,也没有发送/未发送验证的电子邮件和#39;等

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

任何帮助都将不胜感激。

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>'; 
}
}
?>

2 个答案:

答案 0 :(得分:1)

我将此信息发布为社区维基;我觉得不应该有任何代表,我也不想要代表。

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

有一个第五,但它没有做你期待你的第五次做的事。

请从此处删除, $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);
?>

答案 1 :(得分:0)

尝试使用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();