Click to See Complete Forum and Search --> : faulty e-mails


neumy
01-16-2004, 06:43 PM
I'm struggling with an e-mail problem. Here goes:

I've created an e-mail program that sends a pdf file to stated e-mail address. It took me a while until I got everything working perfectly. All my tests to Hotmail, Yahoo, and Outlook worked fine. In celebration I e-mail a few other people to test and they found a few problems. Some e-mail programs "explode" the pdf file and the message body displays code-like characters. Hotmail is continuing to be troublesome and works one day and not the other.

I so badly want to finish this function and desperately need some help. I'll be happy to show code or answer any other questions concerning my code. If someone can shed light on my mishap I'd be grateful. Thanx.

Pittimann
01-17-2004, 10:42 AM
Hi!

Could you please post your code for a check??

It seems, you're not attaching the file properly. Another question: is the .pdf on your local hdd or on a server?

Cheers - Pit

neumy
01-19-2004, 11:01 AM
Here's just my main mailing code:

if ($REQUEST_METHOD=="POST")
{
$id = $_POST['testid'];
$product = $_POST['product'];
if (assessment_auth($id,$product))
{
$result = func_query_first("SELECT score, productid, DATE_FORMAT(FROM_UNIXTIME(last_updated),'%M %e, %Y') AS completed, comments FROM $sql_tbl[scores] WHERE testid = '$id'");
$score= Unserialize($result['score']);
$showdate = $result['completed'];
$notes = $result['comments'];
$report = "This is a test!\nI don't know if this will work, but here is the results:\nSelf = ".$score['part1'][S]."\nFamily = ".$score['part1'][F]."\nPeers = ".$score['part1'][P]."\nWork = ".$score['part1'][W]."\nProjected Self = ".$score['part1'][J];

$email = $_POST['mail_to'];
$message = send_mail($email);
// need to check e-mail address
}
else {
header("Location: error_message.php?permission_denied");
}
}

function send_mail($mail_to)
{
if(empty($mail_to)) return "Please input who you'd like to e-mail this to";
$mail_to = str_replace(";",",",$mail_to);

$mail_parts["mail_to"] = $mail_to;
$mail_parts["mail_from"] = "\"The Report\" <report@blah.com>";
$mail_parts["mail_reply_to"] = "\"Report\" <info@blah.com>";
$mail_parts["mail_subject"] = "Here is the report you requested";
$mail_parts["mail_body"] = "Thank you for taking the assessment.";

if(my_mail($mail_parts)) return "Successfully sent report";
else return "An unknown error occurred while attempting to send report";
} // function send_mail()

function my_mail($mail_parts)
{
$mail_to = $mail_parts["mail_to"];
$mail_from = $mail_parts["mail_from"];
$mail_reply_to = $mail_parts["mail_reply_to"];
$mail_subject = $mail_parts["mail_subject"];
$mail_body = $mail_parts["mail_body"];

$mail_headers = "From: $mail_from\n";
$mail_headers .= "Reply-to: $mail_reply_to\n";

$mail_boundary = md5(uniqid(time()));
$mail_headers .= "MIME-Version: 1.0\r\n";
$mail_headers .= "Content-type: multipart/mixed; boundary=\"$mail_boundary\"\r\n\r\n";
$mail_headers .= "This is a multi-part message in MIME format.\r\n\r\n";

$report = createReport();
$file = BuildAttachment($report);

$new_body = "--$mail_boundary\r\n";
$new_body .= "Content-type:text/plain; charset=us-ascii\r\n";
$new_body .= "Content-transfer-encoding:7bit\r\n\r\n";
$new_body .= "$mail_body\r\n";
$new_body .= "--$mail_boundary\r\n";

$new_body .= "Content-type:application/pdf; name=report.pdf\r\n";
$new_body .= "Content-transfer-encoding:base64\r\n\r\n";
$new_body .= "$file\r\n\r\n";
$new_body .= "--$mail_boundary--";
$mail_body = $new_body;

return mail($mail_to,$mail_subject,$mail_body,$mail_headers);
} // function my_mail()

function BuildAttachment($string)
{
set_magic_quotes_runtime(0);
$FileContent = chunk_split(base64_encode($string));
$attachment = $FileContent;
$attachment .="\n\n";
set_magic_quotes_runtime(get_magic_quotes_gpc());
return $attachment;
} // function BuildAttachment()

function createReport()
{
include 'report.php';
return $report;
} // function createReport()

As for the .pdf file, it is dynamicly created while sending the e-mail based on data from the database etc. Remember, I do get this working nicely on some e-mail programs.

Pittimann
01-19-2004, 11:43 AM
Hi!

Sorry - don't have time for a real analysis. At first sight I would assume, that the prob is caused by "\r\n\" for the linefeeds.

My mailscripts (I have several ones for different purposes) use "\n" only and I never faced problems so far.

Of course that is machine specific, but maybe you should give it a try...

Cheers - Pit

neumy
01-19-2004, 01:27 PM
I ended up so frusterated with this code that I scraped it and found a mail program at SourceForge.net and reworked it until it worked with what I was doing.

I still don't know what I did wrong - I thought everything was perfect - in fact, I don't notice any difference with my new code except positive results.

Thanks for you help!