PHP Form fields will not send to email - forms

I have created a send to email PHP script.
The form seems to work correctly when sending only one field (the message field) but as soon as other fields are added, the form ceases to be emailed on to the inbox (yet the form still fires correctly and reroutes to the thankyou just fine.)
Here is the code I currently have, this does not work
<?php
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$services = $_REQUEST['services'] ;
$message = $_REQUEST['message'] ;
if (!isset($_REQUEST['email'])) {
header( "Location: feedback.html" );
}
elseif (empty($email) || empty($message)) {
header( "Location: error.html" );
}
else {
mail( "info#website.co.uk", "Message via your website!",
$name, $services, $message, "From: $email" );
header( "Location: thankyou.html" );
}
?>
This is the previous code, this does work, but only displays the message
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
if (!isset($_REQUEST['email'])) {
header( "Location: feedback.html" );
}
elseif (empty($email) || empty($message)) {
header( "Location: error.html" );
}
else {
mail( "info#website.co.uk", "Message via your website!",
$message, "From: $email" );
header( "Location: thankyou.html" );
}
?>
This is the HTML for the form:
<form method="post" action="sendmail.php">
<label>Name:</label> <input name="name" type="text" /><br />
<label>Email:</label> <input name="email" type="text" /><br />
<label>What service do you require?:</label> <input name="services" type="text" /><br />
<label>Message:</label><br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input type="submit" />
</form>

As per the mail() docs the message body should be passed as a single parameter. So it should look something more like:
mail( "info#website.co.uk", "Message via your website!", "$name\r\n $services\r\n $message", "From: $email");

According to php.net the correct use of the mail() function is:
<?
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
You are trying to send the additional data ($services) as a header.
Try to merge your $message and $service >
$message = $service . "\r\n\r\n" . $message;
mail( "info#website.co.uk", "Message via your website!",
$message, "From: $email" );

You can't just add more and more variables to mail(). There is only one parameter that specifies the content of the email, which would be the third one, as you can see in the official documentation:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
If you want to have more than just $message in your mail, you need to put everything into one variable to be given to the function as content.
$content = $message;
$content .= "\n" . $services;
// etc...
mail($to, $subject, $content);

Try this
<?php
$email = $_POST['email'] ;
$name = $_POST['name'] ;
$services = $_POST['services'] ;
$message = $_POST['message'] ;
if (!isset($_POST['email'])) {
header( "Location: feedback.html" );
}
elseif (empty($email) || empty($message)) {
header( "Location: error.html" );
}
else {
mail( "info#website.co.uk", "Message via your website!",
$name . $services. $message . "From: $email" );
header( "Location: thankyou.html" );
}
?>

Related

reply to sender mail php script

