| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,9 @@ | |||
| 1 | 1 | # Import the email modules we'll need | |
| 2 | 2 | from email.parser import Parser | |
| 3 | 3 | ||
| 4 | - # If the e-mail headers are in a file, uncomment this line: | ||
| 5 | - #headers = Parser().parse(open(messagefile, 'r')) | ||
| 4 | + # If the e-mail headers are in a file, uncomment these two lines: | ||
| 5 | + # with open(messagefile) as fp: | ||
| 6 | + # headers = Parser().parse(fp) | ||
| 6 | 7 | ||
| 7 | 8 | # Or for parsing headers in a string, use: | |
| 8 | 9 | headers = Parser().parsestr('From: <user@example.com>\n' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,9 +20,8 @@ | |||
| 20 | 20 | for file in pngfiles: | |
| 21 | 21 | # Open the files in binary mode. Let the MIMEImage class automatically | |
| 22 | 22 | # guess the specific image type. | |
| 23 | - fp = open(file, 'rb') | ||
| 24 | - img = MIMEImage(fp.read()) | ||
| 25 | - fp.close() | ||
| 23 | + with open(file, 'rb') as fp: | ||
| 24 | + img = MIMEImage(fp.read()) | ||
| 26 | 25 | msg.attach(img) | |
| 27 | 26 | ||
| 28 | 27 | # Send the email via our own SMTP server. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,8 @@ | |||
| 12 | 12 | from imaginary import magic_html_parser | |
| 13 | 13 | ||
| 14 | 14 | # In a real program you'd get the filename from the arguments. | |
| 15 | - msg = BytesParser(policy=policy.default).parse(open('outgoing.msg', 'rb')) | ||
| 15 | + with open('outgoing.msg', 'rb') as fp: | ||
| 16 | + msg = BytesParser(policy=policy.default).parse(fp) | ||
| 16 | 17 | ||
| 17 | 18 | # Now the header items can be accessed as a dictionary, and any non-ASCII will | |
| 18 | 19 | # be converted to unicode: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,10 +6,9 @@ | |||
| 6 | 6 | ||
| 7 | 7 | # Open a plain text file for reading. For this example, assume that | |
| 8 | 8 | # the text file contains only ASCII characters. | |
| 9 | - fp = open(textfile, 'rb') | ||
| 10 | - # Create a text/plain message | ||
| 11 | - msg = MIMEText(fp.read()) | ||
| 12 | - fp.close() | ||
| 9 | + with open(textfile) as fp: | ||
| 10 | + # Create a text/plain message | ||
| 11 | + msg = MIMEText(fp.read()) | ||
| 13 | 12 | ||
| 14 | 13 | # me == the sender's email address | |
| 15 | 14 | # you == the recipient's email address | |
| Back | FazBrowse Home | New Git URL |
0 commit comments