Source: Online social media and website

Send email using SMTP

Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers.
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.
Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail
Create “sendemail” module
import smtplib

def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
              smtpserver):
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver)
    server.starttls()
    server.login(login,password)
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()
    return problems



sendemail.sendemail(from_addr    = 'vadivel.rangasamy@gmail.com',
          to_addr_list = [' xxx123@gmail.com '],
          cc_addr_list = [‘xxx123@gmail.com’],
          subject      = 'Howdy',
          message      = 'Howdy from a python function',
          login        = abc123test@gmail.com',
          password     = 'XXXXXXXX',
          smtpserver = 'smtp.gmail.com:587')

No comments:

Post a Comment