Select Mailbox "Sent Mail" or "All Mail" in Ruby Net::Imap

Select mailbox sent mail or all mail in Ruby Net::IMAP

The "sent mail" folder will differ from provider to provider. Gmail's "sent mail" folder is named "[Gmail]/Sent Mail". Select that instead and it'll work.

imap.select('[Gmail]/Sent Mail')

FYI, Gmail's system folders are the following:

  • INBOX
  • [Gmail]/All Mail
  • [Gmail]/Drafts
  • [Gmail]/Sent Mail
  • [Gmail]/Spam
  • [Gmail]/Starred
  • [Gmail]/Trash

Ruby/Rails: Obtain list of all available imap folders

This should work, what you can try is to use * instead of %.

% is a wildcard for everything besides hierarchical characters.
* also includes hierarchical characters.

You should get back an array of IMAP::MailboxList

You can also try:

imap.list('*', '*')  to see a list of folders.

The code provided by you should work, however different mail providers have different implementations, so knowing which one you are using, might help figuring this out.

How to read the body text of an email using ruby's net/imap library?

If you just want just the body content of the message you can use:

body = imap.fetch(message_id,'BODY[TEXT]')[0].attr['BODY[TEXT]']

The IMAP API is a bit esoteric though. If you want to deal with the whole message, I would recommend using TMail to parse it into an easier to use format:

msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
mail = TMail::Mail.parse(msg)
body = mail.body

IMAP Selecting multi-worded mailboxes

RFC 3501 does specify that, the first line of page 87 specifies that astring syntax is to be used. Astring is used for a lot of things in IMAP, and in this case it's just:

0004 SELECT "New label"

Do read the astring/string/nstring/literal productions in the syntax, you have to get them right in order to parse or generate IMAP beyond the hello-world stage. Or use a library if you want to outsource it, there are fine libraries on C#.

Gmail: Sync Inbox (IMAP & POP3 disabled)

Solution

In order to avoid the usage of sockets for this assignment I've tried to build an Chrome extension/apps, but this would become as riduculous as using sockets.

Thanks god Google Apps Script exists.

Code



var VENDOR_NAME_INDEX = 0;
var CONTACTS_INDEX = VENDOR_NAME_INDEX + 1;
var HAS_REPLIED_INDEX = CONTACTS_INDEX + 1;
var COMMENTS_INDEX = HAS_REPLIED_INDEX + 1;

var VENDORS_SPREADSHEET_URL = "https://docs.google.com/a/jabil.com/spreadsheet/ccc?key=0Aj7MWNXx-gzsdHJQbGhQZUVkMjBFeVNZX0dXdDZjYWc#gid=0";
var VENDORS_GMAIL_QUERY = 'in:sent has:attachment after:2013/06/26 before:2013/06/28 subject: "*Jabil CUU /// Shipping Letter*"';

// http://flesler.blogspot.mx/2008/11/fast-trim-function-for-javascript.html
String.prototype.trimLeft = function() {
var pivot = -1;
while (this.charCodeAt(++pivot) < 33);
return this.slice(pivot, this.length);
}

// http://flesler.blogspot.mx/2008/11/fast-trim-function-for-javascript.html
String.prototype.trimRight = function() {
var pivot = this.length;
while (this.charCodeAt(pivot--) < 33);
return this.slice(0, pivot);
}

vendors = [];
function processSentItems() {
var threads = GmailApp.search(VENDORS_GMAIL_QUERY);
var ss = SpreadsheetApp.openByUrl(VENDORS_SPREADSHEET_URL);

var data = ss.getDataRange();
var rows = data.getNumRows();
var values = data.getValues();

for (var i = 0; i < rows; ++i) {
var row = values[i];

var vendor = row[VENDOR_NAME_INDEX];
var contacts = row[CONTACTS_INDEX];
var replied_back = row[HAS_REPLIED_INDEX];
var comments = row[COMMENTS_INDEX];

vendors.push({
name: vendor,
contacts: contacts,
replied_back: replied_back,
comments: comments
});

}

threads.forEach(function(thread) {
var id = thread.getId();
var subject = thread.getFirstMessageSubject();
var vendorName = subject.split("-")[0];

vendorName = vendorName.trimLeft();
vendorName = vendorName.trimRight();

var vendor = getVendorByName(vendorName);

if (typeof vendor == "object") {
if (vendor.replied_back.toLowerCase() != "yes") {
thread.replyAll("Friendly reminder...")
}
}
});

};

function getVendorByName(vendorName) {
for (var i = 0; i < vendors.length; ++i) {
if (vendors[i].name == vendorName) {
return vendors[i];
}
}
};


Related Topics



Leave a reply



Submit