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

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.

Related

contact.php not validating or sending correctly after adding new input fields

php novice back again with some minor issues, I've researched for hours but can't find the solution.
I already use this form for another form and it works fine, but this time I added the Input fields of nric, rate and a checkbox "agree". These are the 3 fields that don't validate and stop the form from sending. Any help would be appreciated
Here is the code:
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$phone = ($_GET['phone']) ? $_GET['phone'] : $_POST['phone'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$nric = ($_GET['nric']) ?$_GET['nric'] : $_POST['nric'];
$rate = ($_GET['rate']) ?$_GET['rate'] : $_POST['rate'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
$agree = ($_GET['agree']) ?$_GET['agree'] : $_POST['agree'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your Full name Surname in UPPERCASE.';
if (!$phone) $errors[count($errors)] = 'Please enter your contact number - e.g. +6012345678.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$nric) $errors[count($errors)] = 'Please enter your Business No or NRIC if not a business.';
if (!$rate) $errors[count($errors)] = 'Please enter the rate you wish to pay (in RM)';
if (!$comment) $errors[count($errors)] = 'Please describe what you require this person for and when.';
if (!$agree) $errors[count($errors)] = 'Please agree to the Booking Fee.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - replace your email here
$to = 'me#myemail.com';
//sender - from the form
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from ' . $name;
$message = 'Name: ' . $name . '<br/><br/>
Phone: ' . $phone . '<br/><br/>
Email: ' . $email . '<br/><br/>
NRIC: ' . $nric . '<br/><br/>
Rate: ' . $rate . '<br/><br/>
Agree: ' . $agree . '<br/><br/>
Message: ' . nl2br($comment) . '<br/>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
Are you sure it isn't as simple as a missing quote.
Look at
$to = me#myemail.com';
It should be
$to = 'me#myemail.com'; // ** Missing leading quote on string

PHP Contact Form not emailing all fields

I have VERY little experience with PHP.
I am testing out a contact form for this website. I am only getting the "subject, message, and header" fields in the email.
I need the telephone and name fields to show in my email message as well.
What am I doing wrong?
Any help is appreciated -- thanks!
<?php
$name;$email;$message;$captcha;$telephone;
if(isset($_POST['name'])){
$name=$_POST['name'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['telephone'])){
$telephone=$_POST['telephone'];
}if(isset($_POST['message'])){
$message=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form. <a href=http://www.website.com#contact/>Back to Form</a></h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcYjgcT****q9vhur7iH_O4dZPl4xUAVwW8=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}else
{
// Checking For Blank Fields..
if ($_POST["name"] == "" || $_POST["email"] == "" || $_POST["telephone"] == "" || $_POST["message"] == "") {
echo "Fill All Fields..";
} else {
// Check if the "Sender's Email" input field is filled out
$email = $_POST['email'];
// Sanitize E-mail Address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email) {
echo "Invalid Sender's Email";
} else {
$to = 'email#gmail.com';
$subject = 'Contact Form';
$message = $_POST['message'];
$name = $_POST['name'];
$headers = 'From:' . $email . "\r\n";
// Sender's Email
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message .= "\r\n Name: " . $name;
$message .= "\r\n Email: " . $email;
$message .= "\r\n Telephone: " . $telephone;
$message .= "\r\n Message: " . $message;
// Send Mail By PHP Mail Function
if (mail($to, $subject, $message, $headers)) {
echo "Your mail has been sent successfully!<a href=http://wwwwebsite.com>Back</a>";
} else {
echo "Failed to send email, try again.";
exit ;
}
}
}
}
?>
The $message variable is the content of the mail that will be sent to your adress, add the values you want to get in your mail to that variable.
like so : (since you're not using HTML mail I think)
$message .= "\r\n Name : " . $name;
$message .= "\r\n Email : " . $email;
etc..
Before you call the mail function.
ALSO :
You're adding Phone number, Email, and message in your $email variable. you should give these their own variable.

how to send email using php script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to send email using php script but its not working can you suggest me the error
The following code
<?php
try
{
$mail="atul#divyampg.0fess.us";
$contents="message";
$emailto1="atulkumaronline#gmail.com";
$subject="testing";
$headers="adesh";
mail($emailto1, $subject, $contents, $headers);
echo "mail send";
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
http://php.net/manual/en/function.mail.php.
"adesh" is not a header.
Valid headers:-
'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Also, mail will return a bool value. You can implement like this :-
$contents="message";
$emailto1="atulkumaronline#gmail.com";
$subject="testing";
if(mail($emailto1, $subject, $contents))
echo "Sent";
else
echo "Error sending Email";
try this.
.
if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email']; //'email' must be the same as name="email" in the input
$subject = $_POST['subject'];
$message = $_POST['message'];
$spamcheck = $_POST['spamcheck'];
if(trim($name) == '')
{
$error = '<div class="error">Please enter your name!</div>';
}
else if(trim($email) == '')
{
$error = '<div class="error">Please enter your email address!</div>';
}
else if(!isEmail($email))
{
$error = '<div class="error">You have enter an invalid e-mail address. Please, try again!</div>';
}
else if(trim($subject) == '')
{
$error = '<div class="error">Please enter a subject!</div>';
}
else if(trim($message) == '')
{
$error = '<div class="error">Please enter your message!</div>';
}
else if(trim($spamcheck) == '')
{
$error = '<div class="error">Please enter the number for Spam Check!</div>';
}
else if(trim($spamcheck) != '5')
{
$error = '<div class="error">Spam Check: The number you entered is not correct! 2 + 3 = ????</div>';
}
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
// make sure to change the email address below or you will nver receive mails
// the email will be sent to:
$to = "henry4u.u4edozie#gmail.com";
// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change the text if you wish.
$subject = '[Contact Form] : ' . $subject;
// the mail message ( add any additional information if you want )
$msg = "$message";
//Extras: User info (Optional!)
//Delete this part if you don't need it
//Display user information such as Ip address and browsers information...
$msg .= " \r\n\n---User information--- \r\n"; //Title
$msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
$msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
$msg .= "User come from : ".$_SERVER["HTTP_REFERER"]; //Referrer
// END Extras
mail($to, $subject, $msg, "From: $name <$email>\r\nReply-To: $name <$email>\r\nReturn-Path: $email\r\n");
?>
<!-- Message sent! (change the text below as you wish)-->
<h1>Congratulations!!</h1>
<p>Thank you <b><?=$name;?></b>, your message is sent! We will get back to you as soon as possible.</p>
<p>Return to the home page or use the navigation above.</p>
<!--End Message Sent-->
<?php
}
}
if(!isset($_POST['send']) || $error != '')
{
?>

Contact form not sending email to my address

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

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?

Categories