How to send mails with attachment in python?

 smtplib: The smtplib module defines an SMTP client session object that can be used to send emails and files to any Internet machine with an SMTP or ESMTP listener daemon.

SMTP needs valid source and destination email ids, and port numbers. The port numbers vary for different sites. For example google, the port is 587.

SMTP(Simple Mail Transfer Protocol): 

SMTP is used to transfer mail from one user to another user. SMTP is a push protocol and is used to send the mail whereas POP(post office protocol) or IMAP(internet message access protocol) are used to retrieve those emails at the receiver's side.

MIME(Multipurpose Internet Mail Extension): 

We are using the MIME module to make it more flexible. Using the MIME header we can store the sender and receiver information and some other information. Another most important use of the MIME module is to set the attachment with mail.

Some important classes:

  1. MIMEBase(_maintype_subtype*policy=compat32**_params): This is base class for all the MIME-specific subclasses of Message. 
  2. MIMEMultipart(_subtype='mixed'boundary=None_subparts=None*policy=compat32**_params): This is intermidiate base class of MIME messgaes that are multipart.
  3. MIMEAudio(_audiodata_subtype=None_encoder=email.encoders.encode_base64*policy=compat32**_params): The MIMEAudio class is used to create MIME message object of major type audio. _audiodata is a string containing the raw audio data.
  4. MIMEImage(_imagedata_subtype=None_encoder=email.encoders.encode_base64*policy=compat32**_params): The MIMEImage class is used to create MIME message object of major type image. _imagedata is a strinfg containing the raw image data.

NOTE: If you are using Google's Gmail service to send mail. that time you need to do some settings for google security purposes. If those settings are not set up, then the following code may not work. Because Google doesn't support access from a third-party app.


How to allow access? 

You need to set 'Less Secure App Access' settings in your Google account. To off less secure app access, go to Google's Admin Console, and search for the Less Secure App setup.

Example: 

Example: 

  1. # Send a mail with an attachment 
  2.  
  3. import smtplib 
  4. from email.mime.multipart import MIMEMultipart 
  5. from email.mime.text import MIMEText 
  6. from email.mime.base import MIMEBase 
  7. from email import encoders 
  8.  
  9. body = ''' 
  10. Hello, Pratiksha 
  11. I am attaching The invitation to a birthday celebration with this Email. 
  12. Please come. 
  13.  
  14. Lot's of love, 
  15. Tejaswi 
  16. ''' 
  17.  
  18. #Sender Email addresses and password 
  19. senders_email = 'sender@gmail.com' 
  20. sender_password = 'password' 
  21. reveiver_email = 'receiver@gmail.com' 
  22.  
  23. #MIME Setup 
  24. message = MIMEMultipart() 
  25. message['From'] = senders_email 
  26. message['To'] = reveiver_email 
  27. message['Subject'] = 'Invitation Card' 
  28. message.attach(MIMEText(body, 'plain')) 
  29.  
  30. ## File 
  31. attach_file_name = 'c2.text' 
  32. attach_file = open(attach_file_name, 'rb')  
  33. payload = MIMEBase('application', 'octate-stream') 
  34. payload.set_payload((attach_file).read()) 
  35. encoders.encode_base64(payload)  
  36. payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name) 
  37. message.attach(payload) 
  38.  
  39. #SMTP Connection For Sending Email 
  40. session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port 
  41. session.starttls() #enable security 
  42. session.login(senders_email, sender_password) #login with mail_id and password 
  43. text = message.as_string() 
  44. session.sendmail(senders_email, reveiver_email, text) 
  45. session.quit() 
  46. print('Mail sent sucessfully.') 



Thank you 😊 for reading. Please read other blogs. And also share with your friends and family.




Comments

Popular posts from this blog

How to convert PDF file into audio file?

How to perform operations on emails and folders using imap_tools?

Pillow Libary in Python.