Skip to content

Commit 9894bc0

Browse files
committed
added sending emails tutorial
1 parent 7f77249 commit 9894bc0

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5656
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
5757
- [How to Execute BASH Commands in a Remote Machine in Python](https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python). ([code](general/execute-ssh-commands))
5858
- [How to Manipulate IP Addresses in Python using ipaddress module](https://www.thepythoncode.com/article/manipulate-ip-addresses-using-ipaddress-module-in-python). ([code](general/ipaddress-module))
59+
- [How to Send Emails in Python using smtplib Module](https://www.thepythoncode.com/article/sending-emails-in-python-smtplib). ([code](general/email-sender))
5960

6061
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
6162
- [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor))

general/email-sender/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [How to Send Emails in Python using smtplib Module](https://www.thepythoncode.com/article/sending-emails-in-python-smtplib)
2+
Feel free to edit the code how ever you want to suit your needs!

general/email-sender/email_sender.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import smtplib
2+
from email.mime.text import MIMEText
3+
from email.mime.multipart import MIMEMultipart
4+
from email.mime.audio import MIME
5+
6+
# your credentials
7+
8+
password = "password"
9+
10+
# the sender's email
11+
12+
# the receiver's email
13+
14+
# the subject of the email (subject)
15+
subject = "Just a subject"
16+
17+
# initialize the message we wanna send
18+
msg = MIMEMultipart()
19+
# set the sender's email
20+
msg["From"] = FROM
21+
# set the receiver's email
22+
msg["To"] = TO
23+
# set the subject
24+
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()

0 commit comments

Comments
 (0)