Skip to content

Commit 8453674

Browse files
committed
update sending emails tutorial
1 parent 401efc7 commit 8453674

File tree

6 files changed

+1345
-16
lines changed

6 files changed

+1345
-16
lines changed

general/email-sender/1810.04805.pdf

757 KB
Binary file not shown.

general/email-sender/email_sender.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import smtplib
22
from email.mime.text import MIMEText
33
from email.mime.multipart import MIMEMultipart
4-
from email.mime.audio import MIME
4+
from bs4 import BeautifulSoup as bs
5+
6+
def send_mail(email, password, FROM, TO, msg):
7+
# initialize the SMTP server
8+
server = smtplib.SMTP("smtp.gmail.com", 587)
9+
# connect to the SMTP server as TLS mode (secure) and send EHLO
10+
server.starttls()
11+
# login to the account using the credentials
12+
server.login(email, password)
13+
# send the email
14+
server.sendmail(FROM, TO, msg.as_string())
15+
# terminate the SMTP session
16+
server.quit()
517

618
# your credentials
719
email = "email@example.com"
@@ -15,24 +27,29 @@
1527
subject = "Just a subject"
1628

1729
# initialize the message we wanna send
18-
msg = MIMEMultipart()
30+
msg = MIMEMultipart("alternative")
1931
# set the sender's email
2032
msg["From"] = FROM
2133
# set the receiver's email
2234
msg["To"] = TO
2335
# set the subject
2436
msg["Subject"] = subject
25-
# set the body of the email
26-
text = MIMEText("This email is sent using <b>Python</b> !", "html")
27-
# attach this body to the email
28-
msg.attach(text)
29-
# initialize the SMTP server
30-
server = smtplib.SMTP("smtp.gmail.com", 587)
31-
# connect to the SMTP server as TLS mode (secure) and send EHLO
32-
server.starttls()
33-
# login to the account using the credentials
34-
server.login(email, password)
35-
# send the email
36-
server.sendmail(FROM, TO, msg.as_string())
37-
# terminate the SMTP session
38-
server.quit()
37+
# set the body of the email as HTML
38+
html = """
39+
This email is sent using <b>Python </b>!
40+
"""
41+
# uncomment below line if you want to use the HTML template
42+
# located in mail.html
43+
# html = open("mail.html").read()
44+
# make the text version of the HTML
45+
text = bs(html, "html.parser").text
46+
47+
text_part = MIMEText(text, "plain")
48+
html_part = MIMEText(html, "html")
49+
# attach the email body to the mail message
50+
# attach the plain text version first
51+
msg.attach(text_part)
52+
msg.attach(html_part)
53+
54+
# send the mail
55+
send_mail(email, password, FROM, TO, msg)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import smtplib
2+
from email import encoders
3+
from email.mime.text import MIMEText
4+
from email.mime.multipart import MIMEMultipart
5+
from email.mime.base import MIMEBase
6+
from bs4 import BeautifulSoup as bs
7+
8+
# list of files to send as an attachment to the email
9+
# feel free to add your attachments here
10+
files_to_send = [
11+
"test.txt",
12+
"1810.04805.pdf",
13+
]
14+
15+
16+
def send_mail(email, password, FROM, TO, msg):
17+
# initialize the SMTP server
18+
server = smtplib.SMTP("smtp.gmail.com", 587)
19+
# connect to the SMTP server as TLS mode (secure) and send EHLO
20+
server.starttls()
21+
# login to the account using the credentials
22+
server.login(email, password)
23+
# send the email
24+
server.sendmail(FROM, TO, msg.as_string())
25+
# terminate the SMTP session
26+
server.quit()
27+
28+
# your credentials
29+
email = "email@example.com"
30+
password = "password"
31+
32+
# the sender's email
33+
FROM = "email@example.com"
34+
# the receiver's email
35+
TO = "to@example.com"
36+
# the subject of the email (subject)
37+
subject = "Just a subject"
38+
39+
# initialize the message we wanna send
40+
msg = MIMEMultipart("alternative")
41+
# set the sender's email
42+
msg["From"] = FROM
43+
# set the receiver's email
44+
msg["To"] = TO
45+
# set the subject
46+
msg["Subject"] = subject
47+
# set the body of the email as HTML
48+
html = open("mail.html").read()
49+
# make the text version of the HTML
50+
text = bs(html, "html.parser").text
51+
52+
text_part = MIMEText(text, "plain")
53+
html_part = MIMEText(html, "html")
54+
# attach the email body to the mail message
55+
# attach the plain text version first
56+
msg.attach(text_part)
57+
msg.attach(html_part)
58+
59+
for file in files_to_send:
60+
# open the file as read in bytes
61+
with open(file, "rb") as f:
62+
# read the file content
63+
data = f.read()
64+
# create the attachement
65+
attach_part = MIMEBase("application", "octet-stream")
66+
attach_part.set_payload(data)
67+
# encode the data to base 64
68+
encoders.encode_base64(attach_part)
69+
# add the Content-Disposition header
70+
attach_part.add_header("Content-Disposition", f"attachment; filename= {file}")
71+
msg.attach(attach_part)
72+
73+
# send the mail
74+
send_mail(email, password, FROM, TO, msg)

0 commit comments

Comments
 (0)