PHP email formatted content - php

I am creating an email form for my website which will allow a user to create an email and appear to be sent from any email address they wish. i.e. davidcameron#downingstreet.com.
The form works fine, until I made one small change. I previously allowed the user to submit the content of the email but was finding difficulty in formatting the content (i.e. making areas bold, using a table etc).
(Form code now)
<html>
<body>
<form method="GET" action="send.php">
<p>To: <input type="text" name="to" /></p>
<p>From-Name: <input type="text" name="name" /></p>
<p>From-Email: <input type="text" name="from" /></p>
<p>Subject: <input type="text" name="subject" /></p>
<input type="submit" value="Send E-Mail" ></p>
</form>
</body>
</html>
So I thought it would be easier to submit the content myself in my 'send.php' code that actually sends the email message. This is displayed below:
<html>
<body>
<?php
$to =$_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name =$_REQUEST['name'];
$from = $_REQUEST['from'];
$content = ?><font face="arial"><b>Your question has been received/b>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</font>
<?
$header="From: $from"."<$sender_email>\r\n";
mail($to,$subject,$content,$header);
echo 'sent successfully';
?>
</body>
</html>
However, when I click submit, my server finds an error. I can only presume it is because of my HTML element as this error was not occurring previously without it.
Can anyone advise how I can fix this / if you can suggest an easier way for me to format the email content I would like?
Many thanks

Set your content value to
$content = ''
Since your $content is undefined that is why you are getting error.
Hope this will fix your issue.

Try this
<?php
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name = $_REQUEST['name'];
$from = $_REQUEST['from'];
$content =
'<span style="font-family: arial"><b>Your question has been received</b>
<br>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</span>';
$header = "From:".$from;
$result = mail($to,$subject,$content,$header);
if(isset($result))
{
echo 'sent successfully';
}
else
{
echo 'Failed';
}
?>
Note: <font> tag not supported in HTML5
EDIT 01
Include this lines too
$header .= 'MIME-Version: 1.0';
$header .= 'Content-Type: text/html; charset="ISO-8859-1';

Related

submit button downloading PHP file instead of sent data to email in contact form [duplicate]

