For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2006 > Mail With Attachment Problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Mail With Attachment Problem
Tim McGurk

2006-12-13, 4:09 pm

I'm trying to send an e-mail with an attachment to Send2Fax, which is a
service that takes an e-mail with an attachment and sends it to a designated
fax number

The code I have works fine if I'm sending the attachment to a mailbox, but
when sending to Send2Fax what ends up happening is the attachment is
apparently being read in as a text file, and instead of, say, a .jpg being
faxed the output is 7 or 8 pages of gibberish. Send2Fax provides code, but
not for sending an attachment (and they DO accept attachments- it works fine
from an e-mail client like Outlook)

Here's the code I'm using (found it on the internet). Any help would be
GREATLY appreciated!

function mail_attachment($file,$mailto, $from_mail, $from_name, $replyto,
$subject, $message)
{
$filename=basename($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;
boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream;
name=\"".$filename."\"\r\n"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;
filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header))
{
return true;
}
else
{
return false;
}
}


Ric

2006-12-13, 4:09 pm

Tim McGurk schrieb:
> I'm trying to send an e-mail with an attachment to Send2Fax, which is a
> service that takes an e-mail with an attachment and sends it to a designated
> fax number
>
> The code I have works fine if I'm sending the attachment to a mailbox, but
> when sending to Send2Fax what ends up happening is the attachment is
> apparently being read in as a text file, and instead of, say, a .jpg being
> faxed the output is 7 or 8 pages of gibberish. Send2Fax provides code, but
> not for sending an attachment (and they DO accept attachments- it works fine
> from an e-mail client like Outlook)
>
> Here's the code I'm using (found it on the internet). Any help would be
> GREATLY appreciated!
>
> function mail_attachment($file,$mailto, $from_mail, $from_name, $replyto,
> $subject, $message)
> {
> $filename=basename($file);
> $file_size = filesize($file);
> $handle = fopen($file, "r");
> $content = fread($handle, $file_size);
> fclose($handle);
> $content = chunk_split(base64_encode($content));
> $uid = md5(uniqid(time()));
> $name = basename($file);
> $header = "From: ".$from_name." <".$from_mail.">\r\n";


Mail standard is only "\r", some email clients don't care if you send
\r\n but lots of providers and apps do and then the mail gets broken.
So remove the \n from all your header tags.

> $header .= "Reply-To: ".$replyto."\r\n";
> $header .= "MIME-Version: 1.0\r\n";
> $header .= "Content-Type: multipart/mixed;
> boundary=\"".$uid."\"\r\n\r\n";
> $header .= "This is a multi-part message in MIME format.\r\n";
> $header .= "--".$uid."\r\n";
> $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
> $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
> $header .= $message."\r\n\r\n";
> $header .= "--".$uid."\r\n";
> $header .= "Content-Type: application/octet-stream;
> name=\"".$filename."\"\r\n"; // use diff. tyoes here
> $header .= "Content-Transfer-Encoding: base64\r\n";
> $header .= "Content-Disposition: attachment;
> filename=\"".$filename."\"\r\n\r\n";
> $header .= $content."\r\n\r\n";
> $header .= "--".$uid."--";
> if (mail($mailto, $subject, "", $header))
> {
> return true;
> }
> else
> {
> return false;
> }
> }
>
>

Michael Fesser

2006-12-13, 4:09 pm

..oO(Ric)

>Mail standard is only "\r"


Mail standard is "\r\n" (RFC 2822).

>some email clients don't care if you send
>\r\n but lots of providers and apps do and then the mail gets broken.


It's a bit more complicated. What you send is not necessarily what the
client will receive. The RFC describes the mail format as it should be
handled by MTAs, but some of them have their own mind and may rewrite
line endings. Sometimes all you can do is testing.

The RFC-compliant way for example works on my host's server, but not on
my own (or the other way round, can't remember), where a different MTA
is running. Using CRLF there led to an additional character appended to
each of my own header lines.

>So remove the \n from all your header tags.


I would rather remove the \r. From the PHP manual:

| Note: If messages are not received, try using a LF (\n) only. Some
| poor quality Unix mail transfer agents replace LF by CRLF
| automatically (which leads to doubling CR if CRLF is used). This
| should be a last resort, as it does not comply with RFC 2822.

Micha
Tim McGurk

2006-12-13, 4:09 pm

Thank you both for your help.

Removing '\r' did the trick!

"Michael Fesser" <netizen@gmx.de> wrote in message
news:2cj0o25ne2ju10sv70sa1hg33k099e9a1p@
4ax.com...
> .oO(Ric)
>
>
> Mail standard is "\r\n" (RFC 2822).
>
>
> It's a bit more complicated. What you send is not necessarily what the
> client will receive. The RFC describes the mail format as it should be
> handled by MTAs, but some of them have their own mind and may rewrite
> line endings. Sometimes all you can do is testing.
>
> The RFC-compliant way for example works on my host's server, but not on
> my own (or the other way round, can't remember), where a different MTA
> is running. Using CRLF there led to an additional character appended to
> each of my own header lines.
>
>
> I would rather remove the \r. From the PHP manual:
>
> | Note: If messages are not received, try using a LF (\n) only. Some
> | poor quality Unix mail transfer agents replace LF by CRLF
> | automatically (which leads to doubling CR if CRLF is used). This
> | should be a last resort, as it does not comply with RFC 2822.
>
> Micha



Ric

2006-12-14, 3:59 am

Michael Fesser schrieb:
> .oO(Ric)
>
>
> Mail standard is "\r\n" (RFC 2822).

Uuups you are right!
>
>
> It's a bit more complicated. What you send is not necessarily what the
> client will receive. The RFC describes the mail format as it should be
> handled by MTAs, but some of them have their own mind and may rewrite
> line endings. Sometimes all you can do is testing.
>
> The RFC-compliant way for example works on my host's server, but not on
> my own (or the other way round, can't remember), where a different MTA
> is running. Using CRLF there led to an additional character appended to
> each of my own header lines.
>



Hmmm here I need to send with \r if not some mail clients only show the
html source when they receive the autogenerated html mail, since I'm
using \r I haven't had any complaints.

>
> I would rather remove the \r. From the PHP manual:
>
> | Note: If messages are not received, try using a LF (\n) only. Some
> | poor quality Unix mail transfer agents replace LF by CRLF
> | automatically (which leads to doubling CR if CRLF is used). This
> | should be a last resort, as it does not comply with RFC 2822.
>
> Micha

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com