Jump to content

outlook getting .dat files instead of the right attachment...


njdubois

Recommended Posts

I have an email form on a website I created.  Add attachment, send the message to gmail, or hotmail.  no problems. 

 

Send it to outlook, and we get the .dat file problem.

 

I sent the same message to my roommate at work, who is a computer admin, outlook 2010, fully updated and so on and everything works like it should.

 

I could bet my life's worth of paychecks that the people having this issue are using an old, outdated version of outlook.  As a matter of fact I get all sorts of grief from my clients, clients. Its not working Its not working and its ALWAYS something being outdated ... I swear the tech's they have working for them.... UGH

 

Anyways....

 

I did some research and I found that the problem has something to do with sending emails as Rich Text, and that i should change it to HTML.  The problem is that I already am sending out as HTML, and that the email attachments are working just fine in a newer versions of outlook.

 

So here is my code:

 

//create 2 boundary strings. They must be unique
$boundary1 = rand(0,9)."-".rand(10000000000,9999999999)."-".rand(10000000000,9999999999)."=:".rand(10000,99999);
$boundary2 = rand(0,9)."-".rand(10000000000,9999999999)."-".rand(10000000000,9999999999)."=:".rand(10000,99999);
$fileContent =  chunk_split(base64_encode(file_get_contents($path)));


$html_message = $email_message;

$headers     =<<<AKAM
From: $from
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="$boundary1"
AKAM;

$attachment = <<<ATTA
--$boundary1
Content-Type: application/octet-stream;
    name=$path
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename=$path_name

$fileContent

ATTA;

$body = <<<AKAM
This is a multi-part message in MIME format.

--$boundary1
Content-Type: multipart/alternative;
    boundary="$boundary2"


--$boundary2
Content-Type: text/html;
    charset=ISO-8859-1;
Content-Transfer-Encoding: 7bit

$html_message

--$boundary2--

$attachment
--$boundary1--
AKAM;

//send the email
$mail_sent = @mail($to, $email_subject, $body, $headers);

 

Sorry if the code is all over the place, it took me a while to find a working example and to modify the script to send emails with attachments.  Everytime I would try to format the code things would stop working and I finally said "Fine, if this chunk of code is going to look like ____ than so be it!"

 

But as you can see, it already is set as HTML.

 

So my question is, how do I get the above chunk of code to send an email with attachments that will work in all clients?  Including older versions of outlook?

 

Thanks, as always I appreciate the assistance!

Nick

 

Link to comment
Share on other sites

Outlook will do that when it has trouble understanding the MIME data. I'll give you a few suggestions, but don't throw away the code you already have until we get this working. I have never actually run into this problem, but I've been reading through the RFC's lately.

 

1) You have referenced $path and $path_name in the code. Do both of these variables exist? or is one of them a typo.

 

2) In the MIME data, when you specify the filename, be sure you are only supplying a filename (and extension). Do not supply the directory names. You cannot control the directory on the client, so there is no sense in specifying them. I mention this, because you use $path with file_get_contents() and that one should have the directory path.

 

3) I don't think you need the second boundary. You specify this part of the message as "multi-part/alternative", but you are not supplying any alternative to the HTML. If you are going to put a plain text message in there as well, then OK; but if HTML is the only format you are providing, then I don't think you need the second boundary: take out the "alternative" content-type header and boundary 2, then move the HTML content headers up below the boundary 1.

 

4) Make sure that your HTML message does NOT have any NON-ASCII text in it. Since you specified the encoding as 7-bit, you need to use 7-bit. If you need UTF-8 (or some other multi-byte character set) then you will need to encode the attachment in some way or use htmlentities() on the content only (not on the HTML tags).

 

5) Make sure you use CRLF (\r\n) as the line ending. In emails, the line ending should be CRLF. Your here-docs are probably using LF ("\n") alone.

 

6) Make sure your HTML message is broken up into lines (with CRLF, if possible). The mail specification says lines should not be longer than 78 characters (plus the CRLF line-ending) and must not be longer than 998 characters (plus the CRLF).

 

7) Put double-quotes around the filenames in the content headers

 

So, I think your message body should look like this:

This is a multi-part message in MIME format.

--$boundary1
Content-Type: text/html; charset=ISO-8859-1;
Content-Transfer-Encoding: 7bit

$html_message
--$boundary1
Content-Type: application/octet-stream; name="$path"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$path_name"

$fileContent
--$boundary1--

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.