PHP - sending email with attachment does not show message content - php

Trying to create a script where I can send an email with attachments. Everything works well except that when I don't add a file in the email I can still see an attachment with 0B and no name.
if(isset($_POST["my_send"])){
$email_to = $_POST['my_email_to']; //required
$email_from = "myemail#example.co.uk"; // required
$subject = $_POST['my_subject']; // not required
$comments = $_POST['write_my_email']; // required
$email_message .= stripcslashes("$comments");
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
// create email headers
$headers = 'From: '.$email_from."\r\n".
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"_1_$boundary\"\r\n";
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$email_message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($email_to, $subject, $message, $headers);
echo "<h5>Thanks, your email was sent successfully!</h5>";
}
what am I doing wrong? Any advice?

Just exclude this when there is no attachment:
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--
For example, only appending it to $message when the filename is not empty:
$message = "This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$email_message";
if (!empty($filename))
{
$message .= " --_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
}

$headers = array();
$headers [] = 'MIME-Version: 1.0';
$headers [] = 'Content-type: text/html; charset=utf-8';
$headers [] = 'Reply-To: ' . $From;
$headers [] = 'X-Mailer: PHP/' . phpversion();
$headers [] = "Return-Path: " . $From;
$headers [] = "Errors-To: " . $From;
$header = implode("\r\n", $headers);
$fifth = "-f $From";
if (mail($empfaenger, $betreff, $Mailbody, $header, $fifth)) {};
I think the last header should not end with '\r\n' This code is by someone who posted it in the manual. check the manual, you find good code there.

If you want to send emails with attachmenst I recommend you to use PHP-Mailer library. It will make your life a lot easier.
All you need to do is include the class and instantiate the PHPMailer Object
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//And you will need to set properties for the object
?>
Checkout this PHP-Mailer Github Link. They have also given a simple example on how to implement

Related

How to send HTML email with attachment?

I am using mail() function of PHP,
following is my code;
$to = "info#abc.com";
$subject = "Application for Job";
$attachment =chunk_split(base64_encode(file_get_contents($_FILES['attachresume']['tmp_name'])));
$filename = $_FILES['attachresume']['name'];
$boundary = md5(date('r', time()));
$headers = "From: info#xyz.com\r\nReply-To: info#xyz.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message = "Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
<strong>Candidate Name :</strong> ".$candidatename."<br>
$message .="--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment $attachment --_2_$boundary--";
mail($to,$subject,$message,$headers);
But I dont know why, in email I am only getting attachment, I am not getting candidate name. I mean html contents.
I am moving to PHPMailer
and I am using this code,
if (isset($_FILES['uploaded_file']) &&
$_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
for attachment.

sending file attachment via html form sends big junk of text to the email body

I am trying to make a html file upload to send via email with php. Here is the code snippet:
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$body="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
test
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail('email#example.com', 'Leidige stillinger', $body, $headers) or die("NO");
I get the email but with a junk of text, looks like the $boundary generates that big junk of text. Or I am doing this all wrong, first I have to upload the file somewhere in the server then send it via email
I've always preferred hand-rolling the MIME encoding as well, like you're doing, instead of using a library. You're close. Try this:
// to, from, subject, message body, attachment filename, etc.
$to = "to#to.com";
$from = "from#from.com";
$subject = "subject";
$message = "this is the message body";
$filename="/home/user/file.pdf"; //location of file - path and filename
$fname="file.jpeg"; //name of file for display purposes
$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 = "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";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$file = fopen($filename,"rb");
$data = fread($file,filesize($fname));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fname\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
$ok = #mail($to, $subject, $message, $headers, "-f " . $from);

Correct format for sending mime-encoded email using PHP's mail

What is the correct way to create an mime multipart email for sending with PHP's mail function?
$boundary = uniqid(rand(), true);
$headers = "From: <noreply#example.com>\r\n";
$headers .= "Reply-To: <noreply#example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=$boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n" .
"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($plaintext));
$headers .= "--$boundary\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($body)); // chunk_split adds the ending "\r\n"
$headers .= "--$boundary--\r\n";
//$headers .= ".\r\n";
mail($to, $subject, '', $headers);
I tried the above but PHP seems to ignore it and instead sends this empty email which I receive on the server:
...
...
To: <....>
Subject: Test Email
X-PHP-Originating-Script: 0:Mail.php
From: <noreply#example.com>
Reply-To: <noreply#example.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="1923630041519679ed1ddd25.14582240"
Message-ID: <20130517184149.28AB0A4C4#example.com>
Date: Fri, 17 May 2013 14:41:49 -0400
X-UI-Loop: V01:211ZxL2TMQ4=:uQC6SYy+5ULsBgI8/Yn6FAKnX8a66b5mzBQJFWhGo82c
3a8ZtvbDIKAYJ3vIkfg3
--1923630041519679ed1ddd25.14582240
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: base64
--1923630041519679ed1ddd25.14582240
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: base64
--1923630041519679ed1ddd25.14582240--
Tested on PHP 5.4.9.
I got the above code working. Seems I needed to replace all the windows \r\n with linux \n. I also removed the duplicate newlines after the boundary="...".

In PHP, how can I send an HTML email with an attachment?

I have a form that sends to myself and allows people to attach a file. But I can't seem to allow HTML in the message. I think I need two different Content-Type:
$to = "my#email.com"; // Webmaster
$subj = "Great Subject";
$message = strip_tags($_POST['message']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['attachment']['tmp_name'])));
$filename = $_FILES['attachment']['name'];
$boundary =md5(date('r', time()));
$headers = "From: $fname $lname <$email>\r\nReply-To: $fname $lname <$email>";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message .= "Here is your <strong>File</strong>.".\r\n" ;
$message .= "This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subj, $message, $headers);
Use a class such as PHPMailer, as it will make it simpler.

