How to Send Emails through Gmail in Python?

Sending a large number of emails manually is a tedious task. you can use third-party services to send emails in bulk at once.

how about creating your own custom script to send emails?

Isn’t that wonderful?

yes, it is. we are going to write a python script to send emails.

python has a library called smtplib which is used to send emails. The smtplib library is based on the smtp (Simple Mail Transport Protocol). smtp is used to send email to others.

configure gmail

Here, we are going to use gmail as the email provider. google does not allow scripts to login. and we need to make a security change to our gmail account that allows scripts to log into our gmail account.

Changing the security option on our gmail account is not good as it allows others to access the account very easily. it is recommended to create a new gmail account. go to settings here and turn on the allow less secure apps: setting on .

If you don’t feel comfortable turning on the above settings, you can use the google api to login to your gmail account. you can find the script to use google api for authentication here.

steps to send mail

Certain steps must be taken to send mail using the smtplib library. Let’s go through the steps first, and then we’ll write the script.

#1. connecting to smtp server

Each service provider will have a different domain name and smtp server port. we have to use the domain name of the smtp server and the port of the email provider that we are going to use in the script. the smtp server domain name and port for gmail are smtp.gmail.com and <em>465</em>.

See Also:  The First 24 Hours With Mail Order Chickens - Heritage Acres Market LLC

We will use ssl encryption for the smtp server connection, as it is more secure than tsl encryption. if you want to use tsl encryption, use port 587 instead of 465. the domain name of the smtp server will be different depending on the email service provider.

the code to connect to the smtp server

#2. login

Once the connection to the smtp server is established, we can login with the email address and password using the login smtp object method. the code looks like this.

#3. send mail

after login, there is no way we can wait to send mail. send the mail using the sendmail method. Please make sure to send the email in the following format.

subject: your_subject_for newline mail_content

spaces are not required. they are just for clarification on the above format. let’s see the sample code.

#4. exit

don’t forget to exit the smtp c

We have seen the steps to send mail using python. but, we have not discussed the complete code. let’s quickly review the code.

We have created a class called mail. and it has a method called send to send the mails. writing class or just not up to you. the class makes it more readable. we have implemented all the steps discussed above one by one in the submit method.

hooray! has sent a mail using a python script.

See Also:  How To Set Up Email Quarantine In Gmail For G Suite Subscribers

html content

what if you want to send the mail in html? is it possible?

yes, why not. we can send the mail using html library called email.mime. it is a built-in library.

mime is a standard used to extend the format of emails to support application programs, video, images, etc.,

There are two classes we need from the email.mime module. They are mimetext and mimemultipart. Let’s see a brief explanation about them.

#1. mime text

The mimetext class is used to write our mail content in html. we will create an instance of the mimetext class passing the html content and the content type. see sample code below.

some mail services do not support rendering to html. therefore, it is better to create two instances of the mimetext class for plain text and html.

#2. mimomultipart

The mimemultipart class is used to simplify the formatting and writing of the subject, from address to address, etc. we will give the content created with the mimetext class to mimemultipart using the connect method.

we need to make sure that the instance of mimemultipart is created with the alternative argument to render plain text or html. Let’s send an email with html content.

You can also add a blind copy using that bcc attribute on the mimemultipart instance.

add attachments

The attached files can be images, pdf, documents, sheets, etc., it is called mimebase in the email.mime class. is used to add attachments to mail.

See Also:  Absentee/mail-in ballot vote fraud - Ballotpedia

let’s add an attachment to the above email.

mail to bulk emails at once

we have used a loop to send the same mail to multiple members. that’s one case (when you don’t want receivers to know about other receivers).

Suppose you have to send the same mail to 1000 members of the same group at the same time. in such cases, the use of a loop will not be appropriate. we can add multiple emails in the to field in the general email composition. how to do it in python script?

we need to combine the email list as a string separated by comma and space. we can use the chain of the join method to combine all the emails as a chain. see the code to combine emails as a string.

replace the a field in the above script using the above string. that’s it, you have sent the mail to mass mails in one go.

conclusion

there are some third party libraries to send email in python. some of them are envelopes, yagmail, flanker etc, these libraries help us to write scripts with few lines of code. you can also explore them.

now you can automate your emails using python scripts. the structure of sending emails will be different depending on your use case. we have seen different scenarios of sending emails. you can easily customize the scripts discussed in the tutorial for your use case. I have taken the reference from this article.

happy coding 🙂

Leave a Reply

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