homepage Welcome to WebmasterWorld Guest from 54.197.215.146
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP email with attachments output is blank
JAB Creations




msg:4617981
 8:14 pm on Oct 20, 2013 (gmt 0)

I'm trying to send two small images as email attachments. Unfortunately Yahoo Mail is coming up blank without attachments and AOL Mail is showing all the text below. I mimicked a working email so I'm not sure what I did wrong...

--397762125-123729819-1382094641=:59273
Content-Type: multipart/alternative; boundary="397762125-2116717784-1382094641=:59273"

--397762125-2116717784-1382094641=:59273
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Body text goes here.=A0- John Bilicki III
--397762125-2116717784-1382094641=:59273
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<p>1111111111111111</p>
--397762125-2116717784-1382094641=:59273--
--397762125-123729819-1382094641=:59273
Content-Type: image/gif; name="secret_afound.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="secret_afound.gif"

R0lGODlhDwAPAJEAAP8A/////wAAAAiXCCH5BAEAAAAALAAAAAAPAA8AAAI7
hC2Zx5AD4WIihAitOGJcnA2bA37R1mGWKXXsK1YWmGkuHV1PrPSSI5MFgiOH
ZhWkrJaahnBZpPQmhgIAOw==

--397762125-123729819-1382094641=:59273
Content-Type: image/gif; name="secret_aunlocked.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="secret_aunlocked.gif"

R0lGODlhDwAPAJEAAP8A/////wAAAP8AACH5BAEAAAAALAAAAAAPAA8AAAI7
hC2Zx5AD4WIihAitOGJcnA2bA37R1mGWKXXsK1YWmGkuHV1PrPSSI5MFgiOH
ZhWkrJaahnBZpPQmhgIAOw==

--397762125-123729819-1382094641=:59273--


...and here is my PHP code...

<?php
$headers = "From: someone@example.com\nReply-To: someone@example.com";
$headers .= "\n".'Content-Type: multipart/mixed; boundary="--397762125-123729819-1382094641=:59273"';
ob_start();
?>
--397762125-123729819-1382094641=:59273
Content-Type: multipart/alternative; boundary="397762125-2116717784-1382094641=:59273"

--397762125-2116717784-1382094641=:59273
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Body text goes here.=A0- John Bilicki III
--397762125-2116717784-1382094641=:59273
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<?php echo quoted_printable_encode(trim($_POST['post_body']));?>

--397762125-2116717784-1382094641=:59273--
--397762125-123729819-1382094641=:59273
<?php
$attachments = array();
$count4 = 0;
$i = 0;
$query4 = "SELECT data, filename, type FROM email_attachments WHERE id_email='0' AND (";
foreach ($_POST as $k => $v)
{
if (substr($k,0,15)=='post_attachment')
{
if ($v!='')
{
if ($i>0) {$query4 .= " OR filename='$v'";}
else {$query4 .= "filename='$v'";}
$count4++;
$i++;
}
}
}
$query4 .= ');';
$result4 = mysql_query($query4);

if ($result4)
{
$i = 1;
while ($row4 = mysql_fetch_assoc($result4))
{
?>
Content-Type: <?php echo $row4['type'];?>; name=<?php echo $row4['filename'];?>
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=<?php echo $row4['filename'];?>

<?php
echo wordwrap(base64_encode($row4['data']),'60',"\n",true)."\n";

if ($i<$count4)
{
?>

--397762125-123729819-1382094641=:59273
<?php
}
$i++;
}
?>

--397762125-123729819-1382094641=:59273--<?php
$message = ob_get_clean();
$mail_sent = @mail('someone@yahoo.com','Subject Test',$message,$headers);
echo $mail_sent ? "Mail sent" : "Mail failed";
echo "\r\n";
echo $message;
}
else {mysql_error_report($query4,mysql_error(),__FUNCTION__);}


die();
?>


I'm not interested in using frameworks or anything, I just want to figure out what the problem is and fix it. Thoughts please?

- John

 

penders




msg:4618206
 7:46 pm on Oct 21, 2013 (gmt 0)

Just a few comments... May not make a difference, but the headers would normally be CR+LF separated. They should only be LF separated if that is what's causing the problem.

You also seem to be missing the "MIME-Version" header. This is part of the spec, however, (some) email clients might cope without this.

Just a general comment regarding the use of the output buffer in this instance (or using HEREDOC strings for that matter). The EOL might be dependent on the OS of the server you are using (depending on how you are transferring your files). Move the script from Linux/Windows and the script output could be different. Generally with email generation the line endings are important, so this could be significant.

In order to debug the formatting/boundaries you'll need to echo out the entire email before it's sent.

Readie




msg:4618416
 6:07 pm on Oct 22, 2013 (gmt 0)

Generally with email generation the line endings are important, so this could be significant.

There's a constant you can use - PHP_EOL. Much safer than using ob or heredoc as Penders pointed out.

There's a case I recall from a job I worked a number of years ago - the type of line break that was being used to build the mail headers was causing a html email to be displayed as plain text in Outlook.

penders




msg:4618432
 6:39 pm on Oct 22, 2013 (gmt 0)

There's a constant you can use - PHP_EOL.


Unfortunately PHP_EOL could also be a problem as the value of this constant is dependent on the OS of the server. The line endings need to be fixed and configurable regardless of the server on which the script is installed. (Generally CR+LF for mail headers and LF only for the mail body.)

Readie




msg:4618486
 10:03 pm on Oct 22, 2013 (gmt 0)

Yes... Perhaps better to explicitly define them I guess with something as touchy as Email.

JAB Creations




msg:4619727
 5:59 am on Oct 29, 2013 (gmt 0)

I had the boundaries off. Adding the -- before the string and -- before and after the ending boundaries worked. Thanks for the input!

- John

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved