Folks, I'm new to PHP and having a little trouble understanding how to save attachments from a email. The following script works great for saving to the database. I just need to add a section to save attachments to disk. I have searched for the past few days. I have found plenty of examples. I guess I just don't understand PHP well enough.
Basically I want to run a daily cron job, grab all the emails with ### in the subject. Insert that information into a database. Then grab the file attachments and enter the name of the files into the database and save them to disk. the first part is working.
Can some please demonstrate this with the code below.
PHP Version 5.3.10 running on IIS 6 windows 2003
<?php
$save_msg_to_db = TRUE; // Save e-mail body to DB?
$db_host = 'somename';
$db_un = 'sa';
$db_pass = 'somepassword';
/* Authenticate to Mailserver */
$mailserver = "{smtp.something.com:143}";
$mailuser = "something";
$mailpass = "something";
/* Open Mailbox Stream */
$mbox = imap_open($mailserver, $mailuser, $mailpass) or die ("Couldn't connect to $mailserver");
/* Search For Message Criteria See: http://us.php.net/manual/en/function.imap-search.php */
$crit = "SUBJECT \"###\"";
$header = imap_search($mbox, $crit);
/* Return Results Of Search */
foreach ($header as $val) {
$id="";
/* Retrieve Body Of Message */
$body = imap_fetchbody($mbox, $val, 1 , FT_PEEK);
/* Retrieve Details From Header Of Message */
// [subject] [from] [to] [date] [message_id] [size] [uid] [msgno] [recent] [flagged] [answered] [deleted [seen] [draft]
$v = imap_fetch_overview($mbox, $val);
foreach ($v as $ov) {
$strsubject = $ov->subject; // subject
$strfrom = $ov->from; // from
$strto = $ov->to; // to
$strdate = $ov->date; // date
$messageid = $ov->message_id; // Message ID
}
$JOBNUM = str_replace(' ', '', $strsubject);
$JOBNUM = substr(ltrim($JOBNUM),3,6);
// SAVE OR PARSE THESE VARIABLES INTO MySQL
//$b = body;
//$s = subject;
//$f = from;
//$t = to;
// $d = date;
//echo "found 1<br>";
echo "<br>From: " . $strfrom . "<br>To: " . $strto . "<br>Date: " . $strdate . "<br>". "Subject: " . $strsubject . "<br>";
echo "Job Number: " . $JOBNUM. "<br>";
echo "MessageID: " . $messageid. "<br><br>";
echo nl2br($body)."<br><br>";
echo "---------------------------------------------------------------------------------------------------<br>";
echo "---------------------------------------------------------------------------------------------------<br>";
echo "<br><br>";
//imap_delete($mbox, $val); // delete the e-mail
//DO DB insert here and save Files to Disk
$conn = odbc_connect($db_host,$db_un,$db_pass);
//mysql_select_db($db_name);
$body = str_replace("'",''',$body);
$body = str_replace("\"",'"',$body);
$body = str_replace("\\",'\',$body);
$strsubject = str_replace("'",''',$strsubject);
$strsubject = str_replace("\"",'"',$strsubject);
$strsubject = str_replace("\\",'\',$strsubject);
$stmt = odbc_prepare($conn, "INSERT INTO tblEmail (MailFrom, Subject, Body, JOBNUM, EMAILID) VALUES('" . $strfrom . "','" . $strsubject . "','" . $body . "','" .$JOBNUM. "','" . $messageid . "');");
//$q = "INSERT INTO tblEmails (MailFrom, Subject) VALUES ('test','test')";
if (!odbc_execute($stmt)) {
/* error */
echo "Whoops";
}
odbc_close ($conn);
imap_delete($mbox, $val); // delete the e-mail
}
/* Delete Messages */
imap_expunge($mbox);
/* Close Mailbox Stream */
imap_close($mbox);
?>