Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gmail/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ class AuthenticationError(GmailException):

class Timeout(GmailException):
"""The request timed out."""

class MessageFormatError(GmailException):
"""Improper message format"""
37 changes: 25 additions & 12 deletions gmail/gmail.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
import imaplib

import smtplib
from mailbox import Mailbox
from utf import encode as encode_utf7, decode as decode_utf7
from exceptions import *
Expand All @@ -25,7 +25,8 @@ def __init__(self):
self.logged_in = False
self.mailboxes = {}
self.current_mailbox = None

#Change self.debug to True for SMTP debugging
self.debug = False

# self.connect()

Expand All @@ -39,15 +40,16 @@ def connect(self, raise_errors=True):
# self.imap = None

self.imap = imaplib.IMAP4_SSL(self.GMAIL_IMAP_HOST, self.GMAIL_IMAP_PORT)

# self.smtp = smtplib.SMTP(self.server,self.port)
# self.smtp.set_debuglevel(self.debug)
# self.smtp.ehlo()
# self.smtp.starttls()
# self.smtp.ehlo()

self.smtp_connect()

return self.imap

def smtp_connect(self):
self.smtp = smtplib.SMTP(self.GMAIL_SMTP_HOST,self.GMAIL_SMTP_PORT)
self.smtp.set_debuglevel(self.debug)
self.smtp.ehlo()
self.smtp.starttls()
self.smtp.ehlo()

def fetch_mailboxes(self):
response, mailbox_list = self.imap.list()
Expand Down Expand Up @@ -104,12 +106,23 @@ def login(self, username, password):
self.fetch_mailboxes()
except imaplib.IMAP4.error:
raise AuthenticationError


# smtp_login(username, password)

self.smtp_login(username, password)

return self.logged_in

def smtp_login(self, username, password):
self.username = username
self.password = password

if not self.smtp:
self.smtp_connect()

try:
self.smtp.login(self.username,self.password)
except SMTPAuthenticationError,e:
raise

def authenticate(self, username, access_token):
self.username = username
self.access_token = access_token
Expand Down
51 changes: 50 additions & 1 deletion gmail/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
from email.header import decode_header, make_header
from imaplib import ParseFlags
from email.utils import formatdate, make_msgid
from email.mime.text import MIMEText

class Message():

Expand All @@ -26,6 +28,9 @@ def __init__(self, mailbox, uid):
self.cc = None
self.delivered_to = None

#to_addrs variable for sending mail
self.to_addrs = []

self.sent_at = None

self.flags = []
Expand All @@ -37,6 +42,9 @@ def __init__(self, mailbox, uid):

self.attachments = None

#Message options and recepient options
self.msg_options = []
self.rcpt_options = []


def is_read(self):
Expand Down Expand Up @@ -212,7 +220,48 @@ def fetch_thread(self):
# combine and sort sent and received messages
return sorted(dict(received_messages.items() + sent_messages.items()).values(), key=lambda m: m.sent_at)


def send(self):
if not self.gmail.logged_in:
self.gmail.login(self.username, self.password)
if self.fr is None:
self.fr = self.username
if self.message_id is None:
self.message_id = make_msgid()
if self.to is None:
raise MessageFormatError("To: field is not defined")

self.sent_at = formatdate(time.time(),localtime=True)
self.to_addrs = self.to
if self.cc is not None:
self.to_addrs = self.to_addrs + ", " + self.cc
self.create_message()
print "sending message\n\n%s" %self.message.as_string()
self.gmail.smtp.sendmail(self.fr, self.to_addrs, self.message.as_string(), self.msg_options, self.rcpt_options)

def create_message(self):
"""Can create only text messages as of now"""
self.message = MIMEText(self.body)
self.message['to'] = self.to_addrs
self.message['from'] = self.fr

if self.subject is not None:
self.message['subject'] = self.subject

self.message['date'] = self.sent_at

def reply(self, msg_text):
new_msg = Message(self.mailbox, self.uid)
new_msg.to = self.fr
if len(self.to.split(',')) > 1:
new_msg.to = new_msg.to + ", " + self.to.split(',')[1:]
new_msg.fr = self.to.split(',')[0]
new_msg.cc = self.cc
new_msg.subject = self.subject
new_msg.body = msg_text
#send the message and then return the new message object for further communication
new_msg.send()
return new_msg

class Attachment:

def __init__(self, attachment):
Expand Down