Send email from python programming with SMTP

Send email from python: in this tutorial, we are going to learn about the step by step process to send email using python programming even we are going to write a program.

in terms of sending emails, python has really ice modules that we can use to send an email. so today we are going to use email and smtplib modules to send emails using python.


 Send email from python programming with SMTP

What we are going to learn.

  • The basics of emailing.
  • Send email to multiple addresses
  • Send email using CC and BCC.

Send email from python programming with SMTP

Program to send email using smtplib

    
    import smtplib

    def send_email(host, subject, to_addr, from_addr, body_text):

    """
    Send an email
    """
    BODY = "\r\n".join((
                   "From: %s" % from_addr,
                   "To: %s" % to_addr,
                   "Subject: %s" % subject ,
                   "", body_text ))
    server = smtplib.SMTP(host)
    server.sendmail(from_addr, [to_addr], BODY)
    server.quit()

    if __name__ == "__main__":

    host = "mySMTP.server.com"
    subject = "Test email from Python"
    to_addr = "mike@someAddress.org"
    from_addr = "python@mydomain.com"
    body_text = "Here is my first email!"
    send_email(host, subject, to_addr, from_addr, body_text)


Note: you note that this code doesn't require any username or password in it. so if your server requires authentication then you need to add the following code right after you create the server object.




    server.login(username, password)



simply we first import the smtplib module and then create a separate function. and then we create a BODY string in which we include the necessary information. and then we create an object from the host server and then use the built-in function sendmail to send the mail. and then quite the connection to the server.


Send Email to Multiple Address

Program to send email using multiple addresses


   import smtplib


   def send_email(subject, body_text, emails):

   """
   Send an email
   """
   host = some.server.com
   from_addr = example@mydomain.com

   BODY = "\r\n".join((

                   "From: %s" % from_addr,
                   "To: %s" % ', '.join(emails),
                   "Subject: %s" % subject ,
                   ""body_text ))
   server = smtplib.SMTP(host)
   server.sendmail(from_addr, emails, BODY)
   server.quit()

   if __name__ == "__main__":

   emails = ["mike@someAddress.org", "someone@gmail.com"]
   subject = "Test email from Python"
   body_text = "here is my first email!"
   send_email(subject, body_text, emails)


Here we remove the add parameter and added a list of email addresses. and to make this work we create a comma-separated string in the portion of the BODY and pass the list of emails to the sendmail method.

Send Emails using CC and BCC.

Program to send emails with CC and BCC sections.


   import smtplib
   def send_email(subject, body_text, to_emails, cc_emails, bcc_emails):
   """
   Send an email
   """
   host = some.server.com
   from_addr = example@mydomain.com
   BODY = "\r\n".join((
                   "From: %s" % from_addr,
                   "To: %s" % ', '.join(to_emails),
                   "CC: %s" % ', '.join(cc_emails),
                   "BCC: %s" % ', '.join(bcc_emails),
                   "Subject: %s" % subject ,
                   ""body_text ))
   emails = to_emails + cc_emails + bcc_emails
   server = smtplib.SMTP(host)
   server.sendmail(from_addr, emails, BODY)
   server.quit()
   if __name__ == "__main__":
   emails = ["mike@somewhere.org"]
   cc_emails = ["someone@gmail.com"]
   bcc_emails = ["schmuck@newtel.net"]
   subject = "Test email from Python"
   body_text = "this is my first email !"
   send_email(subject, body_text, emails, cc_emails, bcc_emails)


In the above code, we create CC and BCC fields and then combine the 3 lists into one so we can pass the combined list to the sendmail() method. 

Note: some email clients may allow the recipient to see the BCC list via the email headers. 
So I think now you should understand that how to send the email using python programming and SMTP module and if you like this post then please share it with your friends and don't forget to follow us. 

Post a Comment

0 Comments