mailbox always empty when checking emails via imap - ruby-on-rails

Our platform is regularly checking some of our clients mailbox looking for answered emails, so far it's been working perfectly but we recently got a client with a strange issue.
When i connect to the IMAP server and search for email on a given period of time (for instance all mails from yesterday), i just get an empty list. But when i check with the client on its mailbox i can see the mail received from yesterday. Her mail client was configured to use POP so i asked her to change to IMAP as POP is supposed to remove email from the mail server. Now she is using IMAP but i still can not see anything on the mail server.
To connect to the IMAP server i am using those instructions:
imap = Net::IMAP.new(server, port, ssl, nil, false) # OK
imap.login(login, pwd) # Authentification Successful
imap.select("INBOX") # Mailbox is OK
To look for email from yesterday i am using those criteria:
imap.search(["SEEN","SINCE","Sep-07-2015"]
imap.search(["SEEN","SINCE","Sep-07-2015"]
I even tried to look into other mailbox in case her mail didn't arrive on Inbox, i have listed her mailboxes with imap.list('', '') even so i can not find any mails. It feels her entire mailbox is empty on the server. Yet i can see her emails on the mail client.
I am at loss on this case and was wondering if anyone has an idea of what could be happening preventing me from discovering any emails via IMAP?

It seems you have incorrect date format: it should be similar to 8-Aug-2002 and not Aug-8-2002. Look here for other options. If that doesn't help, try some easier search with a more broader criteria, something like imap.search(['SUBJECT', 'hello']) and use instead of hello some real subject.
This article also may help.
Cheers!

you can use this syntaxe in php , example for yahoo
$inbox='{imap.mail.yahoo.com:993/imap/ssl/novalidate-cert}Inbox';
$mbox = imap_open($inbox,$username,$password);
imap_search($mbox, 'SINCE "'.date('d M Y',strtotime("-1 days")).'"');

Related

Receive, Store, Interact with emails rails application

With my rails application, I'm supposed to provide following features:
There a limited number of users interacting with my system (in order of 10 to 20)
Like any normal mail client users should be able to have an inbox page showing received message, response to individual email and etc....
The mail client part cannot be an external application, they want everything packaged into a single application!
Normally These emails should be stored for future use
In order to send a receive email, we do not need to setup a mail server. They will provide the server and we will fetch the message with POP3 or something else. Same goes for sending emails.
The application itself often needs to look into these message as well, so it should be able to access corresponding email objects.
Separate part of these applications can be handled with individual gems such as Mailman, ActionMailer, and etc...
But what would be your suggestions to get this done?
I suggestion customizing an open source solution according to your needs. This is a gem/project that you should look at https://github.com/mailboxer/mailboxer It has all the features that you mentioned and its straightforward in its customizations.

Running a PHP script on email arrival in an IMAP Server

I'm trying to implement a webmail in PHP. I would like to write a PHP CLI script which is run on every email arrival to store some parts of (not all of) incoming email into database for search purposes. Then when the user finished searching and chose an email to show, a connection is made to mail server to retrieve the complete email. In order to implement this scenario I need to make some sort of connection among emails within database and mail server.
Since my knowledge of working with mail servers is limited to Zend Framework's API, what I believe I need in order to retrieve an email from an IMAP server is a message number or a message unique id (this later one seems not to be supported by all mail servers).
To this point, I've managed to find .forward (and some other ways) to introduce my PHP CLI script to MTAs to be run on every email arrival. This way I can store emails to database. But this won't do since message unique id is created by MDA so MTA do not know of it and they can not provide it to me. This means I can not find emails later when I want to retrieve them from mail server.
At last, here's my question: Is there a way to introduce a PHP CLI script to a MDA for emails' arrival? If this is dependent on the mail server, which servers do support this and how? My personal choice would be Dovecot or Courier, but any other mail server would do as well.
This is tricky -- there are many ways on how to setup delivery. Some of them work with the underlying mail store directly, bypassing your IMAP server altogether, while others use e.g. Dovecot's facilities.
Have you considered building on top of the notify plugin which ships with Dovecot?
It seems like it's impossible to introduce such a PHP CLI script to IMAP server (at least I'm sure of Dovecot). Anyway, the work around I found for this problem is to use my own PHP script to insert the new mails into IMAP server and retrieve their id's and then store the id in database for future references. To be clear, email are given to my PHP CLI script by MTA, not MDA. As I said before this is done easily using .forward file.
[UPDATE]
Unfortunately it seems this solution can not be implemented as well. The way to insert a new email to IMAP server is APPEND command, and to have the UID of the recently added mail server must support UIDPLUS extension. Neither Dovecot nor Courier supports this extension at the moment! If they did it seems the server would return the UID with a APPENDUID response.
[UPDATE]
It is my bad since Courier does support UIDPLUS. So this solution is valid and the one I'm going to implement.

Processing all the email sent to a specific domain

Requirement:
I am writing a web application (Rails on Heroku) through which users can create groups and user should be able to post a message to the group simply by sending an email to the group.
This is what tumblr.com does: each blog is associated with an email address(randomly generated) and user can post to the blog simply by sending an email. Also posterous.com has this feature.
Question:
What is the best way to architect a solution like this one? Comments? Ideas?
I see 2 ways of doing this:
1) Hosting my own email server (sendmail or postfix) on Amazon EC2 and having some script to process all the incoming email?
This will give me a lot of control but an email server to maintain.
2) Have the email server hosted somewhere and just have to write the email processing script would be nice however I do not know of any email cloud service to which you can tell: "please accept all the email for mydomain.com".
Thanks in advance for any help.
I think I am going to go with http://cloudmailin.com. They even have a nice Heroku plug-in. It would be nice to hear any good or bad experience from somebody that tried this out.
You could have the emails sent to GMail through their SMTP servers and run some sort of crontab to pull down the emails from GMail, and process them from there.
They do allow you to have emails sent to your domain.com, see this page:
http://www.google.com/apps/intl/en/group/index.html

