Skip to content

Commit b34e05f

Browse files
authored
Add files via upload
1 parent aa1dde0 commit b34e05f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# My First Project
2+
I recently dived into Python 3 as a learning exercise to explore how I could send a batch of emails. In a production setting, there may be more straightforward approaches, but the following worked well for me.
3+
4+
## Installation
5+
It is built-in module in python. No need of installation.
6+
7+
```python
8+
import smtplib
9+
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
10+
```
11+
# Outline
12+
* [SMTP](#smtp)
13+
* [Algorithm](#algorithm)
14+
* [Python Code](#python-code)
15+
16+
# Python : Sending Emails using SMTPLIB
17+
I recently dived into Python 3 as a learning exercise to explore how I could send a batch of emails. In a production setting, there may be more straightforward approaches, but the following worked well for me.
18+
19+
So, let's say you have a list of contacts with their names and email addresses. And you'd like to send a message to each of those people, with the phrase "Dear [name]" at the head of the message.
20+
21+
You can save the contact information in a file rather than a database for ease of use. You can also save a file with the message template you want to send.
22+
23+
## SMTP
24+
Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers.
25+
26+
Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
27+
28+
Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail −
29+
30+
```python
31+
import smtplib
32+
33+
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
34+
```
35+
Here is the detail of the parameters −
36+
37+
1. host − This is the host running your SMTP server. You can specify IP address of the host or a domain name like tutorialspoint.com. This is optional argument.
38+
2. port − If you are providing host argument, then you need to specify a port, where SMTP server is listening. Usually this port would be 25.
39+
3. local_hostname − If your SMTP server is running on your local machine, then you can specify just localhost as of this option.
40+
41+
An SMTP object has an instance method called sendmail, which is typically used to do the work of mailing a message. It takes three parameters −
42+
43+
1. The sender − A string with the address of the sender.
44+
2. The receivers − A list of strings, one for each recipient.
45+
3. The message − A message as a string formatted as specified in the various RFCs.
46+
47+
## Algorithm
48+
49+
Here are four basic steps for sending emails using Python:
50+
51+
1. First and foremost, the "smtplib" library must be imported.
52+
53+
2. After that, we'll use its instance SMTP to wrap an SMTP connection to create a session.
54+
55+
```python
56+
57+
s = smtplib.SMTP('smtp.gmail.com', 587)
58+
59+
```
60+
61+
You must pass the first parameter, which is the server's location, and the second value, which is the port to use. The port number 587 is used for Gmail. Different port numbers are used by different websites.
62+
63+
3. For security reasons, now put the SMTP connection in the TLS mode. TLS (Transport Layer Security) encrypts all the SMTP commands. After that, for security and authentication, you need to pass your Gmail account credentials in the login instance.The compiler will show an authentication error if you enter invalid email id or password.
64+
65+
4. Store the message you need to send in a variable say, message. Using the sendmail() instance, send your message. sendmail() uses three parameters: sender_email_id, receiver_email_id and message_to_be_sent. The parameters need to be in the same sequence.
66+
67+
5. After you have completed your task, terminate the SMTP session by using quit().
68+
69+
## Python Code
70+
71+
```python
72+
import smtplib
73+
74+
# creates SMTP session
75+
session = smtplib.SMTP('smtp.gmail.com', 587)
76+
77+
# start TLS for security
78+
session.starttls()
79+
80+
# Authentication
81+
session.login("sender_email_id", "sender_email_id_password")
82+
83+
# message to be sent
84+
message = "Message you want to send"
85+
86+
# sending the mail
87+
session.sendmail("sender_email_id", "receiver_email_id", message)
88+
89+
# terminating the session
90+
session.quit()
91+
```
92+
## NOTE :
93+
This code can send simple mail which doesn’t have any attachment or any subject.
94+
95+
# Conclusion
96+
This code can send plain text emails with no attachments or subject lines.
97+
98+
Feel free to copy it and make changes as needed.
99+
100+
Enjoy sending emails with Python, and don't forget to keep it spam-free!
101+
102+
# Contributing
103+
Pull requests are welcome. For major changes, please open and issue first to discuss what you would like to change.
104+
105+
Please make sure to update tests as appropriate.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import smtplib
2+
3+
# creates SMTP session
4+
session = smtplib.SMTP('smtp.gmail.com', 587)
5+
6+
# start TLS for security
7+
session.starttls()
8+
9+
# Authentication
10+
session.login("sender_email_id", "sender_email_id_password")
11+
12+
# message to be sent
13+
message = "Message you want to send"
14+
15+
# sending the mail
16+
session.sendmail("sender_email_id", "receiver_email_id", message)
17+
18+
# terminating the session
19+
session.quit()

0 commit comments

Comments
 (0)