Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive replies from Gmail with smtplib - Python

Ok, I am working on a type of system so that I can start operations on my computer with sms messages. I can get it to send the initial message:

import smtplib  

fromAdd = 'GmailFrom'  
toAdd  = 'SMSTo'  
msg = 'Options \nH - Help \nT - Terminal'  

username = 'GMail'  
password = 'Pass'  

server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username , password)  
server.sendmail(fromAdd , toAdd , msg)  
server.quit()

I just need to know how to wait for the reply or pull the reply from Gmail itself, then store it in a variable for later functions.

like image 723
Roger Parish III Avatar asked Aug 09 '13 22:08

Roger Parish III


People also ask

Does Smtplib work with Gmail?

The SMTP Protocol This may not come as a surprise, but of course Python already has a library that lets you connect to an SMTP server, like the one Gmail uses. This library is called, predictably, smtplib and comes included with Python.

How do I use Smtplib in Python 3?

Python provides smtplib module, which defines an SMTP client session object that can be used to send mails to any Internet machine with an SMTP or ESMTP listener daemon. host − This is the host running your SMTP server. You can specifiy IP address of the host or a domain name like tutorialspoint.com.

Does Smtplib come with Python?

Python comes with the built-in smtplib module for sending emails using the Simple Mail Transfer Protocol (SMTP). smtplib uses the RFC 821 protocol for SMTP.

What does Smtplib SMTP do?

The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (SMTP Service Extensions).


3 Answers

Instead of SMTP which is used for sending emails, you should use either POP3 or IMAP (the latter is preferable). Example of using SMTP (the code is not mine, see the url below for more info):

import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', 'mypassword')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

Shamelessly stolen from here

like image 126
Uku Loskit Avatar answered Oct 22 '22 11:10

Uku Loskit


Uku's answer looks reasonable. However, as a pragmatist, I'm going to answer a question you didn't ask, and suggest a nicer IMAP and SMTP library.

I haven't used these myself in anything other then side projects so you'll need to do your own evaluation, but both are much nicer to use.

IMAP https://github.com/martinrusev/imbox

SMTP: http://tomekwojcik.github.io/envelopes/

like image 1
Liyan Chang Avatar answered Oct 22 '22 09:10

Liyan Chang


I can suggest you to use this new lib https://github.com/charlierguo/gmail

A Pythonic interface to Google's GMail, with all the tools you'll need. Search, read and send multipart emails, archive, mark as read/unread, delete emails, and manage labels.

Usage

from gmail import Gmail

g = Gmail()
g.login(username, password)

#get all emails
mails = g.inbox().mail() 
# or if you just want your unread mails
mails = g.inbox().mail(unread=True, from="[email protected]")

g.logout()
like image 1
Dragu Avatar answered Oct 22 '22 09:10

Dragu