php send e-mail with attachment - php

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

Related

PHP mail with attachment of unknown file type

I want to add send mail in my website. user can attach some files with multiple types in email. So i wrote this code to send mail. the problem is file type is not correct. for example i attached a jpg file to send to email. but the attachment received in mail box is empty or is wrong type.
this is my php mail file:
<?php
//Deal with the email
$sender = $_POST['email'];
$to = 'info#matinjewellery.com';
$subject = $_POST['name'];
$message = strip_tags($_POST['message']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: ".$sender."\r\nReply-To: ".$to;
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$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=\"utf-8\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/zip; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
?>

PHP - sending email with attachment does not show message content

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

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.

PHP Email with attachement

Am facing a problem to sent a PHP email with an excel attachment. I can see the mail sent message, Mail is coming, but there is no attachment. I dint know the real problem. this is my code. I have put the User.xlsx file in my root directory. Please help me to solve this issue. Thanks
$to = "dibeesh#amt.in";
$subject="attachement";
$mail_msg="message with attach";
$filename="User.xlsx"; // Attachement file in root directory
$contentType="application/zip"; // Not sure about what to put here
$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 );
echo $mail_sent ? "Mail sent" : "Mail failed";
I have managed to debug my code, sorry for duplicate posting. It may help someone thanks
// Email to Client with attachement
//define the receiver of the email
$to = 'dibeesh#amt.in';
//define the subject of the email
$subject = 'Bayern3 Notification';
$filename = "User.xlsx";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: lal#amt.in\r\nReply-To: dibeesh#amt.in";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
//define the body of the message.
ob_start(); //Turn on output buffering
echo "--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
--PHP-alt-$random_hash
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<h4>Hi,</h4>
<p><b>$userfullname</b> and his 10 friends qualified for <b>$venuename</b>. Please find the Excel sheet attached. You can download the detailed report form Bayern3 Admin Panel</p>
<h4>Regards,</h4>
<h4>Bayern3 Team</h4>
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/zip; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
//echo $mail_sent ? "Mail sent" : "Mail failed";
if($mail_sent === True)
{
//echo "Mail Sent";
}
else{
//echo "Mail Failed";
}

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.

Categories