Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6529af

Browse files
authored
Add files via upload
1 parent e8d5c87 commit a6529af

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import smtplib
2+
3+
from email.message import EmailMessage
4+
from email.utils import make_msgid
5+
6+
msg = EmailMessage()
7+
8+
asparagus_cid = make_msgid()
9+
msg.set_content('This is a text message')
10+
msg.add_alternative("""\
11+
<html>
12+
<head></head>
13+
<body>
14+
<p>Hello</p>
15+
<p>
16+
Here is an example of sending attachments in email using Python.
17+
</p>
18+
<img src="cid:{asparagus_cid}" />
19+
</body>
20+
</html>
21+
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
22+
23+
with open("sample.jpg", 'rb') as img:
24+
msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg', cid=asparagus_cid)
25+
26+
with open("sample.pdf", 'rb') as fp:
27+
pdf_data = fp.read()
28+
ctype = 'application/octet-stream'
29+
maintype, subtype = ctype.split('/', 1)
30+
msg.add_attachment(pdf_data, maintype=maintype, subtype=subtype, filename='sample.pdf')
31+
32+
fromEmail = '[email protected]'
33+
toEmail = '[email protected]'
34+
35+
msg['Subject'] = 'HTML message with attachments'
36+
msg['From'] = fromEmail
37+
msg['To'] = toEmail
38+
39+
s = smtplib.SMTP('smtp.gmail.com', 587)
40+
41+
s.starttls()
42+
43+
s.login(fromEmail, 'gmail password')
44+
s.send_message(msg)
45+
s.quit()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import smtplib
2+
3+
from email.message import EmailMessage
4+
from email.utils import make_msgid
5+
6+
msg = EmailMessage()
7+
8+
asparagus_cid = make_msgid()
9+
msg.set_content('This is a text message')
10+
msg.add_alternative("""\
11+
<html>
12+
<head></head>
13+
<body>
14+
<p>Hello</p>
15+
<p>
16+
Here is an example of sending attachments in email using Python.
17+
</p>
18+
<img src="cid:{asparagus_cid}" />
19+
</body>
20+
</html>
21+
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
22+
23+
with open("sample.jpg", 'rb') as img:
24+
msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg', cid=asparagus_cid)
25+
26+
with open("sample.pdf", 'rb') as fp:
27+
pdf_data = fp.read()
28+
ctype = 'application/octet-stream'
29+
maintype, subtype = ctype.split('/', 1)
30+
msg.add_attachment(pdf_data, maintype=maintype, subtype=subtype, filename='sample.pdf')
31+
32+
fromEmail = '[email protected]'
33+
toEmail = '[email protected]'
34+
35+
msg['Subject'] = 'HTML message with attachments'
36+
msg['From'] = fromEmail
37+
msg['To'] = toEmail
38+
39+
s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
40+
41+
s.login(fromEmail, 'gmail password')
42+
s.send_message(msg)
43+
s.quit()

python-email-attachments/readme.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/how-to-send-attachments-with-email-using-python/

python-email-attachments/sample.jpg

5.47 KB
Loading

python-email-attachments/sample.pdf

2.96 KB
Binary file not shown.

0 commit comments

Comments
 (0)