Sending email with file attached
Posted: Fri Dec 13, 2002 6:36 pm
I am trying to send an email with a small attachment (15 KB). I was wondering if anyone had a script that would do this. I am running PHP 4.1.2.
The idea is the user enters there email & the serial number they need their "rectification data" for and I email it to them. In the background I get the desired file via ftp, point it to a file pointer locally send it out to them as a string, then remove it from my server. Right now I can retrieve the file, send the email, but the attachment doesn't exist. I have tried to echo the file out, but the pointer returns nothing. If I look in the temp file, there is data, but it seems as if the temp file pointer association is lost. Any ideas?
Here is a the section of the code I've got. Shoot holes in it.
Thanks,
Jacob
The idea is the user enters there email & the serial number they need their "rectification data" for and I email it to them. In the background I get the desired file via ftp, point it to a file pointer locally send it out to them as a string, then remove it from my server. Right now I can retrieve the file, send the email, but the attachment doesn't exist. I have tried to echo the file out, but the pointer returns nothing. If I look in the temp file, there is data, but it seems as if the temp file pointer association is lost. Any ideas?
Here is a the section of the code I've got. Shoot holes in it.
Code: Select all
// Read sensor serial number and to locate correct directory
$rectdatadir1 = $serialnumberї0];
$rectdatadir2 = $serialnumberї1];
// Create directory string
$rectdatadldir = "rectdata/SN".$rectdatadir1."".$rectdatadir2."XXX";
// Log into ftp server
$connrect = ftp_connect($rectserv);
$rectlogin = ftp_login($connrect, $servuser, $servpass);
if ($rectlogin == FALSE) // Ftp Connection Error
{
$eh->show("2003");
}
else
{
// Changing file location directory
ftp_chdir($connrect,$rectdatadldir);
// Assigning file name to "get"
$rectservfile = "SN".$serialnumber.".DT";
// Assigning tempfile on local server
$rectdata = tempnam("/tmp","rectfile");
// Retrieving file from ftp site
$ftp_status = ftp_get($connrect,$rectdata,$rectservfile, FTP_BINARY);
ftp_quit($connrect);
if ($ftp_status == FALSE) // Does Rectification file exist
{
$eh->show("2004");
echo "<p>".$serialnumber." "._MD_RECTDATASERIALNUMBER_FAIL."</p>\n";
}
else
{
// Setting up email strings
$rectemailsubj = "Sensor Serial Number's ".$serialnumber." rectification data";
// Reading file into file pointer string to send via email
$fp = fopen("/tmp/".$rectdata);
$rectemail_file = fread($fp, filesize("/tmp/".$rectdata));
fclose($fp);
// echo "$rectemail_file"; //debug line to see output, right now I see nothing if I comment the rest out
$rectemail_status = mail($rectemail,$rectemailsubj,$rectemail_file);
if ($rectemail_status == FALSE) //email address is faulty
{
$eh->show("2005");
echo "<p>"._MD_RECTDATAEMAIL_FAIL."</p>\n";
}
else
{
// Delete file from Local client
unlink($rectdata);
// The rest is a email & sensor number recognition page
include(XOOPS_ROOT_PATH."/header.php");
OpenTable();
mainheader();
echo "<table width="80%" cellspacing=0 cellpadding=1 border=0>\n";
echo "<tr>\n";
echo "<td>\n";
echo "<br><br>\n";
echo "<p>"._MD_RECTDATASENTTO1." ".$serialnumber." "._MD_RECTDATASENTTO2." ".$rectemail."</p>";
echo "</td>\n";
echo "</tr>\n";
echo "<form action="rectdata.php" method=post>\n";
echo "<tr>\n";
echo "<td>\n";
echo "<center>";
echo "<input type=button class='button' value='"._MD_RECTDATAMORE."' onclick="location='rectdata.php'" /></input>\n";
echo " ";
// echo "<input type=button class='button' value='"._MD_RECTDATADONE."' onclick="location='.$xoopsConfigї'startpage']'" /></input>\n";
echo "</center>\n";
echo "</td>\n";
echo "</tr>\n";
echo "</form>\n";
echo "</table>\n";
CloseTable();
}
}Jacob