I have an email form in PHP and I want to add a subject to the body of the email:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'emailo#example.com';
$subject = 'Message from Camino.bo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>
This form submits an email with the subject "Message from Camino.bo", that's ok but I want to submit in the body of the email the value the user selects from a dropdown menu:
HTML:
<select name="subject" id="email_subject">
<option value="default subject">Select a subject</option>
<option>Product A</option>
<option>Product B</option>
<option>Product C</option>
</select>
How can I do this?
PS: the subject field in my form is optional with no validation required.
Your variables are a bit mixed up. The 4th parameter of the mail() function is not 'From', it is 'Email Headers', which can be any number of header values in the correct format, each separated with a new line. The 'From' will just be one of those headers..
$to = 'you#youremailaddress.com';
$subject = 'My Email Subject';
$body = 'The body text of your email....';
$headers = "MIME-Version: 1.0\r\n".
"Content-type: text/html; charset=iso-8859-1\r\n".
"From: YOU <your#email.com>\r\n";
// and others as required
mail($to, $subject, $body, $headers);
Add your POSTed options to the $body tag as required
I'm not sure if I'm missing something, but in your HTML, you want to add values to the remainder of your options:
<select name="subject" id="email_subject">
<option value="default subject">Select a subject</option>
<option value="Product A">Product A</option>
<option value="Product B">Product B</option>
<option value="Product C">Product C</option>
</select>
Then your PHP could be as follows:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form <youremailaddress#domain.com>';
$to = 'emailo#example.com';
$subject = 'Message from Camino.bo ';
$select_subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message: $message\n Subject: $select_subject";
$headers = "From: " . $from;
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $headers)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>
Notice that I've added $select_subject and added it to your email $body variable. Also please replace 'youremailaddress#domain.com' with the FROM email address you wish to use.
Also, I don't know if you already handle this elsewhere in your code, but I think you should display a message to screen if either $errName, $errEmail or $errMessage evaluate to true. Right now, if either one of those is true (assuming the code you pasted here is complete) your end user would get nothing displayed on their screen upon form submission and they might be confused by what happened and try to resubmit.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
HTML Form with Sendemail
(2 answers)
Closed 5 years ago.
I have only dabbled in PHP a little, I am trying to remember what I did last year. I am having trouble getting this to work - the user is redirected to index.php but then nothing happens. No email is recieved and no verification 'email sent/not sent' etc.
I am sure it is probably a silly mistake.
Any help would be appreciated.
contact.html
<form method="post" action="index.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
index.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email']; // GET EMAIL ADDRESS FROM FORM
$to = 'annie.palmer#outlook.com';
$subject = 'Website enquiry from' .$name;
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: Annie<$from>\r\nReturn-path: $from" . "\r\n";
$headers .= "Content-type:text/text;charset=ISO-8859-1";
?>
<?php
if ($_POST['submit'] && ($_POST['human'] == '4') {
/* Anything that goes in here is only performed if the form is submitted */
if (mail ($to, $subject, $body, $headers, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I'm posting this as a community wiki; I don't feel any rep should come of this, nor do I want rep.
mail() uses 4 arguments, not 5.
http://php.net/manual/en/function.mail.php
There is a 5th but it doesn't do what you're expecting your 5th to do.
So remove the , $from from this:
if (mail ($to, $subject, $body, $headers, $from))
^^^^^^^
The From: belongs in the header and expects an email address.
Example from the manual:
<?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);
?>
Try using the PHPMailer. Once uploaded to your site sending an email is easy.
require ('../mail/PHPMailerAutoload.php');
require ('../mail/class.phpmailer.php');
//set up your email
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tbs'; //set as required
$mail->Host = "enter your email host here";
$mail->Port = 25;
$mail->Username = "enter your mail account username here";
$mail->Password = "enter your mail account password here";
$mail->From = "the from email address";
$mail->FromName = "the from name";
$mail->Subject = "the subject goes here";
$mail->IsHTML(true); //your choice
$body = "the body of you email - html or plain text";
$mail->Body = $body;
$mail->AddAddress("add the email address(es) of recipients");
$mail->Send();
I have a PHP email form embedded at the top of the HTML of my contact form page (index.php):
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
}
}
?>
HTML:
<form role="form" method="post" action="index.php">
...
</form>
and I wanted to hide the form only after successful submission. How can I do this?
In your CSS file;
.hide {
display:none;
}
(If unsure how to do this, this question should help)
Then inside your PHP if block;
$class = "";
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
$class = 'class="hide"';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
and finally, on index.php:
<form role="form" method="post" action="index.php" <?php echo $class ?>>
Now you can hide the entire form, with a little bit of CSS and a dynamic variable.
Edit:
If you wanted to avoid using an external or internal CSS file entirely, you could apply the CSS inline to your html element directly, like so;
$style = 'style = "display:none;"';
An alternative approach, if you're not using style sheets or any other CSS.
To hide form after submission and email success,give id to form(here its id="myForm", then use style to display:none as shown below)
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
echo "<style> #myForm{ display:none; } </style>";
} else {
$result='<div>Sorry there was an error sending your message.</div>';
echo "<style> #myForm{ display:block; } </style>";
}
}
}
?>
<form role="form" method="post" action="index.php" id="myForm">
</form>
I am using a bootstrap contact form. The form works fine but whatever I do I cannot get it to send html emails. I have added headers and css inlined to different parts of the script but it never sends the email with any html formatting. I am banging my head against the wall and would really appreciate any help. Where do I put the headers variable and where do I put the html?
Here is the php code.
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Mywebsite';
$to = 'me#email.com';
$subject = 'Contact Email ';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;
$body = " $headers, From: $name\n E-Mail: $email\n Message: $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if content has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! </div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
And here is the HTML.
<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
My Website
</div>
<div style="padding:24px; font-size:17px; background:#DCDCDC ; ">
This is a message from My website.
<br>
<br>
<br>
Login in to your account
<br>
<br>
</div>
<br>
<br>
</div>
replace mail($to, $subject, $body, $from); with mail($to, $subject, $body, $headers);
I created a simple contact form as follows.
<form method="post" action="mail_receive.php">
<label>Name *</label>
<input name="name" placeholder="Type Here">
<label>Email *</label>
<input name="email" type="email" placeholder="Type Here">
<label>Phone No *</label>
<input name="phone" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="">
And php file called mail_receive.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$from = 'From: Someone';
$to = 'admin#gmail.com';
$subject = 'Ticket Ordering';
$body = "From: $name\n E-Mail: $email\n Phone:\n $phone";
if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $phone != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>Please fill all required fields !!</p>';
}
}?>
}
And i uploaded to my hosting.
Although I can fill up the form and submit, I can't receive email to my inbox.
Is there any wrong?
With mail, it's meant to be
mail($to, $subject, $message, $headers);
Try something like this:
$to = "youremailaddress#whatever.com";
$subject = "Your tickets yo";
$headers = "From: someone#someone.com\r\n";
$headers .= "Reply-To: someone#someone.com\r\n";
$headers .= "MIME=VERSION:1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n;"
$message = "Nice tickets bro.";
mail($to, $subject, $message, $headers);
And please I hope it's just for example purposes, but please don't actually be sending it to admin#gmail.com because obviously (unless you ARE actually admin#gmail.com, you wont recieve the e-mail.
I have a very simple contact form on my site, Im looking for users to be able to just put a check mark next to "CC:" to have it CC them without creating a whole new field for that they have to fill out again.
Here is the HTML:
<form action="send.php" method="post" name="form1">
Name: <input name="name" size="35"><br/>
E-mail:<input name="email" size="35"><br/>
CC: <input input type="checkbox" name="mailcc"><br/>
Comment: <textarea cols="35" name="comment" rows="5"></textarea> <br />
<input name="Submit" type="submit" value="Submit">
</form>
And here is the PHP:
<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$comment = $_REQUEST['comment'] ;
mail( "me#me.com", "Message Title", "Name: $name\n Email: $email\n Comments: $comment\n " );
echo "Message Sent! Thanks!"
?>
Ive been trying to add some items from this site:
http://w3mentor.com/learn/php-mysql-tutorials/php-email/send-email-with-cccarbon-copy-bccblind-carbon-copy/
But it wants to create a text field for CC which means the user would have to enter their email twice.
Ive also tried $mailheader.= "Cc: " . $email ."\n"; but I cant get that to work either.
Make the checkbox have a value (value="1") in HTML.
Add a variable ($mailheader) to the end of mail() function, as the last parameter.
So essentially:
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comment = $_POST['comment'] ;
if ($_POST['mailcc'] == 1) {
$mailheader .= "CC: $name <$email>";
}
mail("me#me.com", "Message Title", "Name: $name\n Email: $email\n Comments: $comment\n ", $mailheader);
echo "Message Sent! Thanks!";
Is the Cc address you are testing with the same as the "to" address(me#me.com on your example)?
I did a quick test and with this code i get only one mail:
<?php
$to = "my#address.com";
$subject = "Testing";
$message = "Testing message";
$headers = "Cc: my#address.com";
mail($to, $subject, $message, $headers);
But with this i get a copy to my other email account:
<?php
$to = "my#address.com";
$subject = "Testing";
$message = "Testing message";
$headers = "Cc: my#otheraddress.com";
mail($to, $subject, $message, $headers);