Sending a Multipart email with (HTML, Plain Text and Attachment) in PHP - php

I am sending a multi-part email with php with html formatting and an attachment...
All is working fine but when the user receiving that email from our website it is showing the boundary as well in last of the HTML part.
But when i remove the boundary line the email showing file contents as base64 (no attachment).
if ( isset($_POST['submit']) )
{
$to = 'admin#abc.com';
$FromSubmitting = $_POST['submitting'];
$FromTitle = $_POST['title'];
$FromName = $_POST['name'];
$FromDesignation = $_POST['designation'];
$FromQualification = $_POST['qualification'];
$FromInstitute = $_POST['institute_address'];
$FromHomeAddress = $_POST['home_address'];
$FromEmail = $_POST['email'];
$FromWorkPhone = $_POST['work_phone_number'];
$FromHomePhone = $_POST['home_phone_number'];
$FromMobilePhone = $_POST['mobile_phone_number'];
$FromPresentingAuthor = $_POST['presenting_author'];
$FromConferenceTheme = $_POST['conference_theme'];
$FromTypeOfSubmission = $_POST['type_of_submission'];
$subject = "New ".$FromSubmitting." Submitted From: ".$FromName;
/* GET File Variables */
$tmpName = $_FILES['uploaded_file']['tmp_name'];
$fileType = $_FILES['uploaded_file']['type'];
$fileName = $_FILES['uploaded_file']['name'];
/* Start of headers */
$headers = "From: $FromEmail";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file); /* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message .= "--{$mimeBoundary}\n"
. "Content-Type: text/html; charset=UTF-8\n"
. "Content-Transfer-Encoding: 8bit\n\n"
. "<table>
<tr><td>Submission Type:</td><td>$FromSubmitting</td></tr>
<tr><td>Name:</td><td>$FromTitle $FromName</td></tr>
<tr><td>Designation:</td><td>$FromDesignation</td></tr>
<tr><td>Qualification:</td><td>$FromQualification</td></tr>
<tr><td>Institute:</td><td>$FromInstitute</td></tr>
<tr><td>Home Address:</td><td>$FromHomeAddress</td></tr>
<tr><td>Work Phone:</td><td>$FromWorkPhone</td>
<td>Home Phone:</td>
<td>$FromHomePhone</td><td>Mobile Phone:</td>
<td>$FromMobilePhone</td></tr>
<tr><td>Presenting Author:</td><td>$FromPresentingAuthor</td></tr>
<tr><td>Conference Theme:</td><td>$FromConferenceTheme</td></tr>
<tr><td>Type of Submission:</td><td>$FromTypeOfSubmission</td></tr>
</table>"
. "--{$mimeBoundary}\n"; // when removing this line it shows Image-2
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n"
. "Content-Type: {$fileType};\n"
. " name=\"{$fileName}\"\n"
. "Content-Transfer-Encoding: base64\n\n"
. $data . "\n\n"
. "--{$mimeBoundary}--\n";
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
}
Email Screen Shot Image-1 :
Email Screen Shot Image-2 :

I am not sure but you can try this.
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n"
."Content-Type: application/octet-stream;\n"
. " name=\"{$fileName}\"\n"
. "Content-Transfer-Encoding: base64\n\n"
. $data . "\n\n"
. "--{$mimeBoundary}--\n";

Related

PHP MAIL Body of email not being included

I have a script to email out attachments, but i cannot get the body of the email to be included it just sends blank
I have tried different charsets, UTF8 and the ISO8559
I tried
"Content-Type: text/html; charset=\"utf-8\n" .
"Content-Type: text/plaintext; charset=\"utf-8\n" .
But made no difference....
$name = "XXXXXXXX";
$email = "XXXXXX";
$to = "XXXXXXXXX";
$from = "XXXXXXXX";
$subject = "Thank you for your payment";
$mainMessage = "ANy text here to send in email";
$fileatt = "inv/".$invRef.".pdf";
$fileatttype = "application/pdf";
$fileattname = $invRef.".pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "" .
"-{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"utf-8\n" .
"Content-Transfer-Encoding: 7bit\n" .
$mainMessage . "\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
I have tried other mailers such as swift & simple external libraries but they caused me more headaches for other reasons
This has got me the closest to sending the email
The attachment works, the email sends, subject is fine.
Just nothing in the body

Warning: file() [function.file]: Filename cannot be empty. Why?

When I am going to submit this (Warning: file() [function.file]: Filename cannot be empty) warning shows. How can I solve this problem. Please help I am new in php. Email has been sent but files doesn't attach. I want to sending mail with attach with raw php.
may code is look like this:
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $fromName";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
echo $message;
$sendmail = mail ("$to", "$subject", "$message", "$headers");
if($sendmail){
echo "A email has been sent to: $to";
}
else{
echo "Error in Email sending";
}
}
if (file($tmpName)) {
You probably want if (file_exists($tmpName)) {?
Also make sure $tmpName is actually filled out.

How can I get my php form working correctly?

I built a form for my website using php. After the form is submitted, the content is emailed to me and the user is redirected to a different page. The code works fine when I fill the form out completely. However, one of the fields is a file upload and when I don't upload anything, the form still sends but leads to a 500 internal server error page. How can I make it so the form still redirects to the right page when a file isn't uploaded? I included my code below
<?php
$email_to = "ashley#MyURL.com";
$email_subject = "Completed Questionnaire";
$name = $_POST['name'];
$gradyear = $_POST['gradyear'];
$hschool = $_POST['hschool'];
$hometown = $_POST['hometown'];
$email_from = $_POST['email'];
$DOB = $_POST['DOB'];
$GPA = $_POST['GPA'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$about = $_POST['about'];
$message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);}
$message .= "Name: ".clean_string($name)."\n";
$message .= "Graduation Year: ".clean_string($gradyear)."\n";
$message .= "High School: ".clean_string($hschool)."\n";
$message .= "Hometown: ".clean_string($hometown)."\n";
$message .= "Email: ".clean_string($email_from)."\n";
$message .= "Date of Birth: ".clean_string($DOB)."\n";
$message .= "GPA: ".clean_string($GPA)."\n";
$message .= "Height: ".clean_string($height)."\n";
$message .= "Weight: ".clean_string($weight)."\n";
$message .= "About: ".clean_string($about)."\n";
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $email_from";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
#mail($email_to, $email_subject, $message, $headers);
header("Location: http://myURL.com/contact.html");
?>
You should check first that a file was indeed uploaded before manipulating the file:
if ( ! empty($_FILES)) {
//file manipulation...
}

how to attach multiple files from form to mail in php?

I am trying to attach two files at maximum, to send them by mail, but the files are sent by mail as binary code, so when I open the received mail I found the files as binary, here is my code:
$files = array();
if(is_uploaded_file($_FILES['cv']['tmp_name']))
array_push($files, $_FILES['cv']);
if(is_uploaded_file($_FILES['portfolio']['tmp_name']))
array_push($files, $_FILES['portfolio']);
$subject = "Contact Mail";
$headers = 'From: '.$email_fromto."\r\n".
"subject: {$subject}";
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"From: $sex $fname $lname.\r\n".
"Message: {$message}";
$email_fromto = "mail#mail.com";
foreach($files as $userfile){
$tmpName = $userfile['tmp_name'];
$fileType = $userfile['type'];
$fileName = $userfile['name'];
if(file($tmpName)){
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
}
so where is the error in what I did?
use like this
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message="This is an example"
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$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";
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (#mail($to, $subject, $message, $headers))
echo "Mail was Send Sucessfully";
else
echo "Failed to send";

Attachment but no text php email?

I am making a piece of code for a website and made the following code, however i need to put html in the email however it doesn't send it, it only attaches the image, please help?
<?php
$emailSubject = 'Application - '.$_POST['full_name'].'';
$webMaster = 'email#webaddress.com';
$from = $_POST["email"];
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
$headers = "From: $fromName";
if (file($tmpName)) {
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$html_message = "<html>"
$email_message = mail($webMaster, $emailSubject, $message, $headers);
$result = 'Result Code Here On Page';
echo "$result";
?>

Categories