I have a rather simple HTML/PHP form that just needs to send the data to my email. The email I'm using is not using the same domain as the website.
I've been stuck on this for hours now and I cant seem to find the solution. Could someone take a look?
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email"];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
} else {
echo 'Something went wrong, go back and try again!';
}
}
?>
Assuming that your PHP code file is sendMail.php
let this be form.html
<form name="sendMail" id="sendMail" action="sendMail.php" method="post">
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<input type="text" name="message" />
<input type="text" name="gender" />
<input type="text" name="user_name" />
<input type="text" name="email" />
<input type="submit" name="submit" />
</form>
Basically, your PHP code should work according to the form above. Of course you may want to change gender field to radiogroup/dropdown and/or message to textarea.
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
}
else {
echo 'Something went wrong, go back and try again!';
}
}
?>
There is one point to correct that $email = $_POST['email"]; should be $email = $_POST['email'];.
And you should ensure that PHP mail settings have to be set properly. I suggest you to use PHPMailer which is so simple and runs smoothly with too few configuration.
Just write isset() in first if condition.
try above code again.
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email"];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
} else {
echo 'Something went wrong, go back and try again!';
}
}
The PHP
The fourth parameter of the mail() function should be a list of additional email headers. You're passing it a user name. You should have something like this:
$email = $_POST['email'];
$email = preg_replace('/[[:cntrl:]]/', '', $email);
$headers = "From: $email\r\n";
...
mail ($to, $subject, $body, $headers);
The second line filters out control characters from the email address. Without this, a malicious user could insert newline characters to add their own email headers, such as CC: headers to send unsolicited spam.
Mixing domains
Also, you might not be allowed to send email with a From: address from a different domain name than your web site (or mail server). You could contact the administrator of your server/web site and ask what your options are.
Some things to consider:
You could try to use a local From: address and leave the actual (external) email address in the Reply-To: header. Eg:
$headers = "From: Me#mydomain.example\r\n";
$headers .= "Reply-To: Someone#otherdomain.example\r\n";
Some email clients might not respect the Reply-To: header, though.
If the address in the From: header is not the actual sender of the email, you should specify the real sender in a Sender: header. Eg:
$headers = "From: Someone#otherdomain.example\r\n";
$headers .= "Sender: Me#mydomain.example\r\n";
You may need to specify the From: address in an additional parameter to the Mail Transfer Agent:
mail ($to, $subject, $body, $headers, "-f '$email'");
Sending email from a different domain name may count against you by SPAM filters. You may need an SPF record to ensure that your emails go through.
You might be better off using a full-featured email class, such as PHPMailer rather than the crude mail() function.
See also
Documentation
RFC 5322 - Internet Message Format: 3.6.2. Originator Fields
The PHP mail() function
SPF - Sender Policy Framework
Stack Overflow
Should I use the Reply-To header when sending emails as a service to others?
How do you make sure email you send programmatically is not automatically marked as spam?
Potential issues using member's "from" address and the "sender" header
Related
I am trying to adjust my code to able to reply to sender email from PHP contact form. please check my code below to give advise. Thank you
<?php
$marke = $_POST['marke'];
$modell = $_POST['modell'];
$name = $_POST['name'];
$adresse = $_POST['adresse'];
$telefon = $_POST['telefon'];
$email = $_POST['email'];
$to = 'myemail#gmail.com';
$from = 'myemail#gmail.com';
$subject = 'Contact Form';
$body = "marke: $marke\n modell: $modell\n name: $name\n adresse: $adresse\n
email: $email\n telefon: $telefon\n";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.website.com/sent.php");
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
First make headers
$headers = "From: $from\r\nReply-to: $email";
Than fix calling of mail function to be
mail ($to, $subject, $body, $headers)
Didn't tried it from times when it was PHP 4 but it will probably work as you expected...
Addition:
I just checked on php.net... go to this url http://php.net/manual/en/function.mail.php and check "Example #2 Sending mail with extra headers."
Ok so I am trying to create a contact form that validates user input to keep hackers from submitting codes and trying to require number, text, and email only. I have already styled the form and imported my php file. The contact from will send it to my gmail account. but everytime I test the php It allows for any type of data to be entered no matter if it is supposed to be a number and letters are submitted and the other way around. If I could get some help in telling me where I went wrong that would be great. I am a beginner at programming and only have the knowledge I recieved from school but I'm pretty good at html and css but having problems with the php validation. The form sends the email but like I said it allows any and all input.
<?php
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: www.webdesignheros.com';
$to = 'heenanwrk#gmail.com';
$subject = 'Service Email for HeenanTech';
$tel = filter_input(INPUT_POST, 'tel', FILTER_SANITIZE_INT);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
?>
<?php
if ($_POST['submit']){
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>
Additionally the form can be found at [http://webdesignheros.com/Contact.html][1]
[1]: http://webdesignheros.com/Contact.html and if someone could tell me how to reject certain input before the submit that would be awesome too. like if an invalid entry was input and they move on to the next input it would reject it and not let the submit button be pushed. would i use the pattern="a-z" in the html or would i need to add javascript for that?
<?php
if (isset($_POST["submit")){
$name = $_POST["name"];
$tel = $_POST["tel"];
$email = $_POST["email"];
$message = $_POST["message"];
$from = "From: www.webdesignheros.com";
$to = "heenanwrk#gmail.com";
$subject = "Service Email for HeenanTech";
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
mail($to, $subject, $body, $from);
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>
I made a PHP Contact Form using this tutorial and it works great, but I've encountered one potential security risk / inconvenience. Each email I receive comes from my admin login name.
I added $headers as this thread instructed, but to no avail.
My Current PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myClientsEmail#gmail.com';
$subject = 'Estimate Contact Form';
$headers = "From: $email\r\n"; /* I added this */
$headers .= "Reply-To: $email\r\n"; /* and this */
$body = "From: $name\n Phone: $phone\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
What exactly am I missing? Any help is greatly appreciated. Thank you!
Your mail() function call has an extra parameter it looks like. The correct mail() call should be:
if (mail($to, $subject,$body,$headers)) {
....
}
So just remove the $from portion and it should be good.
I am having a problem with my contact form and the php mail() function. For some reason, they work for every email address (#gmail, #yahoo, #outlook and even #facebook!) except the old dreaded hotmail. I am just curious as to where my code is missing something. I have checked the mail servers and there is apparently no issue with hotmail addresses.
The email does not even get delivered to the spam/junk folder (it does not reach hotmail). I had a look online and some say to change the headers to avoid being caught in the spam filter. Any pointers to this?
PHP CODE
<?php
header('Content-Type: application/json charset=utf-8');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'companyname#mail.com';
$to = 'myemail#hotmail.com';
$subject = $name . ' has sent you a message';
$human = $_POST['antispam'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['name']) && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '{"status":"1"}';
} else {
echo '{"status":"0"}';
}
}
else
{
echo '{"status":"2"}';
}
?>
The if statements is just a check if all forms are valid and the anti spam (2+2) is correctly entered. nothing much to do in this part. The issue I guess is somewhere in the header
Try using these changes:
<?php
header('Content-Type: application/json; charset=utf-8');
...
$from = 'companyname#mail.com';
$headers = 'From: '. $from. "\r\n";
...
if (mail ($to, $subject, $body, $headers)) {
...
?>
4th parameter of mail function is expected to be additional_headers, not just from address.
I have been trying to get a php contact form working on my portfolio site (currently on a free megabyet.net account), but on testing it(on the uploaded site) even though i get the thankyou/confirmation message, I still don't receive any message on my mail account (specified in the code), I can't seem to understand the problem here....help needed!
can it be something related to SMTP??
Here's the code :
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Mail has been sent, thankyou!";
mail($to, $subject, $body, $headers);
} else {
echo "blarg!";
}
?>
HTML Code:
<form id="contact_frm" action="mail.php" method="POST">
<h4>Name :</h4>
<input type="text" id="f_name" name="name"/><br/><br/>
<h4>E-Mail Address :</h4>
<input type="text" id="f_email" name="email"/><br/><br/>
<h4>Message :</h4>
<textarea id="f_msg" name="message" cols="22" rows="5"/></textarea><br/><br/>
<input id="send_btn" type="submit" value="Send >>" name="submit" /><br/>
</form>
Firstly you should be checking if mail() returns true or not to determine if mail has been sent successfully:
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if ($success) {
echo "Mail has been sent, thankyou!";
// redirect to thank you page here
}
else {
echo "message failed";
}
} else {
echo "blarg!";
}
?>
Try that and let us know if that works.
Also, have you tried sending to a different email address? It may be that Yahoo is blocking that web host for spam. Being a free host it is a very likely scenario.
If you are looking for something related to sending email via SMTP. I would recommend you use Code Igniters mailer class.
http://codeigniter.com/user_guide/libraries/email.html
This also allows for debugging and handling SMTP errors gracefully.
can it be something related to SMTP??
Probably. Why don't you check your mailq and the log files from your MTA?
#John .. checked with that if condition with the code below and i get a failed output =/ ...so my mail() function is returning false =( ...and yea i've tried gmail but with the mail function not running fine on the first place.... it doesn't work...
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if($success) {
echo "Mail has been sent, thankyou!";
} else {
echo "message sending failed!";
}
} else {
echo "blarg!";
}
?>
output- message sending failed!
so, do I need to define some extra params here?...also i saw that my host has given the path to sendmail as -- /usr/sbin/sendmail does it has anything to do with my mail function acting bad?...i mean do I need to define the sendmail param in it?
#unknown- hmm codeigniter may help, but i've never used it before...let's see...
#symcbean- sorry i don't know how to do that :P...probabaly cuz i'm not very well versed with SMTP yet?.... still a learner/beginner...
If the E-Mail goes out correctly, but never arrives, it could be that it gets caught by a spam filter. A few bullet points I wrote in reply to an similar question a few months ago:
Does the sender address ("From") belong to a domain on your server? If not, make it so.
Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
If you have access to log files, check those, of course, as suggested above.
Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.