barbushin / php-imap Goto Github PK
View Code? Open in Web Editor NEWManage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
License: MIT License
Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
License: MIT License
I have placed this code in a Codeigniter project. It has been working good but I have run across one issue that I am not sure how to fix. I have one message that is not HTML it is plain text and the body of the message is getting dropped. I would like to fix but I am not well versed in all the parameters and suck with mail & imap. But what is happening is Line 455 -> 489 pull the body and place it correctly in the $data variable.
After that line $459 shows as having an attachementid. Once it goes into there the $data is not assigned to anything to be returned. I see that lines 520 -> 527 is the only place that the $data is assigned to be returned.
I guess my question is, is there an error in the class or is there an error in the mail message? Should I place an assignment in lines 455 -> 489? Wondering why there is not one there now.
Thanks very much for your help.
Lib gets some mails attachments and damage their.
I decided the problem by commenting line:
if(!empty($params['charset'])) {
$data = $this->convertStringEncoding($data, $params['charset'], $this->serverEncoding);
}
in method "initMailPart".
I think in this method only text parts must be converted
Consider this scenario:
A) I send email to [email protected]
B) He replies to my mail
Now in that email that i received using your class i have in $email->textPlain:
Hello this is a reply from [email protected]
Lorem ipsum.
On 2015/0723/ dejan7 said:
> this is the first email
> i sent to [email protected]
Is there any way to get only contents of the message that [email protected] wrote i.e.:
Hello this is a reply from [email protected]
Lorem ipsum.
?
Is it somehow stored in email parts? The problems is that different clients mark the old emails differently. Some use > and some use } etc. etc. Also some include the line "On 2015/0723/ dejan7 said:" in various formats or don't include it at all!
Great class BTW, it works great, i'm just wondering if this is somehow possible. Thanks!
WIth the below code i want o fetch all the unseen email .. but with your code only one email fetching.. No all.. But i want all unseen.
Why $mail[$mailId] = $mailbox->getMail($mailId); is not working for all Unseen email.
after Print only one email it is redirecting automatically.
$mailsIds = $mailbox->searchMailBox('UNSEEN');
if(!$mailsIds) {
die('Mailbox is empty');
}
if($mailsIds) {
$output = '';
rsort($mailsIds);
$count = 1;
foreach($mailsIds as $mailId) {
$mail[$mailId] = $mailbox->getMail($mailId);
}
print_r($mail);
}
Hi,
The current regex in getInternalLinksPlaceholders doesn't work with links similar to
="cid:[email protected]"
I had to change the line to :
return preg_match_all('/=["\'](cid:([\w\.%*@-]+))["\']/i', $this->textHtml, $matches) ? array_combine($matches[2], $matches[1]) : array();
This will add the @ . - % and * characters to the list of accepted characters in the internal links.
@ and . are commonly used, % is for hex encoding the special chars, * seems to be used in http://tools.ietf.org/html/rfc2392
Not really sure if you can have the - but well, won't hurt. ;-)
I removed the ? in cid (didn't understand why there was one).
This sill won't work with "mid:" links, but I didn't have any in my mails to test it.
Thanks for the quick fixes.
I got a problem displaying emails which have chinese characters. For example I have an email with english and chinese characters in my body. The body output for plain and html is parsed until the first chinese char appears.
Hi
I would like to use this library, but I need IMAP IDLE / NOTIFY as described in http://tools.ietf.org/html/rfc5465.
What effort would it be to implement this?
Mark
PHP 5.4.4-14+deb7u8 (cli) (built: Feb 17 2014 09:18:47)
While getting mail from yandex.ru
[NOTICE] Undefined property: stdClass::$to File: ../vendor/php-imap/php-imap/src/ImapMailbox.php Line: 356
[WARNING] Invalid argument supplied for foreach() File: ../vendor/php-imap/php-imap/src/ImapMailbox.php Line: 356
Hello, well i gat attachment with looong name , how add only the name of the attachement exemple ๐
[email protected]_FA048A7CCqsdqsdqsdqs1490B4C44F0hello.fr.html
so hwo rename the files correctlly please ?
like only the ID mail or something less pratical to use with shell script ?
thanks
Currently, when I use your library, I retrieve the message HTML and it returns all of the message contents from every member of the thread. I'm trying to retrieve the messages in a thread and have the contents separated by individual email (for example, how Gmail handles there email threads). Does your library handle this? If not, how do I accomplish this task?
Hello, sorry but i do'nt understand how works the search function.
i want search on the object the work "Aword" so i use this syntax ๐
but nothing found by the :
...
foreach($mailbox->searchMailBox('OBJECT "Aword"') as $mailId)
..
but i have no result.
how i can search a word on the object of the mail please ?
like "%Aword%" or Aword
thanks to u
Hi, It's possible to archive e-mail in Gmail? I want to get all e-mails and after prcoess them I want archive them - so they will be not in Inbox.
hello..
just want to ask if there's a way to move mails to a folder?
Hello,
well i have this error do u know what's the problem is please ?
PHP Notice: Kerberos error: Credentials cache file '/tmp/krb5cc_1000' not found (try running kinit) for serv1.domaine.com in /var/www/imap/Imap7/lib/ImapMailbox.php on line 54
line 54 is that on the ImapMailmbox.php ๐
/*
* CLose IMAP connection
*/
public function disconnect() {
if ($this->mbox) {
$this->expungeDeletedMessages();
$errors = imap_errors();
if ($errors) {
foreach ($errors as $error) {
trigger_error($error);
}
}
imap_close($this->mbox);
$this->mbox = null;
}
}
what do u think is it please ?
After i read an email i get my attachment on my folder.. the first time is okey but when i open the same email a second time a new attachment is generated (the same with different id).
Im having a major issue with the IncomingMail class containing blank/null attributes, such as subject,textHtml,textPlain, and to/from addresses. The code works fine on some accounts, and then on others (such as when I import from AOL) it doesn't ever retrieve a message.
Any ideas?
/**
* Save mail body.
* @return bool
*/
public function saveMail($mailId, $filename = 'email.eml') {
return imap_savebody($this->getImapStream(), $filename, $mailId, "", FT_UID);
}
Is there a way to retrieve messages without touching the SEEN flag?
Or do you have to use markMailAsUnread() on each message id?
Hello! I get some mails with date like "Thu, 16 Apr 2015 09:15:28 +0400 (GMT-4)".
strtotime() can't parse this date, therefore $mail->date equal "false"
I've solved this issue by adding:
if(preg_match('/(.*)\(.*?\)$/i', $head->date, $a))
$head->date = $a['1'];
before
$mail->date = date('Y-m-d H:i:s', isset($head->date) ? strtotime($head->date) : time());
in method "getMail"
Everything is working great. The only problem I have is that the images aren't being displayed when they have "cid". How do I resolve this?
Destructor isnt working
Hello i have this error ervry time how i can fix it please :
'''Notice: iconv(): Detected an illegal character in input string in /var/www/Pr/lib/ImapMailbox.php on line 426 Notice: iconv(): Detected an illegal character in input string in /var/www/Pr/lib/ImapMailbox.php on line 426 Notice: iconv(): Detected an illegal character in input string in /var/www/Pr/lib/ImapMailbox.php on line 426
'''
Regarding the attachment saving to a folder, every time a email is retrieved it will save this file. Maybe change the naming scheme so it will either for the file originally before saving a new one? How about just using the UID of the email and name of the attachment?
Depending on the thoughts of this I can help out, also thoughts on saving attachments to AWS S3? Dependencies would be the official amazon SDK but that's it.
Installing this onto my project, I found that I had to add this line at the top of Mailbox.php
require('IncomingMail.php');
Else I get this error
Fatal error: Class 'PhpImap\IncomingMail' not found in C:\wamp\www\hydra\Resources\php-imap\src\PhpImap\Mailbox.php on line 411
I did not use composer to install php-imap, so that may be the cause of this. Otherwise, please explain why I needed to add this line where it seems nobody has needed to.
Hi,
Let me ask a silly question in here. I can't find a function to fetch a message by id from "imapMailbox.php". (or it does exist but I'm too dump to know). I'm very new to php but very need to apply this class into my web email. Let me summarize my questions:
Regards,
I have found that certain emails return the textPlain and textHtml as null, but the actual text is in an attachment, as a .plain file
I've got around this by checking for a .plain attachment and if its found, get the string from it, and put it back into the email object.
I would imagine you're going to ask me to send you an email that this happens on, but I cannot as its a confidential email I'm afraid. Just thought you should know.
can i do auto reply via imap
What do you think about adopt PSR standards ?
https://github.com/php-fig/fig-standards
I can help to change these things.
Hello, following the example file.
$mailBox = new ImapMailbox('{pop.afilie.com.br:995/pop3/ssl/novalidate-cert}INBOX', '[email protected]', 'lordbr01', dirname(__FILE__) . '/attch', 'utf-8');
$mails = array();
$mailsIds = $mailBox->searchMailBox("UNSEEN");
if(!$mailsIds) {
die('Mailbox is empty');
}
var_dump($mailsIds);
Gives me all emailsId, but I just need UNSEEN e-mails.
I'm using RoundCube to read emails, and I thing that the flags don't match.
Thanks in advance.
Hey, if i try to connect to a Microsoft Exchange 2003 IMAP Mailbox the connection crashes with the following error: "Specified character set not supported. (errflg=2)"
If i try another imap mailbox, it works correctly. Maybe a charset check is needed?
Hi,
The method initMailPart tries to convert all attachments as soon as a charset is defined.
However, the charset will sometimes be defined for attachments other than text (happened to me with .xlsx attachments).
The iconv line should be changed to convert only text parts :
if(!empty($params['charset']) && $partStructure->type === 0) {
$data = iconv($params['charset'], $this->serverEncoding, $data);
}
Sorry, I'm opening a lot of bug reports, I should have just compiled everything and made a push request but my company coding policy uses spaces instead of tabs so the merge would show the entire file as different for you... ;-)
Hi,
The RFC822 messages are not correctly parsed.
Please see :
http://www.php.net/manual/fr/function.imap-fetchbody.php#46041
It should be fixed in initMailPart function with something like :
if(!empty($partStructure->parts)) {
foreach($partStructure->parts as $subPartNum => $subPartStructure) {
if($partStructure->type == 2 && $partStructure->subtype == "RFC822")
{
$this->initMailPart($mail, $subPartStructure, $partNum);
}
else
{
$this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1));
}
}
}
At line 166, this:
if(!empty($partStruct->dparametersx)) {
Should be changed to this:
if(!empty($partStruct->dparameters)) {
I had a problem not getting any attachments from MDaemon Mail Server emails and found that the problem is this line causing the $params
not getting filled ...
how to read formatted messages discussion?
Once I hold a message, how do I find messages in response to the father?
thanks
PHP displays notice when IMAP server requires Kerberos tickets, even if it fallbacks to classical credentials (login/password).
PHP Notice: Unknown: Kerberos error: No credentials cache found (try running kinit) for imap.example.com (errflg=1) in Unknown on line 0
Please, add a function to delete the attachment files after fetching the mail.
I don't understand why i have this error :
Call to undefined function PhpImap\imap_search() in /var/www/htdocs/vendor/php-imap/php-imap/src/PhpImap/Mailbox.php on line 176
the stdClass is ignored
I want to use a secondary interface with a different IP when making requests to the IMAP server. How do I accomplish that?
Works great but if I get forwarded message it saves the HTML message body to attachment folder. If an actual attachment with html will be attached also it couldn't be possible to undertand what is attahcment and what is message body.
the repository not working ihave the message:
Sorry, this tree took too long to generate.
why ?
I wanted to use the script to transfer images I sent via my iphone.
In these images exif-data is stored (e.g. gps information and the date when the picture has been made).
The problem, the attachent saved by the script does not seem to contain any readable exif data any more. Any clue?
Hello, can u have a exemple code to get attached files ?
because i wan get csv files and work with data on csv, but how i can get the csv attached files please ?
Hey,
Great code! Thanks for sharing, I'm using it to import a few hundred thousand emails into a mysql database for a custom CRM tool, and your code relieves many headaches.
During testing on emails (sent from gmail to a domain account), messages with multiple inline image attachments only get the first image attachment saved, and it is renamed to "image.jpeg".
Any suggestions?
Hi,
Thank you for this imap class. So far it has been very useful.
However, I have found an issue while parsing mails from Yahoo which took me a while to understand.
The imap_search() charset you are using by default is "utf-8" but for some reason the yahoo imap stream expect the charset to be uppercase "UTF-8". When using imap_seach() on a yahoo stream with lowercase utf-8 the result is false while with uppercase we get the expected IDs back.
Here is a quick sample:
$sevrer = "{imap.mail.yahoo.com:993/imap/ssl}INBOX";
$username = "";
$password = "";
// open connection
$imapStream = imap_open($sevrer, $username, $password);
// check connection
if (! $imapStream) {
throw new Exception('Connection error: ' . imap_last_error());
}
$criteria = "SINCE 01-Jan-2015";
var_dump(imap_search($imapStream, $criteria, SE_UID, 'utf-8')); // false
var_dump(imap_search($imapStream, $criteria, SE_UID, 'UTF-8')); // array of message numbers or UIDs
// close connection
if ($imapStream && is_resource($imapStream)) {
imap_close($imapStream, CL_EXPUNGE);
}
Cheers
Hello,
i m trying to creat a function on the class to move a mail, but offcourse not working :
my fonction i have add it on the class ImaMailBox.php ๐
''''
public function moveMail ($mailId,$nameDossier)
{
echo "Mail ID = ".$mailId;
echo "nameDossier = ".$nameDossier;
$new_path = $this->imapPath."/".$nameDossier;
echo "new_path = ".$new_path;
if(imap_mail_move($this->mbox,$mailId,$new_path)==false){return imap_last_error();}
undeleteMessage($mailId) ;
expungeDeletedMessages();
}
'''
but i have the error message when i lunche it ๐
[TRYCREATE] The requested item could not be found.
i m sure the directory exist.
Someone have a suggestion please.
Hello, my problem i m trying to get only attachement on desirate mail.
but he get all attachement.
exemple this code ๐
require_once('libs/ImapMailbox.php');
define('ATTACHMENTS_DIR', dirname(FILE) . '/attachments/');
define('EMAIL_site_1', '[email protected]);
define('PASSWORD_site_1', 'pass');
$mailbox = new ImapMailbox('{web.Fr/imap/ssl/novalidate-cert/notls}INBOX', EMAIL_site_1, PASSWORD_site_1,ATTACHMENTS_DIR, 'utf-8');
$mails = array();
$mails = $mailbox->searchMailbox() ;
foreach ($mails as $key => $mailId) {
$mail = $mailbox->getMail($mailId);
//var_dump($mail);
}
will gett all attachement mail : i will an html files and the attachement
so i have tried to make a selection :
require_once('libs/ImapMailbox.php');
define('ATTACHMENTS_DIR', dirname(FILE) . '/attachments/');
define('EMAIL_site_1', '[email protected]);
define('PASSWORD_site_1', 'pass');
$mailbox = new ImapMailbox('{web.Fr/imap/ssl/novalidate-cert/notls}INBOX', EMAIL_site_1, PASSWORD_site_1,ATTACHMENTS_DIR, 'utf-8');
$mails = array();
$mails = $mailbox->searchMailbox() ;
foreach ($mails as $key => $mailId) {
$mail = $mailbox->getMail($mailId);
//var_dump($mail);
if (trim($mail->fromAddress) == "[email protected]") {
echo "Date : ".$mail->date; echo "\n";
echo "subject : ".$mail->subject;echo "\n";
echo "fromAddress : ".$mail->fromAddress;echo "\n";
$list = $mail->getAttachments();
$filePath_CorpMail = "";
foreach ($list as $key => $value) {
if (preg_match("#.txt#",$value->name))
$filePath_CorpMail = $value->filePath;
}
echo "Lien du fichier : ".$filePath_CorpMail;
}
}
so how get only attachement on desired mail please ?
I suppose that you forget to add this function to last update. At now it does nothing.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.