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
Related
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
So I am mailing the contents of a form to a client and he would like the person who sent the form to be Cc'd in.
I have done some research and it appears I need to use the header code to set the from, subject and cc but my code is set up differently - please see below:
<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$recipient = "email#mydomain.com";
$subject = "More information regarding $relatedproduct";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
Would it be possible to do it like this?
<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
// Mail it
mail($to, $subject, $formcontent, $headers);
?>
php.net/mail
Yes, as you have it:
...
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthday#example.com' . "\r\n";
mail($to, $subject, $content, $headers);
?>
Incidentally, please sanitise your POST variables before you inject them (e.g. $to = $_POST['email'];).
As #Rhopercy says in their comment, perhaps an email library will help you as it takes care of most things for you. Take a look at PHPMailer or SwiftMail.
Should be like this:
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email\r\nReply-To: $email";
$headers .= 'Cc: test#test.com\r\n';
$headers .= 'Bcc: test#test.com\r\n';
$headers .= "Return-Path: <info#premierinspectiontn.com>\r\n";
$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
// Mail it
mail($to, $subject, $formcontent, $headers);
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?
I want to show images in the body of the mail in php mail script
$formcontent=" Name: $name \n Picture: (I want to show the pic here)";
$recipient = "myemail#domain.com";
$subject = "Contact us";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
I tried the following codes but it doesn't work, it shows the words as it is
$formcontent="<img src='http://anydomain.com/pic.jpg'>";
$formcontent="<html><body><img src='http://images2.alphacoders.com/444/444259.jpg'></body></html>";
tried the following codes .
$formcontent= "<img src=//anydomain.com/pic.jpg'>";
$formcontent= "<html><body><img src='//images2.alphacoders.com/444/444259.jpg'>
</body></html>";
Hi all this is the PHP im using:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Message: $message";
$recipient = "sophiec#fdb.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;
color:#ff0099;'> Return Home</a>";
?>
Its working perfectly fine but i would like there to be some text sent with this email that says "This is from your website" or something similar to tell the recipient that it isnt spam (my client isn't tech friendly and sees everything plain text as spam). I'm very new to PHP with nearly 0 knowledge and have no idea how to add something like that. I did have a go at making a new variable with the string inside and then include that in the:
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
line but with no success. Thanks for taking the time any help would be welcome.
Change this line: $formcontent=" From: $name \n Phone: $phone \n Message: $message";
Add the content you want:
$formcontent="This is from your website\n From: $name \n Phone: $phone \n Message: $message";
Or if you want to change the subject, this line: $subject = "Contact Form";
Add the content you want:
$subject = "Contact Form - From your website";
You can either add it in the $subject or $message.
$subject = "Contact Form: This is from your website"; // OR
$message .= "This is from your website";
$formcontent='This is from your website\n From: ' . $name . '\n Phone: ' . $phone . '\n Message: ' . $message;