10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Python - Send Email

Updated on Sep 3, 2025
 
39,064 Views

A mail server is an application that handles and delivers e-mail over the Internet. Mail servers are of two main categories: outgoing mail servers and incoming mail servers.

Outgoing mail servers are known as SMTP, or Simple Mail Transfer Protocol, servers. Simple Mail Transfer Protocol (SMTP) is an Internet standard for email transmission.

Incoming mail servers come in two main varieties. POP3, or Post Office Protocol and IMAP, or Internet Message Access Protocol.

Python's standard library has 'smtplib' module which defines an SMTP client session object that can be used to send mail via Python program.

smptlib.SMTP() function:

This function returns object of SMTPclass. It encapsulates and manages a connection to an SMTP or ESMTP server. The function takes following arguments:

host

Is the name of the remote host to which to connect.

port

Specifies the port to which to connect. By default, smtplib.SMTP_PORT is used.

local_hostname

Used as the FQDN of the local host in the HELO/EHLO command.

source_address

A 2-tuple (host,port) object for the socket to bind

It has following methods that support SMTP operations:

connect(host, port, source_address)

This method establishes connection to a host on a given port.

login(user, password)

Log in on an SMTP server that requires authentication.

quit()

terminate the SMTP session.

data( msg)

sends message data to server.

docmd(cmd, args)

send a command, and return its response code.

ehlo( name)

Hostname to identify itself

starttls()

puts the connection to the SMTP server into TLS mode.

getreply()

get a reply from the server consisting of server response code.

putcmd(cmd, args)

sends a command to the server.

send_message( msg, from_addr, to_addrs)

converts message to a bytestring and passes it to sendmail.

sendmail(self, from_addr, to_addrs, msg)

This command performs an entire mail transaction.
The arguments are:

from_addr

The address sending this mail.

to_addrs

A list of addresses to send this mail to.

msg

The message to send.

To demonstrate above functionality, let us look at the script below which uses google's smtp mail server to send an email message.

First of all SMTP object is set up using gmail's smtp server and port 527. The SMTP object then identifies itself by invoking ehlo() command. We also activate Transport Layer Security to the outgoing mail message.

Next the login() command is invoked by passing credentials as arguments to it. Finally the mail message is assembled by attaching it a header in prescribed format and it is sent using sendmail() method. The SMTP object is closed afterwards.

import smtplib
content="Hello World"
mail=smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
sender='pythonanytime@gmail.com'
recipient='mlathkar@gmail.com'
mail.login('pythonanytime@gmail.com','m15v5l61')
header='To:'+receipient+'\n'+'From:' \
+sender+'\n'+'subject:testmail\n'
content=header+content
mail.sendmail(sender, recipient, content)
mail.close()

Before running above script, sender's gmail account must be configured to allow 'less secure apps'. Visit following link.

https://myaccount.google.com/lesssecureapps 

Set the shown toggle button to ON.

Image

If everything goes well, execute above script. The message should be delivered to recipient's inbox.

+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses