Send Contact Form in PHP - php

I am trying to send the order form of my website in email. it is working fine but the problem is that i receive the whole information in just one single line. I want to have a line break after every field. my code is:
<?php
($_POST["email"]<>'') {
$ToEmail = 'info#mysite.com';
$EmailSubject = 'Website order form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Adress: ".nl2br($_POST["address"])."";
$MESSAGE_BODY .= "product: ".nl2br($_POST["product"])."";
$MESSAGE_BODY .= "phone: ".nl2br($_POST["phone"])."";
$MESSAGE_BODY .= "quantity: ".nl2br($_POST["quantity"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
exit;
?>
Kindly guide me where i am wrong. Thanks in advance

You need to do use \r\n if you use Content-type : text/plain
Or <br/> if you use Html
<?php
($_POST["email"]<>'') {
$ToEmail = 'info#mysite.com';
$EmailSubject = 'Website order form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Adress: ".nl2br($_POST["address"])."\r\n";
$MESSAGE_BODY .= "product: ".nl2br($_POST["product"])."\r\n";
$MESSAGE_BODY .= "phone: ".nl2br($_POST["phone"])."\r\n";
$MESSAGE_BODY .= "quantity: ".nl2br($_POST["quantity"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
exit;
?>

Related

contact form php display on email format query

This is PHP code.
When email displays on email-id the content should be on next line.
$ToEmail = 'abc#gmail.com';
$EmailSubject = 'Exza Contact Form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name :".$_POST["name"]."\n";
$MESSAGE_BODY .= "Email :".$_POST["email"]."\n";
$MESSAGE_BODY .= "Subject : ".$_POST["subject"]."\n";
$MESSAGE_BODY .= "Message : ".nl2br($_POST["message"])."\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
Example:
Name : Abc
Email : abcd#gmail.com
Subject : Any
Message : All the work
Just use \r\n instead of \n.
$ToEmail = 'abc#gmail.com';
$EmailSubject = 'Exza Contact Form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name :".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Email :".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Subject : ".$_POST["subject"]."\r\n";
$MESSAGE_BODY .= "Message : ".nl2br($_POST["message"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

PHP contact form functionality

$from = "from#domain.com";
$to = "myemail#email";
$subject ="Contact Form";
$message ="<p>Item Name: $itemname.</p>
<p>Brand: $brandname.</p>
<p>Description: $description.</p>
<p>Availability: $availability.</p>
<p>Contact Person: $contact</p>
<p>Email: $email</p>
<p>Location: $location</p>
";
$boundary = md5(time());
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n";
$headers .= "Reply-To: ".$from."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Transfer-Encoding: 7bit"."\r\n\r\n";
$body .= "This is a MIME encoded message."."\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $message ."\r\n";
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/octet-stream; name=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
$body .= $encoded_content ."\r\n";
if (#mail( $to, $subject, $body, $headers)) {
$resultMessage = '<div class="alert alert-success">Thank You. Your message has been sent</div>';
} else {
$resultMessage = '<div class="alert alert-danger">Sorry Please Try Again.</div>';
}
}
This is my form PHP code. It is used by my logged in users so users usually signup by choosing username>email>password and register then they log in and see this contact form. so I want that when they submit a form , PHP should send their Username and Email. How can I do that?

Unable to send attached file to E-Mail [duplicate]

This question already has answers here:
Send attachments with PHP Mail()?
(16 answers)
Closed 7 years ago.
I am trying to send attach file to email. But i am unable to send the attach file along with the form data to email. I tried
HTML:
<form action="sendmail.php" method="post" name="form1" enctype="multipart/form-data">
<table width="343" border="1">
<tr>
<td>Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" id="city"></td>
</tr>
<tr>
<td>Descibe Yourself</td>
<td><textarea name="description" cols="30" rows="4" id="description"></textarea></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input name="mobile" id="mobile"type="text"></td>
</tr>
<tr>
<tr>
<td>Email</td>
<td><input name="email" type="email" id="email"></td>
</tr>
<tr>
<td>Attach Your Image</td>
<td><input name="fileAttach" type="file"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
sendmail.php:
<?
$name = $_POST['name'];
$city = $_POST['city'];
$description = $_POST['description'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
if ($name == '' || $city == '' || $description==''|| $mobile=='' || $email=='')
{
echo "<script> alert('Fill all fields, try again!');
window.location='contact.html'</script>";
exit;
}
$ToEmail = 'info#mywebsite.com';
$EmailSubject = 'Website contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Description: ".$_POST["description"]."<br>";
$MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader, $strHeader) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
exit;
if($mail)
{
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
}
else
{
echo "<script> alert('Temporary problem, try again!');</script>";
}
?>
I tried the same code without $strHeader in mail() the mail was sent but when i added $strHeader to mail() i was unable to send code. Kindly guide me where i am doing mistake and also plz tell how how can i allow only jpg,jpeg,png format to to uploaded. Edited code for me would be easy for me for learning. I checked lot of answers on stackoverflow bt couldn't found the answer.
This has been already answered.
Check out form not sending image file to email
and my comment to send email with attachment.
Try using \r\n instead of \n ...
$strHeader .= "--".$strSid."\r\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\r\n";
$strHeader .= "Content-Transfer-Encoding: base64\r\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\r\n\r\n";
$strHeader .= $strContent."\r\n\r\n";
EDIT
Change:
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Description: ".$_POST["description"]."<br>";
$MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";
With:
$hash = md5(date('r', time()));
$mailheader = "From: {$_POST["email"]}\r\n";
$mailheader .= "Reply-To: {$_POST["email"]}\r\n";
$mailheader .= "Content-type: text/html; boundary=\"PHP-mixed-$hash\"";
$MESSAGE_BODY = "Name: {$_POST["name"]}<br><br>";
$MESSAGE_BODY .= "Email: {$_POST["email"]}<br>";
$MESSAGE_BODY .= "Description: {$_POST["description"]}<br>";
$MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";
And change
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
With
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$MESSAGE_BODY .= "--PHP-mixed-$hash\r\n";
$MESSAGE_BODY .= "Content-Type: application/octet-stream; name=\"$strFilesName\"\r\n";
$MESSAGE_BODY .= "Content-Transfer-Encoding: base64\r\n";
$MESSAGE_BODY .= "Content-Disposition: attachment; filename=\"$strFilesName\"\r\n\r\n";
$MESSAGE_BODY .= $strContent."\r\n";
$MESSAGE_BODY .= "--PHP-mixed-$hash--";
You missed the boundary.

Sending email with inline image

My code for sending email
<?php
$to = 'piyu.mulay#gmail.com';
$subject = 'Website Change Request';
$headers = "From: chintamani.mulay#gmail.com \r\n";
$headers .= "CC: chintamani.mulay#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="../1.jpg">';
$message .= '</body></html>';
echo mail($to, $subject, $message, $headers);
?>
When i put link in <img src="http://weber-steinach.de/files/339349"> it works well but when I put path of file which is in my localserver I won't show and Image in email.
Finally I come to know how to do this. Here is the source link from where I come to know
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: youremail#host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<html>
<head>
<style> p {color:green} </style>
</head>
<body>
A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>
</html>'."\n\n".
$bound;
$file = file_get_contents("http://localhost/a/img/1.jpg");
$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;
echo mail($to, $subject, $message, $headers) ;
?>

Contact form throws errors when sending an email.

I am developing a contact form for sending an email to user data,but its not working.
Code:
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'youremail#site.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader = "Reply-To: ".$_POST["email"]."\r\n";
$mailheader = "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY = "Email: ".$_POST["email"]."";
$MESSAGE_BODY = "Comment: ".nl2br($_POST["comment"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="mail.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="Comment" cols="45" rows="6" id="Comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>
It throws an errors like:
Undefined index: commen in line 10.
header missing in line 11.
Here are some mistakes on your code..
1.at the line no 2 change the code from (i don't know why you didn't mention about this error)
if ($_POST["email"]<>'') {
to
if (isset($_POST["email"]) && $_POST["email"]<>'') {
2.change the name of the textarea from "Comment" to "comment"
and finally follow the instruction from the previous answers of this post to solve the "header missing" problem.
something like
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
As everybody told your first mistake, I am not going to repeat it but another mistake is:
You should join the mail headers and mail body:
Your previous code:
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader = "Reply-To: ".$_POST["email"]."\r\n";
$mailheader = "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY = "Email: ".$_POST["email"]."";
$MESSAGE_BODY = "Comment: ".nl2br($_POST["comment"])."";
New Code:
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
Look at the dots given before the equalto signs.
see you need to concatenate the mailHeader var same goes for body message
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
You have done the two mistakes.
In form you have given name="Comment" for please give name="comment" or $_POST["comment"] to $_POST["Comment"]
put the . after mail header.like
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
you are getting this error 10th line because you are using $_POST["comment"]" but in the form you have written id="Comment". To remove the error take care of upar and lower case in the both places..
Thank you.

Categories