Pass File via mail() PHP - php

I have a Problem, I would like to send a File (Photoshop, .gif etc.) File via Email with PHP.
I POST the Data from the HTML Document to the following PHP File.
The Problem: I receive the Email but the File is corrupt.
Any Idea why this doesn´t work??
$to = 'admin#example.com';
$subject = 'Order';
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$plz = strip_tags($_POST['plz']);
$city = strip_tags($_POST['city']);
$street = strip_tags($_POST['street']);
$nr = strip_tags($_POST['nr']);
$plzdelivery = strip_tags($_POST['plz-delivery']);
$citydelivery = strip_tags($_POST['city-delivery']);
$streetdelivery = strip_tags($_POST['street-delivery']);
$nrdelivery = strip_tags($_POST['nr-delivery']);
$buttonsize = strip_tags($_POST['button-size']);
$count = strip_tags($_POST['count']);
$message = "Name: ".$name." Email: ".$email." Plz: ".$plz." Stadt: ".$city." Strasse: ".$street." Hnr: ".$nr." Button: ".$buttonsize." Anzahl: ".$count;
$type = $_FILES['file']['type'];
$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\"";
$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, $subject, $message, $headers);

Short answer: Don't even try.
PHP's mail() function is very very short on features. If you're even considering trying to send attachments using it, you are in for a lot of frustration and wasted time.
The best answer I can give you is to use a third party library. There are several good ones available. Why waste weeks of your time writing something that isn't going to be good enough when others have already spent years doing it for you and getting it right.
My suggestion would be phpMailer, but several others exist as well.

You should install the pear Mail_Mime package, it will allow you to easily attach a file.
http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

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);
?>

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.

Empty message when sending zip file with PHP

**Update: Updated the code and added the email contents as seen in Gmail. **
I have a problem which I've been trying to solve a couple of days. But since I have been unable to make any progress, I'd like the help of someone else. I'm trying to send a zip file from my server to my email. However, the email is being sent empty. This is the code:
`$headers = "From: $from";
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\"";
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"
Attached, please find your backup file.
--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";`
Thanks to all reading and wanting to help.
The resulting email as seen in Gmail is
Attached, please find your backup file.
--PHP-mixed-e44b531b0e0538185289abc521eda78d
Content-Type: application/zip; name=backup-29-11-2014-1417275285-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
UEsDBBQAAAAIAFZ8fUUs...sUEsFBgAAAAACAAIAeAAAAD4B
AAAAAA==
--PHP-mixed-e44b531b0e0538185289abc521eda78d--
I got it working by doing the following changes:
Removed space character in From: headers, it's an invalid header otherwise
Changed boundary: to boundary=
Removed intendation in $message because that seems to break the email. The boundary string --PHP-mixed-random separates the different parts of the email, like plain text, html text, attachments from each other. When there are spaces in front of the boundary separator, it is not interpreted as the separator anymore, but as normal text.
Here's the code:
<?php
$from = "xxx#myserver";
$email = "xxx#gmail.com";
$subject = "backup file";
$date = "29-11-2014";
$time = "1417275285"; // test file "backup-29-11-2014-1417275285-9.zip" in the same folder
$headers = "From: $from"; // remove space
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\""; // : to =
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"
Attached, please find your backup file.
--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";

Sending an image to email via PHP

can you please help me with this code. i am trying to embed a image in my mail and its showing as string of alphanumeric ..
function send_mail_pear()
{
$crlf = "\n";
$to = "mail#mail.in";
$head = ("From: mail#mail.in". "\r\n");
$head .= "Content-type: text/html\r\n";
$head .= "Content-Type: image/jpeg";
$mime = new Mail_mime($crlf);
echo $head. "<br/>";
$mime->setHTMLBody('<html><body><img src="map_6.gif"> <p>You see this image.</p></body></html>');
$mime->addHTMLImage('map_6.gif', 'image/gif');
$subject = "Test mail";
$mailBody = $mime->get();
$mail =& Mail::factory('mail');
echo $mailBody ;
/*if(mail($to, $subject, $mailBody, $head)){
echo "successful";
} else {
echo "Mail Not sent";
}
}
I believe you have to link to the image as "cid:yourcontentid", and specify "yourcontentid" as a parameter to addHTMLImage.
You can consult the documentation for more information.
you can just attach it using. or i suggest the least painful option will be to host the image on some server and send an HTML email.
the body will look like
<html>
<p>
<img src='http://yourserver.com/YOURIMAGE.gif' />
</p>
</html>
EDIT:
to attach images or files to an email try something like this
$to = "someome#anadress.com";
$from = "SOME NAME ";
$subject = "SUBJECT";
$fileatt = "./FILENAME.gif";
$fileatttype = "image/gif";
$fileattname = "ATTACHMENTNAME.gif";
$headers = "From: $from";
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x";
$headers .= <<<HEADER
MIME-Version: 1.0\n
Content-Type: multipart/mixed;\n
boundary="{$mime_boundary}"
HEADER;
$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
\n\n
–{$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
MESSAGE;
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
The answer by Matthias Vance is part of the story.
If you want to send a self-contained HTML mail, then it needs to be sent as an mhtml type (NB the content type for this varies on different implementations - it should be multipart/related but frequently is message/rfc822). There is however a formal definition of the structure in RFC 2557. Each of the components should be a seperate attachment. This also means that each URL has to be rewritten to use a local reference and the cid scheme - IIRC, the PEAR class does not do this for you.
There is some documentation on the net e.g. this one - but do not fall into the trap of believing this is suitable for anything other than sending emails / saving web pages locally - MSIE does not handle mhtml files delivered over HTTP correctly.
I'm not aware of any off the shelf package for generating mhtml files / emails - but there are some for parsing such files. Writing a tool to do this would be straightforward but not trivial.

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