This question already has an answer here:
php file automatically downloads instead of displaying in browser?
(1 answer)
Closed 2 years ago.
I'm trying to make a contact form for my website but when I press submit, the php file is downloaded instead of being run. I am using chrome. I think there's a syntax error but I've messed around with removing, adding and stuff and even when there is no syntax errors, it still downloads the file rather than runs it And, yes...it is the exact name of the php file.
Html code
<div class="form1" id="form" data-aos="fade-down" data-aos-once="true" data-aos-delay="200">
<div class="row">
<h2>We're happy to hear from you !</h2>
</div>
<form class="form" method="POST" action="form.php">
<div class="contact-us">
<h2>Contact Us !</h2>
</div><hr>
<div class="parent">
<div class = "Name">
<label for = "fname">First Name:</label>
<input type = "text" maxlength="15" name = "fname" placeholder="First name" required>
</div>
<div class = "Name">
<label for = "lname">Last Name:</label>
<input type = "text" maxlength="20" name = "lname" placeholder="Second name" required>
</div>
</div>
<div class = "email">
<label for = "email">Email:<br></label>
<input type = "email" minlength="8" name = "email" placeholder="Your email" required>
</div>
<p class = "comment">
<label for="message">Comments:<br></label>
<textarea id="message" name="message" cols="10" rows="5" placeholder="Your Message"></textarea>
</p>
<div class="submit">
<input type="submit" class="submit-btn" value="Send Us" >
</div>
</form>
php code
<?php
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];// Get fName value from HTML Form
$lname = $_POST['lname']; // Get lName value from HTML Form
$email_id = $_POST['email']; // Get Email Value
$msg = $_POST['message']; // Get Message Value
$to = "...#gmail.com"; // You can change here your Email
$subject = "'$name' has been sent a mail"; // This is your subject
// HTML Message Starts here
$message ="
<html>
<body>
<table style='width:600px;'>
<tbody>
<tr>
<td style='width:150px'><strong>FName: </strong></td>
<td style='width:400px'>$fname</td>
</tr>
<tr>
<td style='width:150px'><strong>LName: </strong></td>
<td style='width:400px'>$lname</td>
</tr>
<tr>
<td style='width:150px'><strong>Email ID: </strong></td>
<td style='width:400px'>$email_id</td>
</tr>
<tr>
<td style='width:150px'><strong>Message: </strong></td>
<td style='width:400px'>$msg</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: Admin <...#gmail.com>' . "\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
}
else{
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
?>
If you can add this to your apache config file
AddType application/x-httpd-php .php
That will sort out your problem
HOW TO - https://www.phusionpassenger.com/library/install/apache/working_with_the_apache_config_file.html
Also make sure you are running the code on a server environment like wamp or xamp. If you are working offline.

Mail sending is failure in php contact us form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am creating a contact us form, In that i am using HTML and PHP .Now my doubt is after click the submit button the details are not sending to mail,I want to know where i am mistaken on my code.
Here my HTML code for create the form
<form action="sendmail.php" method="post" >
<table width="100%" border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<td width="10%">Subject</td>
<td width="2%">: </td>
<td width="82%"><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td width="2%">: </td>
<td width="82%"><textarea name="detail" cols="50" rows="4"id="detail"></textarea></td>
</tr>
<tr>
<td>Email</td>
<td>: </td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>Name</td>
<td>: </td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Submit">
<input type="reset" name="submit" value="Reset">
</td>
</tr>
</table>
</form>
PHP code is below:
<?php
$subject="$subject";
$message="$detail";
$mail_from="customer_mail";
//From
$header="from: $name <$mail_from>";
//Enter your email address
$to="simon#abcinfomedia.in";
$sendmail=mail($to,$subject,$message,$header);
//check mail send to ur mail
if($sendmail){
echo"success";
}
else{
echo"Error";
}
?>
please try this:
<?php
if(isset($_POST['send']) && !empty($_POST['send'])) {
$subject = $_POST['subject'];//if you sent by post method else put manually
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
//Enter your email address
$to = "simon#abcinfomedia.in";
$sendmail = mail($to, $subject, $message, $header);
//check mail send to ur mail
if ($sendmail) {
echo "success";
} else {
echo "Error";
}
}
?>
This is not working because you're not filling the variables with the data from the form by using $_POST[''].
The HTML doesn't change, but try this PHP:
<?php
$subject = $_POST['subject'];
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$to = "simon#abcinfomedia.in";
//check mail send to ur mail
if(mail($to,$subject,$message,$header)){
echo"success";
}
else{
echo"Error";
}
?>
I have defined subject, message and mail_from using the $_POST data from the form. For the if statement, I have dropped the part where you store the mail() function as a variable and just put it straight into the if.
This will simultaneously send the email and check it at the same time.
In my test, the email worked but there were flaws because some of your fields are missing and not defined, but I believe the above solves your initial problem. :)

How to send response email to user on form submit PHP

I have a rather basic php form that submits on the same page it is called "registration.php". I have it set to collect the form data, store it in the $to, $subject and $body variables then I call the mail() method to send off an email to myself.
please see code below ( Don't worry! there is a question comin' right up! ):
<?php
ob_start(); //output buffering because I like it.
if (isset($_POST['submit'])) {
// Process the form
$message = "Thank you for registering! We will respond to your request shortly";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$comments = $_POST['userComment'];
$date = gmdate("M d Y");
$to = "myemail#outlook.com";
$subject = "Registration Submission";
$body = " Date: $date \n Registrant Name/Name's: $name \n Registrant E-mail: $email \n \n User Comments: \n $comments \n \n";
mail($to,$subject,$body);
}
?>
<form id="contactForm" name="contactForm" action="registration.php" method="post">
<table>
<tr>
<td><label for="name"><strong>Registrant Name / Names</strong></label></td>
<td><input required='required' type="text" id="name" name="name" /></td>
</tr>
<tr>
<td><label for="email"><strong>Registrant E-mail</strong></label></td>
<td><input required='required' type="email" maxlength="40" id="email" name="email" /></td>
</tr>
<tr>
<td><label for="subject"><strong>Registrant Address</strong></label></td>
<td><input required='required' type="text" maxlength="100" id="address" name="address" /></td>
</tr>
<tr>
<td>Message:</td>
<td> </td>
</tr>
<tr>
<td id="textAreaInput" colspan="2"><textarea rows="3" id="userComment" name="userComment" placeholder="Let us know your thoughts!"></textarea></td>
</tr>
</table>
<br />
<button type="submit" name="submit" id="submit">Submit</button>
</form>
What I want to know is Can I send a custom response email to the users collected $email with a custom message using just php? can I send other variables through the mail() method? like $to2, $subject2, $body2 or must they be named $to, $subject & $body when passed?
I have a working solution using PHPMailer found here:
https://github.com/PHPMailer/PHPMailer
I am just curious as to weither or not there is an easy process using PHP that wouldn't rely on a required library.
I think all you have to do is
if(mail($to,$subject,$body)){
mail($email,"Thanks!!","Thank you very much for registering!!!")
}

