简体   繁体   中英

php form not sending email

I am using a php script to send an email to a given address, however no email is being sent, even though the script is sending the user to the thank you page. It was working at one stage, but then inexplicably stopped.

Any help is appreciated :)

<?php

$to = "emailaddress@gmail.com \r\n";//<== update the email address
$headers = "From: $email_from \r\n";

$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];


$email_from = 'email@mydomain.com';//<== update the email address
$email_subject = "Contact via the website";
$email_body = "You have received a new message from $name.\n\n".
"Here is the message:\n $message\n\n\n".

$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: http://domain.com/thank-you/');


// Function to validate against any email injection attempts
function IsInjected($str)
{
 $injections = array('(\n+)',
           '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}

?> 

检查是否遇到任何错误。如果邮件发送成功,则mail函数返回true;否则,返回false。

$headers = "From: $email_from \r\n";
  • $email_from isn't yet defined, maybe you've got error here?
  • Don't forget to enable error_reporting to E_ALL to see all errors, also check logs on server to see where's problem, maybe mail cannot be send by SMTP server or something...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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