I don't know why but I don't manage to get any email after html form is submitted.
I can see the data in the DB but I'm not receiving any Emails.
I put some code after the sending that work just fine. Only the mails are not sending.
Please help me.
The PHP file:
<?php
$Name = $_POST['Name'];
$Phone= $_POST['Phone'];
$Email= $_POST['Email'];
$Subject= $_POST['Subject'];
$Message= $_POST['message'];
$conn = mysql_connect("MyServerName", "userName", "password");
mysql_select_db("Mydbname",$conn);
$sql = "INSERT INTO FC(Name, Phone, Email, Subject, Message)
VALUES ('$Name', '$Phone', '$Email', '$Subject', '$Message')";
if (mysql_query($sql, $conn)) {
$to = "myEmail#test.com"; // this is your Email address
$from = "myEmail#test.com"; // this is the sender's Email address
$headers = "From: " . $Email;
$subject = "Form submission - " . $Subject;
$message = $Name . " wrote the following:" . "\n\n" . $Message . "\n\n" . "His Phone number is: " . $Phone;
mail($to,$subject,$message,$headers);
echo '<html>
<head>
<meta http-equiv="refresh" content="0.1;url=http://www.google.com" />
</head>
<body>';
echo '<script language="javascript">';
echo 'alert("message successfully sent! we will contact you shortly.")';
echo '</script>';
echo '</body></html>';
} else {
echo "something went wrong";
}
?>
Start by checking the return value of mail to see if the message is being accepted by your SMTP server.
Please make sure that mail() is not blocked in your php.ini file.
If your mail function returns FALSE then you would KNOW there's a problem with your mail configuration. If it returns TRUE then there could still be something wrong with your mail configuration. You would need to check the mail log in either case to see if you are getting any errors.
The kicker is that php may be handing off the mail, your server may be sending off the mail, and it may get spam filtered along the way.
You could try explicitly setting the From address using the -f parameter, as explained in example #3 in the php.net manual page for mail:
<?php
mail('nobody#example.com', 'the subject', 'the message', null,
'-fwebmaster#example.com');
?>
So for you, your From address could be invalid based on your hosting specifications but if its not:
$from = "-fmyEmail#test.com";
mail($to,$subject,$message,$headers,$from);
Related
I am trying to send email from a web page hosted on a shared platform over at fasthosts. I cannot for the life of me get this to work, I had a much more extensive script which checked the validity of email etc, but now I've been reduced to using the basic example from fasthosts and it is still not working.
Please could someone take a look and let me now where I am going wrong...
<?php
// You only need to modify the following two lines of code to customise your form to mail script.
$email_to = "contact#mywebsite.co.uk"; // Specify the email address you want to send the mail to.
$email_subject = "Feedback from website"; // Set the subject of your email.
// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("contact#mywebsite", $email_from);
// Get the details the user entered into the form
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The \r\n is the code to use a new line.
$headers = "From: " . $email_from . "\r\n";
$headers .= "Reply-To: " . $email_from . "\r\n"; // (You can change the reply email address here if you want to.)
// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "\r\nEmail: " . $email . "\r\nMessage: " . $message ;
// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);
// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$name .' Thank you for contacting us.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}
?>
I have a piece of code that will not work i don't understand why, i am trying to get the information sent to the database and also sent to the email address, it is saving it to the database but will not send to the email, even though it echoes your email was successfully sent. Any help appreciated.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$to = "edonaghy--#hotmail.co.uk";
$subject = "Dress fitting";
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$headers = "From: $email";
$sent = mail($to, $date, $headers);
if($sent)
{
print "Your mail was sent successfully";
}
else
{
print "We encountered an error sending your mail";
}
mysql_select_db("lr", $con);
mail("edonaghy--#hotmail.co.uk", $time, $date, $place, $comments);
$sql="INSERT INTO fitting (time, date, place, comments) VALUES ('$_POST[time]','$_POST[date]','$_POST[place]','$_POST[comments]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Information has been sent!";
?>
First of all, it wont send your e-mail if your localhost werent properly configured, so I suggest you to test on your server if you have one.
There are libraries that can help you with a better code like php-simple-mail
Example:
require 'class.simple_mail.php';
$mailer = new SimpleMail();
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$message = "<strong>My message on date:".$date." ".$time."</strong>";
$send = $mailer->setTo('edonaghy--#hotmail.co.uk', 'Your Email')
->setSubject('Test Message')
->setFrom('no-reply#domain.com', 'Domain.com')
->addMailHeader('Reply-To', 'no-reply#domain.com', 'Domain.com')
->addMailHeader('Cc', 'bill#example.com', 'Bill Gates')
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
echo ($send) ? 'Email sent successfully' : 'Could not send email';
You're using mail wrong.
It's meant to be used like this:
mail(to,subject,message,headers,parameters)
So yours would be:
$to = "email#hotmail.co.uk";
$subject = "Dress fitting";
$headers = "From: email#hotmail.co.uk";
$message = "Time:".$_REQUEST['time']."\r\n";
$message .= "Date:".$_REQUEST['date']."\r\n";
mail($to,$subject,$message,$headers);
This is assuming your $_REQUEST's are working.
Your second mail() is using variables that you haven't even set yet ($comments, $place) etc.
Set them first:
$place = $_POST['place'];
$comments = $_POST['comments'];
I have the following code in a PHP file.
$to = $row['EmailID'];
$subject = "XYZ";
$message = "yes!!!!";
$from = "pallav123goyal#gmail.com";
if(!mail($to,$subject,$message,"From:" . $from))
{
echo "Confirmation E-mail couldn't be sent to " . $row['EmailID'] . "<br>";
}
else
{
echo "Confirmation E-mail sent to " . $row['EmailID'] . "<br>";
}
}
On running the code, the else part of the above code is executed as evident from its output. But no e-mail gets sent to $row['EmailID'] (which is a valid email ID) What could be the error?
Try this syntax
<?php
$to =$row['EmailID'];
$subject ="XYZ";
$txt ="yes!!!!";
$headers = "From: pallav123goyal#gmail.com";
mail($to,$subject,$txt,$headers);
?>
It is most likely a problem with your sendmail (assuming you are running the code on a linux system) configuration and not with the code per se. Try to use PEAR mail and specify an external SMTP server (e.g. gmail).
I know I'm being sent a status of '1' from this process file as my JavaScript resulting is functioning. Problem is that I'm not getting the email.
<?php
//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'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//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 name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your comment.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'myemail#gmail.com';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Comment from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
<tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
//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;
}
?>
You mentioned you are using GoDaddy. GoDaddy requires you set the sender address legitimately to match the domain of the site it is sending from or use SMTP with Authentication.
There is a huge gaping hole with this method of sending email. Spammers can easily override the From: header by inserting additional recipients.
I'm not sure how mail centric your application plans to be, but I would recommend using a package like PHPMailer or PEAR::Mail as it takes care of email handling for you at a much higher level. This let's you focus on more important parts of your application. The built-in PHP mail() feature is very limited in its abilities and as you try to extend your mail capabilities you'll run into many road blocks that the base mail() function just cannot handle without a lot of additional logic on your behalf (attachments, MIME-types, etc come to mind).
when testing mails you can test it directly to your server, php mail has a function that already runs on it. if you test it on xampp locally it will not send , unless you have set the php mailer in localhost. but for me its better to test it on server than in localhost.
How do I send an email to the user with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form.
Here is my current php code. It currently just shows them a message that says there name and that the message has been sent and then sends me an email to my email address.
<?php
if(isset($_POST['submit'])){
$to = "benlevygraphics#gmail.com";
$headers = "From: " . $_POST['email'];
$subject = "Ben, you have been contacted...";
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
echo("<p class=contactformsent>".$_POST['name'].", your message has been sent!</p>");
}
else{
echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
}
}
?>
I am new to php and I have read stuff online and I still don't understand so if you could be clear in your examples or help I would greatly appreciate it very much. Thanks!
Assuming your current code is already working fine, you can do this to send yourself an email together with the recipient:
Set $to to $_POST['email']
Set $headers to "From: {$_POST['email']}\r\nBcc: benlevygraphics#gmail.com"
Adjust $body and $subject to your needs.
Btw, I can't say this often enough; make sure that your page has some form of CSRF protection.
How to properly add CSRF token using PHP
The above is just one way, there are others, just search for it :)
Look into your php.ini beacuse you have to enter a SMTP Server.
At my file it begins in line 1087 with "[mail function]"