reaching Gmail SMTP daily limit

In one of my Rails applications I'm sending emails through the Gmail SMTP server and everything just works, mails are not going to spam and so on ... But there's one thing that concerns me, the 500messages/day limit the SMTP has, currently I'm over 350/day. I didn't find any official Google page where they talk about the subject, just blog posts that seems to be reliable. Then my question is what do you suggest me in order to be able to send more than 500messages/day? I would love to keep using the Gmail SMTP.
Any help would be appreciated.
Don't use GMail for what it wasn't built for. It wasn't designed as a mass-email system, although Google definitely has the firepower to do this.
Instead, perhaps use something like SendGrid to send your emails. SendGrid is designed for this and is just as easy (actually, probably easier) to set up with than GMail.
You can consider using more than one gmail account to access the smtp server, however you may have problems with ip limits (couldn't find anything on their docs about it). Another option is performing dns mx lookups yourself and reproducing your own smtp server by directly delivering the messages to the user's emails - but that can increase the odds of you being tagged as a spammer as your ip is not whitelisted as sender.
I think the best way is to create another gmail account and trying to reroute your connection to use it when one of the accounts reaches the daily limit. A vpn connection can solve that for you.

Receive mail in Ruby/Rails

How would i go about to receive mails in a Ruby on Rails application without going through a mail server like PostFix or to fetch them by pop3 etc.
What i was to do is to catch all mails sent to #mydomain.com and just do something with them in my application. I don't need to store the mails or anything like that.
Is this posible?
I just implemented this for my SAAS to autoprocess mailer-bounce notification messages.
Call me, call you?
You call me
You can set up a local mail server. It would then respond to an incoming email, and start up a rails executable to process the email. This method is NOT recommended since starting up rails is a big task (takes multiple secs and lots of memory). You don't want a Rails bad boy started up just because you received an email. You'd be writing your own DDOS attack. (Attacking yourself.)
I call you
Instead, poll for email on your own schedule by using a single job to process all currently waiting emails. You need to set up a background job handler since stock rails is focused on responding to web requests. I use delayed_job, but there are other alternatives including kicking off a cron job every so in often.
Another benefit is that you don't need to manage a mail server. Leave that headache to someone else. Then use the Ruby library net::imap to read the incoming mail and process it.
If your process doesn't recognize the email format, then forward the msg to a human for processing.
And be sure that if the process sends mail in addition to reading/processing it, that the process uses a different email address as its From address. Otherwise, odds are good that sometime along the way, you'll end up in an email loop and many gigabytes of messages going back and forth. For example, your process receives a message, responds to it, but in the meantime the sender (a human) has switched on vacation response. And your robot then responds to the vacation response..... oops....
Writing your own mail server
Re:
How would i go about to receive mails in a Ruby on Rails application without going through a mail server like PostFix or to fetch them by pop3 etc.
What i was to do is to catch all mails sent to #mydomain.com and just do something with them in my application. I don't need to store the mails or anything like that.
Direct answer: Yes, you could do this by writing an smtp server and setting up dns so your machine will be the mail destination for the domain. Your smtp server would process the messages on the fly, they would not be stored on your system at any point.
Is this a good idea? No, not at all. While appearances may be to the contrary, email is a store and forward system. Trying to avoid storing the messages before your app processes them is not smart. It would be a very very poor "optimization." However, using an access protocol (POP3 or IMAP) is a good way to avoid the costs of installing, configuring and managing a mail server.
You can do this if you write your own mail server, or if your mail server supports hooks to run external programs upon receipt of mail (e.g. procmail).
If you don't have procmail available (or, if on something like Exchange Server, don't feel like writing custom rules or extensions), you're simply better off using a pop3 library to fetch mail.
Obviously, writing a mail server is more difficult than any of the alternatives.
If you're mostly worried about checking potentially hundreds of email accounts, that's solvable by configuring your email server properly. If you're on a hosted provider, ask your server administrator about creating a "catch-all" account that routes all mail to unknown addresses to a single account.
If you're aiming to avoid having to poll a server, consider the IMAP IDLE command. I've successfully written a Ruby client that opens a connection to an IMAP server, and gets told by the server when new mail arrives.

Resources