Contact form not sending email to my address - php

I am having trouble with my contact form, the message isn't being sent to the email address I specified. Could anyone tell me if I'm doing something wrong here:
$emailTo = 'email#myemailwenthere.com';
$siteTitle = 'My sitename went here';
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if(isset($_POST['submitted'])) {
// require a name from user
if(trim($_POST['contactName']) === '') {
$nameError = 'Forgot your name!';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot to enter in your e-mail address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
$subject = 'New message to ';
$sendCopy = "";
$body = "Name";
$headers = 'From: ' .' <'.$email.'>';
mail($emailTo, $subject, $body, $headers);
// upon no failure errors let's email now!
if(!isset($hasError)) {
$subject = 'New message to '.$siteTitle.' from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nMessage: $comments";
$headers = 'From: ' .' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
//Autorespond
$respondSubject = 'Thank you for contacting '.$siteTitle;
$respondBody = "Your message to $siteTitle has been delivered! \n\nWe will answer back as soon as possible.";
$respondHeaders = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;
mail($email, $respondSubject, $respondBody, $respondHeaders);
// set our boolean completion value to TRUE
$emailSent = true;
}

Try with a mini script to check or sending mail is possible:
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>

I would advise you to try PHPMailer with SMTP configured, it gives lot of flexibility.
PHPMailer

Related

E-mail is not receiving after click onn submit [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have a contact form. Form action access this code after click on submit.
<?php
// Email address verification
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email));
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'xxxxxx#gmail.com';
$clientName = trim($_POST['name']);
$clientEmail = trim($_POST['email']);
$message = trim($_POST['message']);
$subject = "New message from your site";
$array = array('nameMessage' => '', 'emailMessage' => '', 'messageMessage' => '',);
if($clientName == '') {
$array['nameMessage'] = 'Please enter your name.';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Please insert a valid email address.';
}
if($message == '') {
$array['messageMessage'] = 'Please enter your message.';
}
if($clientName != '' && isEmail($clientEmail) && $message != '') {
// Send email
$headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject, $message, $headers);
}
echo json_encode($array);
}
?>
<?php
// Email address verification
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email));
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'xxxxxxxx#gmail.com';
$clientName = trim($_POST['name']);
$clientEmail = trim($_POST['email']);
$message = trim($_POST['message']);
$subject = "New message from your site";
$array = array('nameMessage' => '', 'emailMessage' => '', 'messageMessage' => '',);
if($clientName == '') {
$array['nameMessage'] = 'Please enter your name.';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Please insert a valid email address.';
}
if($message == '') {
$array['messageMessage'] = 'Please enter your message.';
}
if($clientName != '' && isEmail($clientEmail) && $message != '') {
// Send email
$headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject, $message, $headers);
}
echo json_encode($array);
}
?>
But email is not coming from that form. Please tell me what i have to do. PHP curl is active. this code is runing on ubuntu based server...
If you use a localhost no email will be sent because localhost is a virtual server. Others, I don't know.

PHP Redirect Issue

