You are on page 1of 12

Sending email

using python
Sending Email
 SMTP:
Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-
mail and routing e-mail between mail servers.

 Pythonprovides 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.
 Simple syntax to create one SMTP object, which can later be used to send an e-
mail.

import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )

Where
 host − This is the host running your SMTP server. You can specify IP address of the
host or a domain name like xyz.com. This is optional argument.
 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.
 local_hostname − If your SMTP server is running on your local machine, then you
can specify just localhost as of this option.
 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 −

• The sender − A string with the address of the sender.


• The receivers − A list of strings, one for each recipient.
• The message − A message as a string formatted as specified in the various
RFCs.
Sending basic email
#native library in Python to send emails
import smtplib
#To create an SMTP object, each object is used for connection with one server

server = smtplib.SMTP('smtp.gmail.com', 587)

#TLS (Transport Layer Security)


server.starttls()

#security function, needed to connect to the Gmail server


server.login("YOUR EMAIL ADDRESS", "YOUR PASSWORD")
msg = "YOUR MESSAGE!"
server.sendmail("YOUR EMAIL ADDRESS", "THE EMAIL ADDRESS TO SEND TO", msg)
print("Mail Sent")

server.quit()
 Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the
format of email to support:

– Text in character sets other than ASCII.


– Non-text attachments: audio, video, images, application programs etc.
– Message bodies with multiple parts.
– Header information in non-ASCII character sets.
Sending a more elaborated email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

fromaddr = "YOUR ADDRESS"


toaddr = "ADDRESS YOU WANT TO SEND TO“

#Create the MIMEMultipart message object and load it with appropriate headers
for From, To, and Subject fields
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"
body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)


server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Sending email to multiple recipients
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

fromaddr = " YOUR ADDRESS"


toaddr = ["ADDRESS YOU WANT TO SEND TO ", "ADDRESS YOU WANT TO SEND TO"]
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddr)
msg['Subject'] = "SUBJECT OF THE MAIL"

body = "YOUR MESSAGE HERE"


msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)


server.starttls()
server.login(fromaddr, "YOUR PASSWORD ")

#Return the entire message flattened as a string


text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Sending email with to,cc,bcc
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

fromaddr = "YOUR ADDRESS"


toaddr = "ADDRESS YOU WANT TO SEND TO"
cc = "ADDRESS YOU WANT TO SEND TO"
bcc = "ADDRESS YOU WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"

body = "YOUR MESSAGE HERE"


msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)


server.starttls()
server.login(fromaddr, "YOUR PASSWORD ")

#Return the entire message flattened as a string


text = msg.as_string()
server.sendmail(fromaddr,[toaddr,cc,bcc] , text)
server.quit()
Sending an email with attachment
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

fromaddr = " YOUR ADDRESS "


toaddr = " ADDRESS YOU WANT TO SEND TO "
msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"

body = "TEXT YOU WANT TO SEND"

msg.attach(MIMEText(body, 'plain'))
filename = “note.txt"
attachment = open("C:\\Users\\SAI\\Desktop\\note.txt","rb")

#MIME attachment with the content type "application/octet-stream" is a binary file


part = MIMEBase('application', 'octet-stream')

#To change the payload to encoded form


part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)


server.starttls()
server.login(fromaddr, " YOUR PASSWORD ")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

You might also like