php mailheaders vs header on website email from - php

I am new to php and stackoverflow and I am trying to figure out a simple website contact form. I have the form and email functioning properly, but I have one nagging issue I can't figure out.
When the email is sent, the from email says myusername#p3pxxxxxx.com which is my server. When I direct the email to my domain based email account, they don't get to my inbox, I bet the spam filters are stopping the odd email address. So I tried sending it to my gmail inbox, which worked, but I don't check that email regularly. I'd rather have it go to my domain based email account.
So, I am looking for a way to edit the 'from' email address. Instead of the user/server I would like it to use a real email address, mine or the person that sent it would be even better. Here are a couple of attempts and what I currently have, none of which worked.
Couple of Attempts:
Attempted to pull the email address entered my sender.
//$mailheader = "From: ".$_POST["email"]."\r\n";
//$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
This was another attempt.
//$mailheaders = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
What I currently have:
<?
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$security = $_POST['security'];
$to = "myemailaddress#gmail.com";
$subject = "Contact Message from Website";
$mailheaders = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$message = "A visitor of exampledomain.com has submitted the following message.\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage: $message";
if ($security=="10") {
mail($to,$subject,$message,$mailheaders);
header("Location:contact.php?s=1");
}
else {
header("Location:contact.php?s=2");
}
?>
Followed the suggestion. Last attempt still didn't work...
<?
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$security = $_POST['security'];
$to = "mail#example.com";
$subject = "Contact Message from Website";
$message = "A visitor of exampledomain.com has submitted the following message.\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage: $message";
$mailheaders = "From: webmaster#example.com\r\nReply-To: website#example.com";
if ($security=="10") {
mail($to,$subject,$message,$mailheaders);
header("Location:contact.php?s=1");
}
else {
header("Location:contact.php?s=2");
}
?>

Alastair's correction should work.
When you were calling the mail function, you weren't passing in the variables in the proper order.
How you were calling it: mail($to, $subject, $mailheaders, $message)
But the actual order is: mail($to, $subject, $message, $mailheaders)
Notice how I switch $message and $mailheaders
It's a thing you can check on PHP's documentation, such as:
http://us3.php.net/manual/en/function.mail.php
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Related

PHP E-mail form - Define sender

I'm trying to create a HTML form, which will in the end send an e-mail using the folling PHP Code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$destinationemail = "myemail#domain.com";
$emailcontent = "Name: {$name}\n\nE-Mail: {$email}\n\nMessage: {$subject}\n{$message}";
$subject = "Contact from Domain.com";
$from = $email;
mail($destinationemail, $subject, $emailcontent) or die("Error!");
echo "Thank you $name!";
?>
The problem is, everytime i receive an e-mail, i get is as if being sent from a what i guess is the Webhost general e-mail.
htgkaylg#server776.web-hosting.net
<htgkaylg#server776.web-hosting.net>
dom 08/10/2017, 18:40
Você;
I would like it to be received something like this:
myemail#domain.com
<myemail#domain.com>
dom 08/10/2017, 18:40
Você;
Is it possible?
Thank you,
Vítor
You have to use the Header as 4th parameter in mail() function.
Add this line and change mail as follow:
$headers = "From:" . $from . "\r\n";
mail($destinationemail, $subject, $emailcontent, $headers) or die("Error!");
Ref. https://www.w3schools.com/php/func_mail_mail.asp

Forum email address display issue

I created a web forum with the script below. I am able to receive inquiries from my site; however, the visitor's email address is not showing in the email sent from the webmaster. I would like to have some guidance here to fix the problem.
Here is my PHP script:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "abc#gmail.com";
$subject = "Contact Form";
mail($recipient, $subject, $formcontent) or die("Error!");
echo "Thank You!";
?>
Here is what I got from the webmaster:
From: xx
Message: xx
No email address listed in the email sent by the webmaster.
I found some similar scripts on the web with two additional rows:
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
This actually gives me an Error output.
How can I fix the issue?
Thanks!
The third parameter of mail() is just the actual body of the message, which you have saved as $message. The fourth variable is where you define the headers, and is where you define who the message is From. Note that you don't actually need to pass the message as a header, and as such, your variable $formcontent, shouldn't contain your From: header. However, you do need to provide carriage returns in the form of "\r\n" after the provided email address.
The modified code would look like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name" . "\r\n";
$recipient = "abc#gmail.com";
$subject = "Contact Form";
mail($recipient, $subject, $message, $formcontent) or die("Error!");
echo "Thank You!";
?>
Hope this helps! :)
You could add headers to the mail function.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "abc#gmail.com";
$subject = "Contact Form";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: <$email>" . "\r\n";
mail($recipient, $subject, $formcontent, $headers);
FYI - I would santize and validate all POST values before using them in this manner

Problems with contact form and input validation

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>';
}
}
?>

Contact form sends everything to spam

My contact form works fine but sends everything to spam when I set the recipient as Gmail account and sends nothing if I set it as my domain email client (eg info#mydomain.com). Is there something wrong in the code? What do I need to do?
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#mydomain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: message-sent.html#contact'); exit();
?>
Second question. I set up the location to redirect a user to a thank-you page. How can I set it to open in a new tab instead?

When receiving email's I only get my email address as "sent from." although it was sent from another email address. What should I add or change?

So I'm using this code for php mail and I keep getting MY email address rather than the actualy senders email, when I test it on my website's contact form. Any help? By the way, I use my email in the recipients address.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Type: $type \n Message: $message";
$recipient = "myemail#address.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Have a look at php:mail manual in the example#3 you can see,
<?php
mail('nobody#example.com', 'the subject', 'the message', null,'-fwebmaster#example.com');
?>
You can see
The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path.

Categories