I have been looking for a mailform script and came up with his. Works fine, but just this matter: when I reply to the received email, I am replying to myself, whilst I would like to reply automatically to the mailadres of the sender.
<?php
$to = "MY#MAILADRES.COM";
$subject = "mail via website";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$onderwerp = $_POST['onderwerp'];
$comments = $_POST['message'];
$message = "
name: $firstname $lastname
email: $email
subject: $onderwerp
message: $comments
";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: MY#MAILADRES.COM\r\n".
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
foreach($_FILES as $userfile)
{
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name))
{
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$message.="--{$mime_boundary}--\n";
if (mail($to, $subject, $message, $headers))
echo "Thanks for getting in touch.<br>Your message wil get my full attention.<br>I will get back to you soon.";
else
echo "Error in mail.<br>Please try again.";
?>
What should I change in this code, please?
I added these (seperately):
$headers .= "From: Contact Form (Domain.com) <no-reply#domain.com>\r\n";
$headers .= "Reply-To: ".$_REQUEST['email']."<". $_REQUEST['email'].">\r\n"
But none seem to work. I got this code to work with and all good, except for the reply-to.

Email attachment works fine in gmail but not on my mailserver

Using PHP i am sending email attachment on both my gmail and mailserver id(company email id). I am getting email in both of my email account but on my company email id my email attachment shows error(it doesn't open properly) whereas in gmail it is fine.
Any ideas or suggestion??
I am pasting my php code here. any help would be highly appreciated.
HTML CODE:
<form action="contact.php" method="post" name="form1" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="fileAttach" id="file"><br><br>
<input type="submit" name="submit" value="Submit" align="right">
</form>
contact.php:
<?php
if($_POST['submit'])
{
$strTo = "mymail#company.com, mymail#gmail.com";
$strSubject = "Attachment file";
$strMessage = "Attachment";
$txtFormEmail = "mymail#gmail.com";
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From: "."<".$txtFormEmail.">\nReply-To: ".$txtFormEmail."";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
//*** 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";
}
$flgSend = mail($strTo,$strSubject,$strMessage,$strHeader);
}
?>
You can use PHP Mailer Class to send mails with attachments.

PHP mail opens in Gmail but not in Outlook

When I send a mail with PHP it displays correct in Gmail but not in Outlook. It's a mail with a PDF attachment and some text. The PDF is created with fpdf and sent as an attachment, this works fine in Gmail and Outlook.
The only problem there is, in Gmail the text of the emails is displayed correctly and in Outlook it's just a blank page and the text is an extra attachment.
Here is my code:
// encode data
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// email stuff (change data below)
$to = $contactEmail;
$from = $myEmail;
$subject = "MySubjectHere";
$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head></head><body>
<p>Hi '$contactName.',</p><br>
<p>Please find your pdf attached per your request.</p>
<p>Feel free to call or email if you have any questions.</p>
<p>Kind regards,</p><br>
<p>MyNameHere</p>
<p><strong>Manager</strong></p>
<img alt="image" src="http://LinkToImage.png"/>
<p style="color:#76923c;">
<strong>P:</strong> 01 2345 6789|
<strong>W:</strong> www.example.com</p>
<p style="color:#76923c;">
<strong>A:</strong> 94 Test Street.
</p></body></html>';
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = $id."_".str_replace(" ","_",$Name).'.pdf';
// main header
$headers = "From: ".$from.$eol;
$headers .= "Bcc: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
I found where it went wrong:
$body .= $message.$eol;
Should be
$body .= $message.$eol.$eol;
Finally found it after 3 days!
Thanks for the responses anyway!
$to = $contactEmail;
$from = $myEmail;
$boundary = md5(date('r', time()));
$msg = "This is a multi-part message in MIME format.\n\n";
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/html; charset=utf-8; format=flowed; delsp=yes\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= "<h1>Website Enquiry</h1>
<p><strong>Name:</strong> $name<br />
<strong>Email:</strong> $email<br />
<strong>Phone:</strong> $phone<br />
<strong>Subject Name:</strong> $subject<br />
<strong>Question:</strong> $question</p>";
$e_headers = "From: " . $from . "\n";
$e_headers .= "MIME-Version: 1.0\n";
$e_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
mail($to, $subject, $msg, $e_headers);
This is the code I would use for a HTML contact form.
You shouldn't need to pass the content through the PHP_EOL, just the attachment
$separator = md5(time());
$eol = "\r\n";
$message=""; // Always empty
$mailmessage="<h1>Yes got it</h1>"; //html content
$headers = "From: ".$email.$eol;
$headers .= "Bcc:". $Bcc. "\n";
$headers .= "Cc:". $cc. "\n";
$headers .= "Return-Path: <".$email.">\n";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "--" . $separator . "\r\n";
$body .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n";
$body .= $message . "\r\n";
$body = "--" . $separator . $eol;
$body .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $mailmessage . $eol.$eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol.$eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers);

PHP Sending Emails with File Attachments - Email Not Sending At All

