I am using the form below. I am using the page, contact.php to display the form and submit the form. The form validates and says email sent but when i check the email it doesn't show.
PHP:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("me#mysite.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='contact.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
In your form when the 'thank you' message displays it means the mail function has been run, not that it succeeded.
the php mail function returns true when it succeeds and othwerwise false.
Could you please try something like:
if(mail("me#mysite.com", "Subject: $subject",$message, "From: $email")){
echo "Thank you for using our mail form";
}else{
echo "hmm... seems the mail cannot be sent";
}
also is the mail function allowed in the php.ini?
Check your php log. Maybe it does fail because it does not have a SMTP client configured neither in your app or in PHP to be used when trying to send mails.
Try to send mail "from" an address you have on your contacts. Are you sure your email account accepts these spoofed emails? Gmail sends those php emails from addresses not in your contact list to spam.
Try to test on a different email account. It's worth the shot.
Another test you can run is using an alert to retrieve the mail() return, to check whether it comes true or false..
alert(mail(hey#joe.jh, subject, message, email));
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have created a simple html form to send an email using php. Currently whenever I try to send the info I just get redirected to my process.php page with and browser error (myserver.com unable to handle this request).
I have already tried sending a test email by making my php page just the mail() function and it does indeed work so it has to do with my code somewhere. I'm sure it's something simple so here is my code.
HTML (contact.html):
<!-- Contact form -->
<form id="form" action="process.php" method="post">
<div>
<label for='name'><span class='required'></span></label>
<input id="Field1" type='text' name='name' placeholder='Type your Email Here' required/>
</div>
<div>
<label for='message'><span class='required'></span></label>
<textarea id="Field2" name='message' placeholder="Type a Message for us Here" required></textarea>
</div>
<div>
<button type='submit'>SEND MESSAGE</button>
</div>
</form>
PHP (process.php):
<?php
//if "email" variable is filled out, send email
if(isset($_POST['name']) && isset($_POST['message'])) {
//Email information
$admin_email = "test#mydomain.com";
$email = $_POST['name'];
$subject = "Email from contact form";
$comment = $_POST['message'];
//send email
if(mail($admin_email, $subject, $comment, "From:" . $email)) {
echo '<p>Success</p>';
header('Location: contact.html');
} else {
echo '<p>Error sending message</p>';
}
} else {
echo '<p>Please fully fill out the form</p>';
}
?>
You have syntax error in the PHP code you have shared - You are missing ; in the $subject = "Email from contact form" line.
Please see bellow -
<?php
//if "email" variable is filled out, send email
if (isset($_POST['name'], $_POST['message'])) {
//Email information
$admin_email = "test#mydomain.com";
$email = $_POST['name'];
$subject = "Email from contact form";
$comment = $_POST['message'];
// //send email
if(mail($admin_email, $subject, $comment, "From:" . $email)) {
echo '<p>Success</p>';
header('Location: contact.html');
} else {
echo '<p>Error sending message</p>';
}
} else {
echo '<p>Please fully fill out the form</p>';
}
?>
Check your php.ini file for mail configurations.
open php.ini file
search "mail function" there
set SMTP to your server
set sendmail_from (sending mail_ID)
Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
<?php
$errors = '';
$myemail = 'EMAIL#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
after that you can addded your futher code
Try add in top file:
error_reporting(E_ALL);
and edit your code, see:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
http://www.php.net/manual/en/function.error-reporting.php
http://www.php.net/errorfunc
http://php.net/manual/pt_BR/function.set-error-handler.php
http://www.php.net/register_shutdown_function
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
The problem is with your From field in your $headers variable.
you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.
The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.
I've already mentioned it in details here.
My contact form doesn't work. Emails are not being sent.
Webpage Code :
<!--Form Start-->
<form method="post" action="sendmail.php">
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit">
</form>
<!--Form End-->
PHP Script :
<?php
$to = "oxydyzestudios#gmail.com";
$subject = "Comment";
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if($sent) {
print "Your mail was sent successfully";
} else {
print "We encountered an error sending your mail";
}
?>
Yes, the email is correct.
Live Demo : http://unitedasone.web1337.net/form.html
Any help would be appreciated.
There's nothing wrong with your code and it reports success.
This is likely a problem with your mail set up on your server, if you're seeing Your mail was sent successfully then php's mail function has successfully passed the mail to the server for delivery.
From php.net:
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
How is mail sent on your server?
I have a really simple php contact form on one of my sites, the problem is that it won't work when sending emails to some addresses.
It works fine sending to my gmail address, but it doesn't work with iCloud (#me.com) addresses or other domain specific emails that I have set up.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemailaddress#me.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
If it didn't work at all I'd know there was a syntax error, but I get the 'Email Sent!' confirmation.
add if statement to the mail function :
if( mail("myemailaddress#me.com", $subject, $message, $from)) echo "Email sent!";
else echo "failed" ;
this way you'll know if it was sent or not .
then start checking the problem .
you man check your php.ini file :
check
sendmail_from = '';
sendmail_path = '';
and fill them with needed data ... maybe some address doesnt accept emails with no full data in the header . maybe they found it as a spam or somthing else .
Just have a look at your else part where you are using mail .Just below that is the echo so it always get executed as soon as it enters the else block.So, check for mail by if & else condition and then show the massage accordingly
I am trying to send an email through php using this:
<?php
mail("my_email", "Test Message", "welcome to the test message") or die("Error!");
?>
But when i run this in php the email doesnt come through and no error message is created and the die message does not appear anywhere.
I have got this information from http://www.php.net/manual/en/function.mail.php
What have i done wrong? I have been looking but i am unable to find out if its a problem with php or my server and everything i have followed has failed.
can someone clarify this?
----EDIT----
By the looks of it i need to do some more research in this matter, thanks for all your help and ill do some more work
Basic implementation, however, if the above doesn't work then I'm sure you need to set up a MTA
HTML Code
<form action="mail.php" method="post">
<input type="text" name="email" />
<input type="submit" value="submit mail" />
</form>
PHP Code:
if (isset($_POST['email']) && !empty($_POST['email'])) {
$userEmail = $_POST['email'];
$to = strip_tags($userEmail);
$subject = "email subject";
$message= 'email body message goes here';
$headers = "From: anotheremail#test.com";
if (mail($to,$subject,$message,$headers)) {
echo "mail sent";
} else {
echo "error sending mail";
}
}