18 SENDING EMAIL AND TEXT MESSAGES Flashcards
- What is the protocol for sending email? For checking and receiving email?
Send: SMTP
- Simple Mail Transfer Protocol -
Receive: IMAP
- Internet Message Access Protocol -
Most web-based email providers protect against smtp/imap usage to prevent spam etc.
- What 7 smtplib functions/methods must you call to log in to an SMTP server and send a mail?
- import smtplib
- smtpObj = smtplib.SMTP(‘smtp.example.com’, 587)
- smtpObj.ehlo()
- smtpObj.starttls()
- smtpObj.login(‘bob@example.com’, ‘MY_SECRET_PASSWORD’)
- smtpObj.sendmail(‘bob@example.com’, ‘alice@example.com’, ‘Subject: ….\n…’)
{} - smtpObj.quit()
- What 3 imapclient functions/methods must you call to log in to an IMAP server?
- import imapclient
- imapObj = imapclient.IMAPClient(‘imap.example.com’, ssl=True)
- imapObj.login(‘my_email_address@example.com’, ‘MY_SECRET_PASSWORD’)
- What kind of argument do you pass to imapObj.search()?
> > > imapObj.select_folder(‘INBOX’, readonly=True)
> > > UIDs = imapObj.search([‘SearchKey’])
> > > UIDs
SearchKey Examples:
-BEFORE/ ON/ SINCE 05-Jul-2019
-ALL
…
- What do you do if your code gets an error message that says got more than 10000 bytes?
Increase the max byte size
> > > imaplib._MAXLINE = 10000000
- The imapclient module handles connecting to an IMAP server and finding emails. What is one module that handles reading the emails that imapclient collects?
- Create Message Object, 2.use PYZMAIL
- rawMessages = imapObj.fetch(UIDs, [‘BODY[]’])
2.
»> import pyzmail
»> message = pyzmail.PyzMessage.factory(rawMessages[UIDs][b’BODY[]’])
UIDs can be 40032 or whatever is found in the inbox
- When using the Gmail API, what are the credentials.json and token.json files?
Credential:
Contains Client Secret Info + Client ID - treat it like your password!
-> place in same folder as .py file
Token:
Will be generated to give script access to Gmail account. If not found, browser will be opened for Gmail-login
Both are required to login/send/receive mails without entering password in the source code.
- a. How can you see a quick Gmal summary of email threads?
b. In the Gmail API, what’s the difference between “thread” and “message” objects?
a.
»> ezgmail.summary(unreadThreads)
b. unreadThreads[0].messages[0].messageobject #see below
Thread:
consists of GmailMessage objects
MessageObjects:
- subject
- body
- timestamp
- sender
- recipient
- Using ezgmail.search(), how can you find emails that have file attachments?
> > > resultThreads = ezgmail.search(‘RoboCop’)
Enter GmailThread objects inte the search function, e.g.:
- ‘has:attachment’
- ‘from:al@inventwithpython.com’
- ‘subject:hello’
Full List:
https://support.google.com/mail/answer/7190?hl=en/.
- What three pieces of information do you need from Twilio before you can send text messages?
- phone number to use as the sender of text messages
- account SID
- auth (authentication) token
- Imap Search Keys
‘ALL’:
Returns all messages in the folder. You may run into imaplib size limits if you request all the messages in a large folder. See “Size Limits” on page 429.
‘BEFORE date’, ‘ON date’, ‘SINCE date’:
These three search keys return, respectively, messages that were received by the IMAP server before, on, or after the given date. The date must be formatted like 05-Jul-2019. Also, while ‘SINCE 05-Jul-2019’ will match messages on and after July 5, ‘BEFORE 05-Jul-2019’ will match only messages before July 5 but not on July 5 itself.
‘SUBJECT string’, ‘BODY string’, ‘TEXT string’:
Returns messages where string is found in the subject, body, or either, respectively. If string has spaces in it, then enclose it with double quotes: ‘TEXT “search with spaces”’.
‘FROM string’, ‘TO string’, ‘CC string’, ‘BCC string’:
Returns all messages where string is found in the “from” email address, “to” addresses, “cc” (carbon copy) addresses, or “bcc” (blind carbon copy) addresses, respectively. If there are multiple email addresses in string, then separate them with spaces and enclose them all with double quotes: ‘CC “firstcc@example.com secondcc@example.com”’.
‘SEEN’, ‘UNSEEN’:
Returns all messages with and without the \Seen flag, respectively. An email obtains the \Seen flag if it has been accessed with a fetch() method call (described later) or if it is clicked when you’re checking your email in an email program or web browser. It’s more common to say the email has been “read” rather than “seen,” but they mean the same thing.
‘ANSWERED’, ‘UNANSWERED’:
Returns all messages with and without the \Answered flag, respectively. A message obtains the \Answered flag when it is replied to.
‘DELETED’, ‘UNDELETED’:
Returns all messages with and without the \Deleted flag, respectively. Email messages deleted with the delete_messages() method are given the \Deleted flag but are not permanently deleted until the expunge() method is called (see “Deleting Emails” on page 432). Note that some email providers automatically expunge emails.
‘DRAFT’, ‘UNDRAFT’:
Returns all messages with and without the \Draft flag, respectively. Draft messages are usually kept in a separate Drafts folder rather than in the INBOX folder.
‘FLAGGED’, ‘UNFLAGGED’:
Returns all messages with and without the \Flagged flag, respectively. This flag is usually used to mark email messages as “Important” or “Urgent.”
‘LARGER N’, ‘SMALLER N’:
Returns all messages larger or smaller than N bytes, respectively.
‘NOT search-key’:
Returns the messages that search-key would not have returned.
‘OR search-key1 search-key2’:
Returns the messages that match either the first or second search-key.
- a. How can you send a Gmail Account Mail
b. with an attachement
c. to bcc / cc
> > > import ezgmail
a.
ezgmail.send(‘recipient@example.com’, ‘Subject line’, ‘Body of the email’)
b.
»> ezgmail.send(‘recipient@example.com’, ‘Subject line’, ‘Body of the email’,
[‘attachment1.jpg’, ‘attachment2.mp3’])
c.
»> ezgmail.send(‘recipient@example.com’, ‘Subject line’, ‘Body of the email’,
cc=’friend@example.com’, bcc=’otherfriend@example.com,someoneelse@example.com’)
13.
a. How can you read from a Gmail account
b. See the 25 most recent mails
> > > import ezgmail
a.
»> unreadThreads = ezgmail.unread() # List of GmailThread objects.
»> ezgmail.summary(unreadThreads)
b.
ezgmail.recent()