I am trying to create a script that inserts emails into a MySQL database and copies the attachments to a folder...
I've googled and scoured this forum 'til my fingers are sore.... still nothing helpful!
I know how to open an IMAP connection and get a list of the emails in the mailbox... but want I want to do is a little more complicated...
-Open the mailbox (as I said, I know how to do this)
-open the emails in the mailbox, save the from address, subject, timestamp and body to a MySQL database
-copy the attachment (if any) to a folder
-delete the email if the MySQL insert/file copy worked fine
-loop back and do this for any other emails that may be in the mailbox
can anyone shed some light?
from the research I have done, I would need to use imap_list, imap_body, imap_headerinfo, imap_delete and of course the usual MySQL INSERT query
this is my first time playing with IMAP, so any an explanation of any code, or further suggestions would be more than appreciated! =D
save IMAP emails to MySQL database
Moderator: General Moderators
Re: save IMAP emails to MySQL database
Okay, so I've worked out how to do most of this... except for the attachments bit!
I'll admit I cheated by adding in the refresh instead of doing all the emails in some description of array...
if anyone can help me with the attachments part, I will be eternally greatful!
here is my code for anyone interested:
I'll admit I cheated by adding in the refresh instead of doing all the emails in some description of array...
if anyone can help me with the attachments part, I will be eternally greatful!
here is my code for anyone interested:
Code: Select all
<?php
//begin by opening connection
$server = "{imap.whatever.com:143}";
$username = "email@whatever.com";
$password = "password";
$mbox = imap_open("$server", "$username", "$password") or die(imap_last_error());
$num = imap_num_msg($mbox);
if($num > 0) {
//get from, date and subject
$header = imap_headerinfo($mbox, 1);
$from = $header->from;
foreach($from as $id=>$object) {
$fromaddress = $object->mailbox . "@" . $object->host;
}
$subject = $header->subject;
$date = $header->date;
//read the body
$body = imap_body($mbox, $num);
//open MySQL connection
$db = mysql_connect("localhost", "user", "password") or die ('Unable to connect. Check your connection parameters.');
mysql_select_db(emails, $db) or die(mysql_error($db));
//save to MySQL
$query = "INSERT INTO emails (fromaddress, subject, date, body) VALUES ('$fromaddress', '$subject', '$date', '$body')";
mysql_query($query, $db) or die(mysql_error($db));
//delete email
imap_delete($mbox, 1);
imap_expunge($mbox);
header('refresh: 3');
} else {
//no emails, refresh again in 20 seconds to check for new
header('refresh: 20');
}
//close connection
imap_close($mbox);
?>Re: save IMAP emails to MySQL database
Thank you Tutigun for your valuable post.
It is great useful for me.
I found one mistake on your above code.
$body = imap_body($mbox, $num); //here the last message number
$header = imap_headerinfo($mbox, 1); //here the first message number
both should be kept same to get correct information of an email.
It is great useful for me.
I found one mistake on your above code.
$body = imap_body($mbox, $num); //here the last message number
$header = imap_headerinfo($mbox, 1); //here the first message number
both should be kept same to get correct information of an email.