PHP Email Form More Fields - php

So I have an email form using PHP:
<form method="post" action="contactus.php" autocomplete="off">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" maxlength="60" required/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" maxlength="120" required/>
<label for="message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message" required></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
And the PHP to send to my email:
<?php
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail.com', 'Quick Pass', $message, $email);
}
?>
But the problem is only 'myemail.com' (which is my email address), 'Quick Pass' (the email subject) and $message (the email message). $email isn't being sent.
I have tried:
<?php
$to = "myemail.com";
$subject = "Quick Pass";
$message=$_POST["message"];
$email=$_POST["email"];
mail($to,$subject,$message,$email);
?>
With this I am receiving four emails (two with $message and two with $email).
The problem is I need all of this in one email. So the $message is sent along with $email somewhere in the email.
Any ideas? (hopefully using my form)

There is an example, direct from the php manual, which looks as follows:-
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
The fourth parameter is NOT simply an email address - it is a header:value type arrangement with multiple headers separated using \r\n
In your code perhaps you could try:-
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
$headers='From: '.$email."\r\n".
'Reply-To: '.$email;
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail#example.com', 'Quick Pass', $message, $headers );
}

Make sure you getting your form input correctly. then try this.
// recipients
$to = 'to#example.com'; //your email address
$user_email = $_POST["email"]; //input from form
// subject
$subject = 'Subject';
// message
$message = $_POST["message"];
// set conent type
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: User Email <'.$user_email.'>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);

As previous posters have said, the fourth parameter id for for headers so it should be "From: {$email}" or similar.
However, mail servers are sometimes configured to ignore From headers and set a default value. Also you could find that the message is being ignored somewhere along the email route (by your host's own relay server or by your email provider's incoming mail server) if the sending address doesn't match any DNS MX records for the domain in question. You will need to talk to your hosting provider(s) to investigate this.

Related

Contact form reply to sender

So I've completed a contact form made in PHP. After testing it sends emails, but in the email header it is not capturing the sender's name or email address. It shows up as sh-908129268#eu.hosting-webspace.io
Is there a way to correct that? I need to be able to respond to emails.
The inputs are as follows:
<form id="contact" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<input class="input is-small" type="text" name="name" placeholder="Text input" value="<?= $name ?>" >
<p class="help is-danger"><?= $name_error ?></p>
<input class="input" type="email" name="email" placeholder="Email Address" value="<?= $email?>">
<p class="help is-danger"><?= $email_error?></p>
// back-end file
$sendTo = 'test#email.com';
$subject = 'Business Enquiry';
$headers = "";
$headers .= "Sent From: ".$name. "\r\n";
$headers .= "Reply To: ".$email. "\r\n";
$headers .= "Message: ".$message. "\r\n";
if (mail($sendTo, $subject, $message, $headers)) {
$success = "Thank you for your message, I will reply as soon as I can.";
$name = $email = $message = ''; // resets all fields
}
You cannot pick the names of the headers freely, you have to adhere to the standard (RFC 5322 Internet Message Format).
Your header "Sent From" should be just From, your header "Reply To" should be Reply-To and your header "Message" shouldn't be in the headers at all - you already pass the message as such into the mail function.
All fixed now, thanks. All I needed was to specify one header ,
$headers = "From: ".$email;
So it's just $subject and $headers

