18 SENDING EMAIL AND TEXT MESSAGES Flashcards

1
Q
  1. What is the protocol for sending email? For checking and receiving email?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What 7 smtplib functions/methods must you call to log in to an SMTP server and send a mail?
A
  1. import smtplib
  2. smtpObj = smtplib.SMTP(‘smtp.example.com’, 587)
  3. smtpObj.ehlo()
  4. smtpObj.starttls()
  5. smtpObj.login(‘bob@example.com’, ‘MY_SECRET_PASSWORD’)
  6. smtpObj.sendmail(‘bob@example.com’, ‘alice@example.com’, ‘Subject: ….\n…’)
    {}
  7. smtpObj.quit()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What 3 imapclient functions/methods must you call to log in to an IMAP server?
A
  1. import imapclient
  2. imapObj = imapclient.IMAPClient(‘imap.example.com’, ssl=True)
  3. imapObj.login(‘my_email_address@example.com’, ‘MY_SECRET_PASSWORD’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What kind of argument do you pass to imapObj.search()?
A

> > > imapObj.select_folder(‘INBOX’, readonly=True)

> > > UIDs = imapObj.search([‘SearchKey’])

> > > UIDs

SearchKey Examples:
-BEFORE/ ON/ SINCE 05-Jul-2019
-ALL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What do you do if your code gets an error message that says got more than 10000 bytes?
A

Increase the max byte size

> > > imaplib._MAXLINE = 10000000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. The imapclient module handles connecting to an IMAP server and finding emails. What is one module that handles reading the emails that imapclient collects?
A
  1. Create Message Object, 2.use PYZMAIL
  2. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. When using the Gmail API, what are the credentials.json and token.json files?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. 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

a.
»> ezgmail.summary(unreadThreads)

b. unreadThreads[0].messages[0].messageobject #see below

Thread:
consists of GmailMessage objects

MessageObjects:

  • subject
  • body
  • timestamp
  • sender
  • recipient
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Using ezgmail.search(), how can you find emails that have file attachments?
A

> > > 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/.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What three pieces of information do you need from Twilio before you can send text messages?
A
  1. phone number to use as the sender of text messages
  2. account SID
  3. auth (authentication) token
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  • Imap Search Keys
A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. a. How can you send a Gmail Account Mail

b. with an attachement
c. to bcc / cc

A

> > > 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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13.
a. How can you read from a Gmail account

b. See the 25 most recent mails

A

> > > import ezgmail

a.
»> unreadThreads = ezgmail.unread() # List of GmailThread objects.
»> ezgmail.summary(unreadThreads)

b.
ezgmail.recent()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly