Python script to forward emails using Gmail.
As Gmail doesn't allow to forward multiple emails at once, I've created this script to help me do it.
The script makes use of imaplib
and smtplib
libraries that are built-in in python.
To use the script there are a few changes you need to make:
- Change the
USERNAME
and thePASSWORD
for your email account. - Gmail, fortunately, by default, doesn't allow IMAP connections, as it is considered risky and unsecure.
You will have to enable access for "Less secure apps" for this to work. Google
gmail enable less secure apps
and you'll find a way. Also, if you'll try the script before enabling "Less secure apps", Gmail will send you an email describing the problem and will show you the way to enable it. - Change the
SEARCH_CRITERIA
according to your needs. You can find more documentation for this here: search command documentation - Don't forget to disable
less secure apps
when you're done, for your safey.
This script creates a new SMTP connection for each email forwarding. Gmail has lots of limits, among which, limits on how many emails you can send per minute or per day. I've also figured that Gmail didn't manage to execute each forward command when many commands were sent consecutively.
You can disable this limitation on your own and try sending more emails during a single connection,
if you don't have lost of emails.
I had 200+ emails and this is what worked for me.
I've also enabled a 5 seconds time delay between each forward command, but you can easily change it.
The constant name is TIME_DELAY
.
Following articles can help get more knowledge on the topics: