I am writing an application that is to send an email that contains a CSV as an attachment. However, it is not working. The mail function does not return false (implying that it succeeded); however, the email is not being sent.
I have sent/received email from the server before, so I do not believe that is the issue. However, I do not really have any experience with sending attachments through PHP, so I would suspect the issue lies in there. I have followed this tutorial to an extent: http://www.texelate.co.uk/blog/post/56-send-an-email-attachment-with-php/
Here are some code snippets:
$file = tmpfile();
// code omitted
$fileArray = array(); // this gets filled with more arrays/lines of data
// code omitted
try {
foreach($fileArray as $line) {
var_dump($line);
fputcsv($file, $line);
}
// begin mailing
$toEmail = 'myemail';
$subject = "Insurance Registration";
fseek($file, 0);
$attachData = fread($file, 1024);
$fileName = $lastName . "_" . date('Y');
$random = md5(time());
$boundary = "==Multipart_Boundary_x{$random}x";
$headers = "From: Test \nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed\n" . "boundary=\"{$boundary}\"";
$message = "Testing!.\n\n " . "-{$boundary}\n " . "Content-Type= text/plain; charset=\"iso-8859-1\n" . "Content-Transfer-Encoding 7bit\n\n " . "Testing Again " . "\n\n ";
$attachData = chunk_split(base64_encode($attachData));
$message .= "--{$boundary}\n" . "Content-Type: text/plain;\n" . "name=\"{$fileName}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileName}\"\n" . " . Content-Transfer-Encoding: base64\n\n" . $attachData . "\n\n" . "-{$boundary}-\n";
} catch (Exception $e) {
echo $e->getMessage();
}
if(mail($toEmail, $subject, $message, $headers)) {
} else {
$hasError = true;
}
if($hasError) {
echo "<h2>Error Submitting form. Please check your input and try again</h2>";
} else {
header('Location: success.php');
}
I get redirected to the success page, but never receive an email. Thanks!
Related
I'm trying to store an uploaded file from a form to the uploads folder as well as send the file as an attachment to an email. The email sends perfectly fine and there is also an attachment, however the attachment is unreadable/corrupted.
I have tried with multiple file types and that makes no difference, I still have the same errors.
<?php
//Store File
if (!empty($_FILES) && isset($_FILES['upload'])) {
switch ($_FILES['upload']["error"]) {
case UPLOAD_ERR_OK:
$target = "uploads/";
$target = $target . basename($_FILES['upload']['name']);
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
$status = "The file " . basename($_FILES['upload']['name']) . " has been uploaded";
$imageFileType = pathinfo($target, PATHINFO_EXTENSION);
} else {
$status = "Sorry, there was a problem uploading your file.";
}
break;
}
echo "Status: {$status}<br/>\r\n";
}
$fileatt = getcwd() . "/uploads"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = $_FILES['upload']['name']; // Filename that will be used for the file as the attachment
$email_from = $_POST['email']; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for visiting mysite.com! Here is your free file.";
$email_message .= "Thanks for visiting."; // Message that the email has in it
$email_to = 'test#test.com'; // Who the email is to
$headers = "From: " . $email_from;
$file = fopen($fileatt . "/" . $fileatt_name, 'rb');
$data = fread($file, filesize($fileatt . "/" . $fileatt_name));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\r\nMIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\r\n\r\n" . "--{$mime_boundary}\r\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . $email_message .= $_POST['email'] . "<br>" . $_POST['firstname'] . "<br>" . $_POST['surname'] . "<br>" . $_POST['degree'] . "<br>" . "\r\n\r\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\r\n" . "Content-Type: {$fileatt_type};\r\n" . " name=\"{$fileatt_name}\"\r\n" . "Content-Disposition: attachment;\r\n" . " filename=\"{$fileatt_name}\"\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n" . $data .= "\r\n\r\n" . "--{$mime_boundary}--\r\n";
$ok = mail($email_to, $email_subject, $email_message, $headers);
if ($ok) {
echo "You file has been sent
to the email address you specified.
Make sure to check your junk mail!
Click here to return to mysite.com.";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
Please could somebody point me in the right direction?
You can find the solution here.
Explained completely to need to ass same info again.
Solution
I'm getting spam emails from my web form every week. I have tried to make some changes in my query, but that hasn't helped. I am sharing my query below, including the headers.
Can someone help me find where the problem is?
<?php
$submitted_email = '';
if (isset($_SESSION['form'][$mail_from_email])) {
$submitted_email = $_SESSION['form'][$mail_from_email];
}
if (check_email($submitted_email) && $send_from_users_email === false) {
$from = $reply_to = $_SESSION['form'][$mail_from_name].' <'.$submitted_email.'>';
} else {
$from = '<'.$email.'>';
$reply_to = check_email($submitted_email) ? '<'.$submitted_email.'>' : $from;
}
$subject = '';
if (isset($_SESSION['form'][$mail_subject])) {
$subject = $_SESSION['form'][$mail_subject];
}
//email headers
if ($windows_server === true) {
$headers = "From: test.co.uk\r\n" .
"Reply-to: test.co.uk\r\n" .
"Return-Path: test.co.uk\r\n".
"MIME-Version: 1.0\r\nContent-Type: multipart/mixed; " .
"boundary=$mixed_mime_boundary";
} else {
$headers = "From: $from\n" .
"Reply-to: $reply_to\n" .
"MIME-Version: 1.0\nContent-Type: multipart/mixed; " .
"boundary=$mixed_mime_boundary";
} else {
$headers = "From: $from\n" .
"Reply-to: $reply_to\n" .
"MIME-Version: 1.0\nContent-Type: multipart/mixed; " .
"boundary=$mixed_mime_boundary";
}
////////////////////////////
// CONSTRUCT HTML CONTENT //
////////////////////////////
//Construct HTML email content, looping through each form element
//Note: When you get to a file attachment you need to use $_FILES['form_element']['name']
//This will just output the name of the file. The files will actually be attached at the end of the message.
//Set a variable for the message content
$html_content = "<html>\n<head>\n<title>" .
safe_escape_string($subject) .
"</title>\n</head>\n<body>\n<p>\n";
////////////////////////////
// CONSTRUCT TEXT CONTENT //
////////////////////////////
//construct a plain text version of the email.
$text_content = '';
//build a message from the reply for both HTML and text in one loop.
foreach ($form_fields as $field => $label) {
$html_content .= '<b>' . safe_escape_string($label) . '</b> ';
$text_content .= "$label ";
if (isset($_FILES[$field])) {
$string = (isset($_FILES[$field]['name'])) ? $_FILES[$field]['name'] : '';
} else {
$string = (isset($_SESSION['form'][$field])) ? $_SESSION['form'][$field] : '';
}
$html_content .= nl2br(safe_escape_string($string)) . "<br /><br />\n";
$text_content .= "$string\n\n";
}
//close the HTML content.
$html_content .= "</p>\n</body>\n</html>";
/////////////////////////////
// CONSTRUCT EMAIL MESSAGE //
/////////////////////////////
//Now we combine both HTML and plain text version of the email into one.
//Creating the message body which contains a Plain text version and an HTML version,
//users email client will decide which version to display
$message = "\r\n--$mixed_mime_boundary\r\n" .
"Content-Type: multipart/alternative; boundary=$alt_mime_boundary\r\n\r\n" .
"--$alt_mime_boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8; format=flowed\r\n" .
"Content-Transfer-Encoding: Quoted-printable\r\n\r\n" .
"$text_content\r\n\r\n" .
"--$alt_mime_boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: Quoted-printable\r\n\r\n" .
"$html_content\r\n\r\n" .
"--$alt_mime_boundary--\r\n\r\n" .
"\r\n\r\n--$mixed_mime_boundary";
//////////////////////
// FILE ATTACHMENTS //
//////////////////////
//loop through the $_FILES global array and add each attachment to the form.
if (isset($_FILES)) {
foreach ($_FILES as $attachment) {
$filename = $attachment['name'];
//if the file has been uploaded
if ($attachment['error'] === UPLOAD_ERR_OK && is_uploaded_file($attachment['tmp_name'])) {
$file = fopen($attachment['tmp_name'],'rb');
$data = fread($file,filesize($attachment['tmp_name']));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "\r\nContent-Type: application/octet-stream; name=\"$filename\"" .
"\r\nContent-Disposition: attachment; filename=\"$filename\"" .
"\r\nContent-Transfer-Encoding: base64\r\n\r\n$data\r\n\r\n--$mixed_mime_boundary";
} else if ($attachment['error'] !== UPLOAD_ERR_NO_FILE) {
//try to provide a useful error message determined from the error code.
switch ($attachment['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$error = "File $filename exceeds the " . ini_get('upload_max_filesize') . 'B limit for the server.';
break;
case UPLOAD_ERR_PARTIAL:
$error = "Only part of the file $filename could be uploaded, please try again.";
break;
default:
$error = "There has been an error attaching the file $filename, please try again.";
}
redirect($return_url, $error);
}
}
}
//finish off message
$message .= '--';
//for windows users.
if ($windows_server === true) {
ini_set('sendmail_from', $email);
}
//if the mail sending works
if (#mail($email, $subject, $message, $headers)) {
//set the success message
$notice = $message_success;
unset($_SESSION['form']);
} else {
$notice = "I'm sorry, there seems to have been an error trying to send your email. Please try again.";
}
//redirect to the form
redirect($return_url, $notice);
}
?>
I am using mPDF class and am successfully able to generate an email with the following code. However, my email comes out blank. I am assuming but am not sure if this has something to do with my headers. It's hard for me to tell because I am getting my emails but am not able open the pdf it generates.
include("./mpdf.php");
$mpdf->debug = true;
$html2 = '
<div style="margin-left:3%;">Attach additional photos:
<input type="file" name="file" id="file" /></div><hr />';
echo $html2;
if ( isset( $_POST['submit'] ) ) {
$file_path = "webform.php";
$file_path_type = "application/pdf";
$mpdf=new mPDF('iso-8859-2');
$mpdf->WriteHTML($html);
$file_path_name = "eval.pdf";
$headers .= 'Content-type: text/html; charset=utf-8' . "\n";
$from = "info#myemail.com";
$to = $_POST['email'];
$ccto = $_POST['youremail'];
$subject = "New Form Submitted";
$message = "*** This is an automatically generated email,
please do not reply *** Someone in your association
has completed a survey.
$headers = "From: ".$from;
$headers.= "cc: " . $ccto . " <" . $ccto . ">" . "\n" ;
$file = fopen($file_path,'rb');
$data = fread($file,$file_path);
fclose($file);
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if(#mail($to, $subject, $message, $headers)) {
echo '<script language="javascript">';
echo 'alert("Document sent successfully!")';
echo '</script>';
echo "Sent!";
} else {
echo 'Failed';
}
}
exit;
PHP mail and mpdf users any help would be appreciated.
You're learning the hard way - Don't call mail() yourself because you will do it wrong; constructing and sending email messages is horribly complicated and full of pitfalls, as you're finding. Use a library, whether PHPMailer, SwiftMailer Zend_Mail etc, to do it and it will save you a great deal of hassle. You also need to check your two operations separately - first create a PDF, write it to a file and make sure it works correctly; Then write some code that sends a message and check that works; Then get it to send the PDF. Otherwise if you find it's not working, you won't be able to tell which part is broken.
Here is how I did it with MPDF and PHPMAILER.
I also have it so you can attach another file within my form that I made. Hope this helps you along the way.
include("mpdf/mpdf.php");
if ( isset( $_POST['submit'] ) ) {
$mpdf=new mPDF('c','Letter','','','10','10','10','10','','');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML(utf8_encode($html));
$emailAttachment = $mpdf->Output('serviceagreement.pdf', 'S');
//$emailAttachment = chunk_split(base64_encode($emailAttachment));
require("php_mailer/class.phpmailer.php");
$mail = new PHPMailer(true);
try {
$mail = new PHPMailer;
$mail->AddAddress('send email');
$mail->SetFrom('support#myorganization.com');
$mail->AddReplyTo($_POST['email1']);
$mail->Subject = 'Evaluation';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML("*** Form attached! Please see the attached form (.pdf).");
$mail->AddStringAttachment($emailAttachment, $filename = 'serviceagreement.pdf',
$encoding = 'base64',
$type = 'application/pdf'); // attachment
if (isset($_FILES['attached']) &&
$_FILES['attached']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attached']['tmp_name'],
$_FILES['attached']['name']);
}
$mail->Send();
echo "<div style='margin-left:4%;'>Message Sent OK</div>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}}
?>'
The problem I'm having is that I want to pass through data from a number of fields in a form into one email send via PHP, the concerned code is as follows:
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
This is where I'm declaring all the information to be passed as a header for the email. The current PHP passes syntax check as works to the extent that an email is sent containing the $to, $subject, $message all fine, but the $header only passes through the final part (Keep Data? $data\n). When I remove all the other fields and simply keep the $data part, the email stops sending any $header. I also have an issue with the redirect, which has been removed from the below code and will be inserted as soon the current issue is resolved. The current full PHP is:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
?>
</body>
and is temporarily hosted at http://luke-hale.com/temp/contact.html. I'm sure this is just a case of being misinformed on my part, but any help would be great, cheers!
EDIT:
okay so that issue is sorted with:
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'] . "\r\n\r\n" . $_POST['inputname'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
Which works though doesn't display the $headers anywhere, but I'm not too fussed about this, the next thing is the redirect, which I would usually run through via:
header("refresh:5;url=http://www.domain.com")
Though this was not working correctly earlier, I will apply an edit when tested for anyones future reference.
EDIT
So the form works but the re-direct does not. The site is still hosted on the same domain, but now the full PHP looks like this:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
header("refresh:5;url=http://www.domain.com")
?>
</body>
The form still sends fine but the return states that the line beginning "header" cannot be passed because the browser data has already been modified. I'm not sure sure what I've done or where so if anyone could lend a hand, that'd be great!
FINAL EDIT
My final, fully working, code:
<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
ob_start();
if (isset($_REQUEST['inputemail'])&&($_REQUEST['inputname'])&&($_REQUEST['inputmessage']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid email, you'll be redirected ";
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for your messgae, I'll get back to you as soon as possible! You'll be redirected in 5 seconds.";
}
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
echo "You did not fill all the required fields, please try again.";
header("refresh:5;url=http://www.luke-hale.com/");
}
?>
Try putting your form data into the $message variable, insert \n after each attribute to give a new line. Do not use the header.
Location and Refresh both require an absolute URI
header('Location: http://www.domain.com');
Examples for mail:
https://stackoverflow.com/a/22833890/3489793
https://stackoverflow.com/a/22831825/3489793
mail("Your Email Address Here",$subject,$therest,"subject: $subject\n");
U said this:
"The mail function is also not the issue, and correct syntax is" mail($to,$subject,$message,$headers,$parameters)
Explanation:
your email adress = $to
$subject = $subject
$therest = $message (rename it if you want)
"subject: $subject\n" = the header (change it to $headers if you want that)
Header example (as in the documentation)
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You could echo a Javascript at the bottom of your PHP script which redirects instead, like this:
$url = "url";
$time = 5000 // time in milliseconds
print "<script>window.setTimeout(function(){window.location='".$url."'},".$time.") </script>"
I have a simple PHP contact form (Seen Here) that I want to add a file upload option to, so clients can attach an important document and mail it to me using PHP's mail function.
The form works fine on its own, but I can't seem to get the code right for uploading the attachment, storing it temporarily on the server and sending it to me as part of the e-mail. Here is the code I'm using:
<?php
if ($_POST['test'] != '') {
echo 'Unfortunately, by filling out the hidden field, you have been identified as a potential spambot and your message has been terminated.';
} else {
//Validate the name:
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
echo "You forgot to enter your name.<br>";
}
//Validate the phone:
if (!empty($_POST['phone'])) {
$phone = $_POST['phone'];
} else {
echo "You forgot to enter your phone number.<br>";
}
//Validate the e-mail:
if (!empty($_POST['email'])) {
$email = $_POST['email'];
} else {
echo "You forgot to enter your e-mail.<br>";
}
//Validate the message:
if (!empty($_POST['message'])) {
$message = $_POST['message'];
} else {
echo "You forgot to enter a message.";
}
if (!empty($_POST['name']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['message'])) {
// Obtain file upload variables:
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_name = $_FILES['attachment']['name'];
if (file($attachment)) {
// Read the file to be attached ('rb' = read binary):
$file = fopen($attachment,'rb');
$data = fread($file,filesize($attachment));
fclose($file);
// Generate a boundary string:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment:
$headers = "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message:
$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";
// Base64 encode the file data:
$data = chunk_split(base64_encode($data));
// Add file attachment to the message:
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$attachment_type};\n" .
" name=\"{$attachment_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$attachment_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
$body = "$name\n$phone\n$email\n\n$message";
mail("*#*.com", "Starcrest Escrow, Inc. Website - Real Property Sale", $body, $headers);
header("Location: confirm.html");
}
}
?>
When I run this script presently, it forwards me to the confirmation page, but no e-mail appears to be generated at all. What am I doing wrong?
<?php
if (isset($_POST['txtEmail'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xyz#abc.com";
$email_subject = "Subject";
$email_from = "abc#xyz.com";
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['txtName']) || !isset($_POST['txtEmail']) || !isset($_POST['txtAddress']) || !isset($_POST['txtContact']) || !isset($_POST['txtUpload'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['txtName']; // required
$email = $_POST['txtEmail']; // required
$address = $_POST['txtAddress']; // required
$contact = $_POST['txtContact']; // not required
$upload = $_POST['txtUpload']; // required
$email_message = "Form Details are below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message.= "Full Name: " . clean_string($name) . "\n\n";
$email_message.= "Address: " . clean_string($address) . "\n\n";
$email_message.= "Email ID: " . clean_string($email) . "\n\n";
$email_message.= "Contact No.: " . clean_string($contact) . "\n\n";
$email_message.= "File: " . clean_string($upload) . "\n\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>