I'm having a difficult time getting my page to redirect after form submission. I've followed the advice that I've been able to find so far and no luck. Any help would be greatly appreciated.
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
$subject = "CONTACT FORM";
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'email#domain.com';
$body = "Name: $name \nEmail: $email";
$headers = 'From: Bond Limo (Newsletter Signup) <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) {
header("Location: http://www.website.com");
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="side-form">
Your header is not being processed. In order for your header to be processed and the redirect to occur, you will have to call the die() function after your header. Like so:
<?php
if(isset($emailSent) && $emailSent) {
header("Location: http://www.website.com");
die();
}
?>
Additionally, your code can be optimized by not checking:<?php if(isset($hasError)) { //If errors are found ?> again. Rather, just connect it with the above if statement and use an else statement like so:
// If there is no error, send the email
if (!isset($hasError)) {
$emailTo = 'email#domain.com';
$body = "Name: $name \nEmail: $email";
$headers = 'From: Bond Limo (Newsletter Signup) <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
$emailSent = mail($emailTo, $subject, $body, $headers);
} else {
// Errors found
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
}

Php email script failed

I have some trouble with this, Anyone know why it won't work?
I have this homepage where it uses this script for sending an email, but it won't work.
When I call that I should get an email sent, but it just runs without any error.
<?php
$emailTo = 'youremail';
$siteTitle = 'SiteTitle';
//error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if(isset($_POST['submitted'])) {
$hasError = false;
// require a name from user
if(trim($_POST['contactName']) === '') {
$nameError = 'name plz!';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot Email?';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'It's not right fool';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'Forgot something=';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$subject = 'New message to '.$siteTitle.' from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nMessage: $comments";
$headers = 'From: ' .' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
//Autoresponse
$respondSubject = 'Thank you for contacting '.$siteTitle;
$respondBody = "Your message to $siteTitle has been delivered! \n\nWe will answer back as soon as possible.";
$respondHeaders = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;
mail($email, $respondSubject, $respondBody, $respondHeaders);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
Add \ in ', in all occurences
$emailError = 'It\'s not right fool';
instead of
$emailError = 'It's not right fool';
You need to escape any 's with \ in strings that are enclosed in 's. For example, you need to change $emailError = 'It's not right fool' to $emailError = 'It\'s not right fool'.
The same goes for "s when enclosed in "s.
You said it runs without any error, so it may not be a PHP error. Have you made sure that SMTP is set up properly on your sever? With the new code you just posted, are there any new errors?

How can I modfy this send.php $mail call to include the $phone paramerter

How do I get the phone number parameter, which I added, into the body of the email sent by this php script?:
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$subject = trim($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
$error .= 'Name required! ';
// Check email
if(!$email)
$error .= 'E-mail required! ';
if($email && !ValidateEmail($email))
$error .= 'E-mail address is not valid! ';
// Check phone
if(!$phone)
$error .= 'Phone number required! ';
// Check message
if(!$message)
$error .= "Please enter your message!";
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
echo 'OK';
}
else
echo '<div class="errormsg">'.$error.'</div>';
}
I've added $phone to the $post section, created the feild in my form, now I just need to get the contents returned in the email that is recieved. Any tips greatly appreciated.
If I understood correctly what you want, you need something like that:
$message = "Phone: $phone\r\n" . $message;
before
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());

Mail delivery failed: returning message to sender

I would like to add a failed message re-direct to my mail script, so that if a user enters a wrong address in the email field it gets returned to me, and not to my hosting company's inbox, how do I do that? I've already added return-path but doesn't work, what else can i do to get this code to work.
Here is the code:
<?php if(isset($_POST['submit'])) {
if(trim($_POST['first-name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['first-name']);
}
//Check to make sure that the last name field is not empty
if(trim($_POST['last-name']) == '') {
$hasError = true;
} else {
$lname = trim($_POST['last-name']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if(!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure that the phone field is not empty
if(trim($_POST['tel']) == '') {
$hasError = true;
} else {
$phone = trim($_POST['tel']);
}
//Check to make sure that the phone field is not empty
if(trim($_POST['company']) == '') {
$hasError = true;
} else {
$company = trim($_POST['company']);
}
foreach (array($_POST['q1']) as $value) {
$q1 = $value[0];
$q2 = $value[1];
$q3 = $value[2];
}
//If there is no error, send the email
if(!isset($hasError)) {
$to = 'me#host.com';
$recipient = $email;
$subject = 'Subject';
$headers = "From: Me\n" . $to . "\r\n";
$headers .= "Reply-To: ". $to . "\r\n";
$headers .= "Return-Path: ". $to . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$msg1 = "First Message";
$msg2 = "Second Message";
//Send Email
mail($recipient, $subject, $msg2, $headers);
mail($to, $subject, $msg1, $headers);
$emailSent = true;
}
}
?>
the return path is set by the mta based on the envelope sender, you can't just set that in the headers yourself. You can try to set the envelope sender using the $additional_parameters argument of the mail function. See Example #3 on http://www.php.net/manual/en/function.mail.php
In your case, that would be something like
mail($recipient, $subject, $msg2, $headers, "-f $to");
On some systems overriding the envelope sender using -f is restricted. In that case you'd probably have to switch to submitting the mail via SMTP instead of calling the sendmail binary via mail().

Categories