Welcome to WebmasterWorld Guest from 23.20.137.66

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP email with attachments output is blank

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

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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
7:46 pm on Oct 21, 2013 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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.
6:07 pm on Oct 22, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
6:39 pm on Oct 22, 2013 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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.)
10:03 pm on Oct 22, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Yes... Perhaps better to explicitly define them I guess with something as touchy as Email.
5:59 am on Oct 29, 2013 (gmt 0)

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

- John
 

Featured Threads

Hot Threads This Week

Hot Threads This Month