Sunday, August 20, 2017

Sending Mail using SMTP library in python

What is SMTP?

SMTP (Simple Mail Transfer Protocol) is a TCP/IP protocol used in sending and receiving e-mail. However, since it is limited in its ability to queue messages at the receiving end, it is usually used with one of two other protocols, POP3 or IMAP, that let the user save messages in a server mailbox and download them periodically from the server. In other words, users typically use a program that uses SMTP for sending e-mail and either POP3 or IMAP for receiving e-mail. On Unix-based systems, sendmail is the most widely-used SMTP server for e-mail. A commercial package, Sendmail, includes a POP3 server. Microsoft Exchange includes an SMTP server and can also be set up to include POP3 support.


Port Number:


While sending the mail using SMTP, we need to set the port number which is given below in table:

Default Ports                                               Server                           Authentication                          Port

SMTP Server (Outgoing Messages)           Non-Encrypted              AUTH                                       25(or 587)
                                                                    Secured(TLS)               startTLS                                    587
                                                                    Secured(SSL)               SSL                                           465
POP3 Server (Incoming Messages)           Non-Encrypted              Auth                                          110
Secure (SSL)                                                                                    SSL                                           995

for more detail about the ports, Click here.


What is TLS?

Transport Layer Security (TLS) is a protocol that provides privacy and data integrity between two communicating applications. It's the most widely deployed security protocol used today, and is used for Web browsers and other applications that require data to be securely exchanged over a network, such as file transfers, VPN connections, instant messaging and voice over IP.

Less Secure apps Section:

If we are using gmail server for sending the mail, first of all we have to enable the less secure apps section. To enable this option, please redirect here and turn it on, or follow steps shown in pictures below:
1. Search for 'less secure apps section' in google.

2. Click on 'less secure apps sections' link.

3. Click on allow less secure apps and turn it on.

Here you go..!!

(THIS OPTION CAN'T BE ENABLED IF THE TWO STEP VERIFICATION IS ENABLED IN YOUR ACCOUNT. FIRST OF ALL, YOU HAVE TO DISABLE THE 2ND STEP VERIFICATION TO ENABLE LESS SECURE APPS)


Steps:

  1. Import SMTP Library.(can be found as smtplib).
  2. Start the SMTP by providing the link and port number.
  3. start the TLS.
  4. Provide input for email ID and password as these are necessary credentials for login.
  5. Login to the server using these credentials. 
  6. Input the message you want to send.
  7. Provide the email of the recipient. 
  8. Provide the sendmail command to send the mail to the recipient.
  9. Quit the server.

Source Code:



import smtplib
server=smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
print('starting server')
mail_id=str(input('Enter the email ID:'))
password=str(input('enter the password:'))
server.login(mail_id,password)
print('logged in')
msg=str(input('Enter the message here:'))
email_id=str(input("enter the recipient's ID:"))
server.sendmail(mail_id,email_id,msg)
print('Mail sent successfully')
server.quit()
print('logged out')


Was this topic helpful? Please reply with yes or no in comments below.

No comments:

Post a Comment