After trying to read various articles on sending emails with attachments in PHP (I am use to ASP with VBScript), I wrote the code below. Unfortunately, it does not work at all. Not only does it not send the email with the attachment, the email doesn't seem to send at all, even though my script says that it did send. Where have I gone wrong? I'm not using a form to upload a file. This is a static script.
<?php
$EmailTo = "Me#here.com";
$EmailFrom = "You#There.com";
$EmailSubject = "The Email Subject";
$MailBoundary = md5(uniqid(time()));
$Headers = "To: ". $EmailTo . "\r\n";
$Headers .= "From: ". $EmailFrom . "\r\n";
$Headers = "MIME-Version: 1.0\r\n";
$Headers .= "Content-type: multipart/mixed;boundary=\"$MailBoundary \"";
$Headers .= "\r\n\r\n";
$Headers .= "This is a multi-part message in MIME format.";
$Headers .= "\r\n\r\n";
$FileAttachment = "AttachedFile.pdf";
$File = fopen($FileAttachment, "r");
$FileData = fread($File, filesize($FileAttachment));
$FileData = chunk_split(base64_encode($FileData));
$FileName = basename($FileAttachment);
$EmailBody = "--$MailBoundary\r\n";
$EmailBody .= "Content-type: text/html; charset=iso-8859-1\r\n";
$EmailBody .= "Content-transfer-encoding: 8bit\r\n\r\n";
$EmailBody .= "<html>" . chr(13) .
"<head>" . chr(13) .
"<style>" . chr(13) .
".breg {font-family:arial;font-size:10pt;color:#000000;padding:5px;}" . chr(13) .
"</style>" . chr(13) .
"</head>" . chr(13) .
"<body>" . chr(13) .
"<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) .
"The message text body goes here" . chr(13) .
"</div>" . chr(13) .
"</body>" . chr(13) .
"</html>";
$EmailBody .= "--$MailBoundary\r\n";
$EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName\r\n";
$EmailBody .= "Content-transfer-encoding:base64\r\n\r\n";
$EmailBody .= $FileData. "\r\n\r\n";
$EmailBody .= " --$MailBoundary--";
if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers))
{
echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13);
}
else
{
echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13);
}
?>
For sending mail with attachment using php mail().Try this code:
<?php
//If there is no error, send the email
if(isset($_POST['ur_submit_button_name'])) {
$EmailTo = "Me#here.com";
$EmailFrom = "You#There.com";
$EmailSubject = "The Email Subject";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "ip.zip";//store that zip file in ur root directory
$attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
$mail_sent=true;
echo "mail sent";
} else {
$mail_sent=false;
echo "Error,Mail not sent";
}
}
?>
i think you have an error in your email content,
generate your email content with imap_mail_compose function:
mail('', 'message subject', '', imap_mail_compose(array header, array body));

Adding an attachment to this mail form

I have this simple php mail form. It is working and I can use it for making my forms, but I have a problem:
I want to add 2 or 3 attachments to this form. I tried a lot reading about mail at php.net, but I cannot do it by my self.
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$mobile=$_POST['mobile'];
$subject=$_POST['subject'];
$website=$_POST['website'];
$message=$_POST['message'];
$fulltext = "
______________________________________________
|
| This Is $name Information:
|______________________________________________
| Name : $name
|______________________________________________
| E-Mail : $email
|______________________________________________
| Address : $address
|______________________________________________
| Phone : $phone
|______________________________________________
| FAX : $fax
|______________________________________________
| Mobile : $mobile
|______________________________________________
| Subject : $subject
|______________________________________________
| Website : $website
|______________________________________________
| Message : $message
|______________________________________________
";
$to = 'support#site.com';
$subject = 'Connect FORM <<';
$headers = 'From: contactform#site.com' . "\r\n" .
$message = $fulltext;
mail($to, $subject, $message, $headers);
echo 'file ersal shod';
?>
I improved code from http://ru.php.net/manual/ru/function.mail.php#105661 :
<?php
function multi_attach_mail($to, $subject, $message, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
//$subject = date("d.M H:i")." F=".count($files);
$message .= "\n".count($files)." attachments";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
multi_attach_mail("to#somebody.dom",
"Subject of mail" ,
"Hello world!!!",
array($_SERVER["DOCUMENT_ROOT"] . "/first.file", // one file in root of your site
$_SERVER["DOCUMENT_ROOT"] . "/second.file" // another one
),
"from#someone");
?>