sending email upon hitting submit in html or php

I have a simple site with 3 input forms: a first name, last name, and email address fields for the user to fill out. I want them to be able to fill those 3, and when hitting submit, an email to be generated with some autofilled text, for example:
email: what the user put as their email
the subject will be something I make, like, "regarding your _"
and the body of the email to use their first and last name they entered like so:
Dear __ __,
click here to fill out this bla bla bla etc
How would I go about doing this?
any helpful links or tutorials would be fantastic
you can make a simple html and process it by php. You can do like this :
index.php:
<form action='sendmail.php' method='post'>
<table>
<tr>
<td>Firstname</td>
<td><input name="firstName" /></td>
</tr>
<tr>
<td>LastName</td>
<td><input name="firstName" /></td>
</tr>
<tr>
<td>Lastname</td>
<td><input name="lastName" /></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" /></td>
</tr>
<tr>
<td colspan="2"><input type='submit' value='send' /></td>
</tr>
</table>
</form>
sendmail.php:
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
//build email content
$emailSubject = "Email subject";
$mailContent = "Regarding ".$firstName." ".$lastName;
$mailContent .= "more content here";
//send mail
if(mail($email,$emailSubject,$mailContent))
echo "mail send successful";
else
echo "mail send fail";
NOTICE: I suppose that all the input param is valid for send mail. You should add more validation for input field.
I would not use the basic mail function within PHP as it does not protect you against header inject (open relay etc).
Take a look at one of these:
- http://framework.zend.com/manual/2.0/en/modules/zend.mail.message.html
- http://symfony.com/doc/2.0/cookbook/email/email.html
NB: They can be used outside of the MVC framework, i.e just as a mail library
This short code for this...i hope its clear to you and will help to you, there is a lot of tutorials ......just try to search in google
foreach($_POST as $field_name=>$field_val)
{
$message = $message. $field_name . " : $field_val \n";
}
//email code
$headers = "From: Feedback <".$email.">";
mail($email_to, $subject, $message, $headers);

Form with multiple email fields to multiple recipients

This is my html form. The user will input the email addresses he/she would like to send the html email to.
<form id="form1" name="form1" method="post" action="">
<table width="400">
<tr>
<td>Please enter your email address:</td>
<td<input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td>Please enter the email addresses you would like to notify below:</td>
<td>
</td>
</tr>
<tr>
<td>Email:</td>
<td>
<input type="text" name="email1" id="email1" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email2" id="email2" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email3" id="email3" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email4" id="email4" />
</td>
</tr>
</table>
</form>
This is somewhat the php code.
<?php
$ToEmail = '["email1"]["email2"]["email3"]["email4"]';
$EmailSubject = 'Check this out guys!';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: "noreply#domain.com"\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail(......) or die ("Failure");
?>
<script type="text/javascript">
alert("Success! You have sent the notification to the emails you have entered.");
<!--
window.location = "form.html"
//-->
</script>
How do I:
1. Modify the PHP code so that it will send to the emails inputed by the user?
2. The body message of the notification is a html email. How do I go about adding it to the PHP code?
Your help is highly appreciated. Thanks in advance!
Looks like you need to $_POST the email1, email2 etc. values to a variable then use that as your value for $to in the mail() function - just make sure you add a comma after each:
$to = $_POST['email1'] . ', ';
$to .= $_POST['email2'] . ', ';
$to .= $_POST['email3'];
etc. Leave off the comma for the last email and you should be ready to go.
Regarding the content of your email, you should be able to send html no problem - just store it in a variable for ease of use later, e.g:
$message = '
<html>
<head>
<title>This is the HTML Email</title>
</head>
<body>
<div id="container">
<p>Welcome to the html!</p>
<img src="../img/some_image.jpg" alt="some image"/>
</div>
</body>
</html>
';
then make sure you add the relevant HTML headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Along with any other headers e.g.:
$headers .= 'From: HTML Email <you#example.com>' . "\r\n";
Then call mail() with your defined variables:
mail($to, $subject, $message, $headers);
Hope that helps.
p.s. its all available on the mail function definition: mail()
Look at the php manual for mail
Example:
$toemails = "user#example.com, anotheruser#example.com";

Categories