Steps to send mail from your Gmail account:
- First of all, you need to import the "smtplib" library.
- After that, to create session, we will use its SMTP instance to encapsulate the SMTP connection.
s = smtplib.SMTP (’smtp.gmail.com’, 587)
In this case, you need to pass the first parameter of the server location and the second the port parameter to be used. For Gmail, we use port number 587.
- For security reasons, set your SMTP connection to TLS mode. TLS (Transport Layer Security) encrypts all SMTP commands. After that, for security and authentication, you need to pass your Gmail account credentials to the login instance.
The compiler will show an authentication error if you enter an invalid email id or password.
- Save the message you need to send in a variable, say message. Using a sendmail () instance, send your message. sendmail () uses three parameters: sender_email_id, receive_email_id, and message_to_be_sent . The parameters must be in the same sequence.
This will send an email from your account. Once you have completed your task, end your SMTP session with quit ().
Sending one message to multiple people yam
If you need to send the same message to different people. You can use to loop for this.
For example, you have a list of email ids to which you need to send the same mail. To do this, insert a for loop between initialization and termination of the SMTP session. The loop will be initialized in turn, and after the email is sent, the SMTP session will be terminated.
- This code can send simple mail that has no attachments or any subject .
- One of the most amazing things about this code is that we can send any number of emails with this, and Gmail always puts your mail in the main section. Sent emails will not be spam.
- File processing can also be used to retrieve an email ID from a file and then send emails.