php send e-mail with attachment

I can't seem to find the problem with this php function i wrote that should send an e-mail with attachment. I've been struggling with it for quite a while.
function myMail($to, $subject, $mail_msg, $filename, $contentType){
$random_hash = md5(date('r', time()));
$headers = "From: webmaster#example.com\r\nReply-To: ".$to;
$headers .= "\r\nContent-Type: ".$contentType.
"; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
ob_start();
echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit
$mail_msg
--PHP-alt-$random_hash
--PHP-mixed-$random_hash--
Content-Type: text/plain; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
$mail_sent = #mail( $to, $subject, $message, $headers );
return $mail_sent ? "Mail sent" : "Mail failed";
}
Edit The problem is that the message of the mail is mixed with the file and send as an attachment.
I've just looked at a couple of my emails, and I notice the the final attachment boundary ends with '--', while the opening boundary marker does not. In your code, you have:
--PHP-mixed-$random_hash--
Content-Type: text/plain; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
Perhaps it should be:
--PHP-mixed-$random_hash
Content-Type: text/plain; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
Have a look at the example here:
http://en.wikipedia.org/wiki/MIME#Multipart_messages
Artefacto made me look at the output with more attention and i've found the fix:
function myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){
$random_hash = md5(date('r', time()));
$headers = "From: webmaster#mysite.com\r\nReply-To: ".$to;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($pathToFilename)));
ob_start();
echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit
$mail_msg
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: $contentType; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
$fh=fopen('log.txt','w');
fwrite($fh,$message);
$mail_sent = #mail( $to, $subject, $message, $headers );
return $mail_sent ? "Mail sent" : "Mail failed";
}
Unless you are doing this to learn about the internal workings of MIME mails, the standard answer is to use a mailer library like PHPMailer or Swiftmailer that can deal with attachments out of the box.
SwiftMailer Examples on how to attach files are here.
These are the headers I use and they have always worked like a charm.
$base = basename($_FILES['upload']['name']);
$file = fopen($randname_path,'rb');
$size = filesize($randname_path);
$data = fread($file,$size);
fclose($file);
$data = chunk_split(base64_encode($data));
//boundary
$div = "==Multipart_Boundary_x".md5(time())."x";
//headers
$head = "From: $from\n".
"MIME-Version: 1.0\n".
"Content-Type: multipart/mixed;\n".
" boundary=\"$div\"";
//message
$mess = "--$div\n".
"Content-Type: text/plain; charset=\"iso-8859-1\"\n".
"Content-Transfer-Encoding: 7bit\n\n".
"$message\n\n".
"--$div\n".
"Content-Type: application/octet-stream; name=\"$base\"\n".
"Content-Description: $base\n".
"Content-Disposition: attachment;\n".
" filename=\"$base\"; size=$size;\n".
"Content-Transfer-Encoding: base64\n\n".
"$data\n\n".
"--$div\n";
$return = "-f$from";
http://asdlog.com/Create_form_to_send_email_with_attachment

Categories