PHP mail function sending blank emails [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have an HTML form that posts to a PHP mail function. For some reason it does not send the content anymore. The emails display "This email has no content." Am I missing something?
//HTML - Post method form.
<form class="form" id="form" action="submitform.php" method="post">
<h2 style="padding-top: 10px;">Contact Us</h2>
<input class="input" type="text" placeholder="Name" name="name"><br>
<input class="input" type="text" placeholder="E-Mail Address" name="email"><br>
<input class="input" type="text" placeholder="Phone #" name="phone"><br>
<textarea class="input" placeholder="Questions, Specifications, etc."
name="message</textarea>
<input class="inputButton" type="submit">
</form>
//PHP - Gets posted HTML input data and sends it to the email, formatted with \n
<?php
$to = 'example#example.com' ;
$subject = 'Inquiry' ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nPhone: $phone \nMessage: $message \n";
$sent = mail ($to, $subject, $message) ;
if($sent == true) {
echo "<h3>Thank you for your inquiry.</h3>";
}else{
echo "<h3>Sorry, your message wasn't sent.</h3>;
}
?>
I've found defining headers, even if I just want default values, helps me get my mail delivered. I can't say this is what you need to do because your code looks like it should run successfully as is.
An example of setting the headers to (I believe) default values:
$to = $to_email;
$subject = $your_subject;
$message = $your_message;
$headers = "From: S.O.<no-reply#dontusethis.email>\r\n" .
"Reply-To: [can be nice, not needed of course]\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);

How to create contact form and mail to the owner of website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to get customers details through contact form and that would be sent to my mail id. so I am trying to code but its not working. I have tried by watching YouTube videos
This is my HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name: <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>
</form>
</body>
</html>
This is my PHP Code
<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk#gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
?>
mail function as described here http://php.net/manual/en/function.mail.php
Example:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
So the header "From" must be an email address while you are putting a custom name.
How to get mail function delivery mail to 'to' address please read here
It is mostly correct.
You can't pass Mobile as the additional parameter, that will do nothing.
I have changed the script to append the mobile to the message body.
To set a from address, you need to have a from email, just a name by itself won't work.
Also depending on your php configuration and mail server configuration, you may not be able to change the from address and it will result in an error, or be ignored.
<?php
if ($_POST) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$mes = $_POST['mes'];
// append mobile to message
$mes .= "\r\nMobile: $mobile\r\n";
$from_email = "imjeevajk#gmail.com';
mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");
// mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
exit;
}
Please set submit name field and update the php mail function as mentioned below.
<input type="submit" name="sendMail" value="Send Mail"></input>
<?php
if (isset($_POST['sendMail'])) {
$to = 'admin#dummyemail.com';
$subject = 'Contact From Site';
$from = "From: ".$_POST["name"]."\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message = "Name: ".$_POST["name"]."";
$message .= "Mobile: ".$_POST["mobile"]."";
$message .= "Message: ".nl2br($_POST["mes"])."";
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo 'Thanks for Contacting Us.';
} else{
echo 'Unable to send email. Please try again.';
}
}
?>
Better use PHP Mailer
some servers providers block mail function and that can be reason why it doesn't work.

FORM: Name as sender instead of e-mail.

I have a very basic form that uses PhpMail to send it's content but is there some way to change the "sender name" of the form from an e-mail adress to a name.
Example:
Instead of "from: form.ello#whatever.org" it says "From: Ello Recruitment" or whatever.
Here's the code i'm currently using:
HTML:
<form name="10_percent" action="http://www.whatever.org/recruit.php" method="post" style="text-align:center;">
<label for="description"><p>Description</p></label>
<textarea name="description" id="description" rows="4" cols="50"></textarea>
<input type="submit" id="submit" name="submit" value="Send" style="width:88px; height:24px;"/>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "xxx#whatever.org";
$subject = "Recruitment";
$epost_field = $_POST['description'];
$headers = "Content-type: text/html; charset=utf-8\r\n";
$headers = 'From: Ello Recruitment' . "\r\n" .
'Reply-To: xxx#whatever.org' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = "$description_field\n";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.whatever.org\">";
mail($to, $subject, $body, $headers);
} else {
echo "not working";
}
?>
Marcel was nearly there - you need to change the From header:
$headers = 'From: Ello Recruitment <form.ello#whatever.org>' . "\r\n" .
You may have trouble setting the From address on some servers, for example gmail doesn't let you do this arbitrarily.
I would recommend you use a library to send email from PHP as it will do all this kind for formatting for you correctly.

how to change default mailed by : address in php mail()

