top

Search

Python Tutorial

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:hostIs the name of the remote host to which to connect.  portSpecifies the port to which to connect. By default, smtplib.SMTP_PORT is used.  local_hostnameUsed as the FQDN of the local host in the HELO/EHLO command.  source_addressA 2-tuple (host,port) object for the socket to bindIt 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 itselfstarttls()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_addrThe address sending this mail.to_addrsA list of addresses to send this mail to.  msgThe 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. If everything goes well, execute above script. The message should be delivered to recipient's inbox.
Rated 5/5 based on 11 customer reviews
logo

Python Tutorial

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:

hostIs the name of the remote host to which to connect.  
portSpecifies the port to which to connect. By default, smtplib.SMTP_PORT is used.  
local_hostnameUsed as the FQDN of the local host in the HELO/EHLO command.  
source_addressA 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_addrThe address sending this mail.
to_addrsA list of addresses to send this mail to.  
msgThe 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. 

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

R Programming Tutorial

By KnowledgeHut

R Programming
Rated 5/5 based on 19 customer reviews

C# Tutorial

By KnowledgeHut

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
Rated 5/5 based on 12 customer reviews
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...

Read More