i have the following code
$subject = "Subject Here";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Domain Name <domain#domain.com>' . "\r\n";
$to = $email;
$body = '
My Message here
';
mail($to, $subject, $body, $headers);
and it send mail correctly but when i see details in the email in gmail ...
it shows
from Domain Name
to myemail#myemail.com
date Tue, May 25, 2010 at 12:41 PM
subject my subject here
mailed-by mars.myhostingcompany.net
while i want to show my own address in mailed by section so that it should be mydomain.com instead of mars.myhostingcompany.net
There are two types of sender (From), the MIME header sender and the envelope sender.
You send the MIME sender with in the headers in the 4th parameter of the mail() function. You are doing this fine.
The enveloper sender (the one you can send when sending email through sendmail or a sendmail compatible wrapper) with the -f flag, is set in the 5th mail() parameter, additional_parameters, in the format you'd pass it on the command line: -femail#address.tld.
So your mail function would end up looking like this:
mail($to, $subject, $body, $headers, "-fdomain#domain.com");
I take it you're on shared hosting so the reason it shows your hosts email address is because when you configure PHP there is a setting called "sendmail_from" which is a default address to send mail through in the event that no address is provided in your code.
You appear to be specifying the proper headers in your code so I can only think of one possibility (that I can't test from this computer). Try removing the < > around your email address - it may be trying to read that as HTML and so you have nothing. This can occur on Windows machines because PHP itself parses the custom headers and not the MTA (message transfer agent) and PHP treats any < > as HTML.
I realize that it doesn't look as professional (since the email client won't show the name when it receives the email) but if you're running from a Windows machine there's very little else you can do unless you switch to an alternative mail package.
$subject = "Subject Here";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: domain#domain.com' . "\r\n";
$to = $email;
$body = '
My Message here
';
mail($to, $subject, $body, $headers);
//FORM PAGE:
<form method="POST" action="mailer.php">
<p>Please feel free to contact me on the form below or my direct email address: jkench#jasonkench.co.uk<br>
<br><br>
<br>
<br>
<br>
</p>
<table width="327" border="0">
<tr>
<td width="102">Name:</td>
<td width="215"><input type="text" name="name" size="19"></td>
</tr>
<tr>
<td>Company:
<label for="company"></label></td>
<td><input type="text" name="company"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" size="19"></td>
</tr>
<tr>
<td>Telephone No:
<label for="telephone"></label></td>
<td><input type="text" name="telephone"></td>
</tr>
</table>
<p><br>
Enquiry:<br>
<textarea rows="9" name="message" cols="65"></textarea>
<br>
<br>
<input type="submit" value="Submit" name="submit">
</p>
</form>
//PHP MAILER PAGE
<?php
if(isset($_POST['submit'])) {
//SEND TO
// Send the completed form to the below email address:
$to = "myemail#mydomain.co.uk";
//SUBJECT
// Subject of the email form:
$subject = "Jason Kench - Web Developer";
//NAME
//POST the details entered into the name box
$name = $_POST['name'];
//COMPANY NAME
//
$company = $_POST['company'];
//EMAIL
//
$email = $_POST['email'];
//TELEPHONE NUMBER
//
$telephone = $_POST['telephone'];
//MESSAGE/ENQUIRY
$message = $_POST['message'];
//Headers from a online site may help not sure
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//FROM EMAIL ADDRESS:
// Additional headers to change the FROM EMAIL ADDRESS
$headers .= 'From: Web-Contact-Form#mydomain.co.uk' . "\r\n";
// BODY
// This is the body of the message that will be sent to my email address with their details.
$body = "
You have received a message from the online contact form at http://www.jasonkench.co.uk\n
Details Below: \n \n
From: $name\n
Company: $company\n
$headers
Email Address: $email\n
Telephone No: $telephone\n
Message: $message\n";
// FORM SENT
// This will alert the customer the form has been successfully sent.
echo "Your details have been sent, I will contact you within 48 hours.";
// Use the mail function to email the following variables to my $to email address.
mail($to, $subject, $body, $headers);
} else {
// Display error message if there is a problem.
echo "Sorry there seems to be a problem. Please email me direct at: $to thank you.";
}
?>

Categories