Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:45:03+00:00 2026-05-26T16:45:03+00:00

I have the script below which I’m using to send say 10 messages myself<->myself.

  • 0

I have the script below which I’m using to send say 10 messages myself<->myself. However, I’ve noticed that Python really takes a while to do that. Last year I needed a system to send about 200 emails with attachments and text and I implemented it with msmtp + bash. As far as I remember it was much faster.

Moving the while loop inside (around the smtp_serv.sendmail(sender, recepient, msg) function yields similar results).

Am I doing something wrong? Surely it can’t be slower than bash + msmtp (and I’m only sending a ‘hi’ message, no attachments).

#! /usr/bin/python3.1

def sendmail(recepient,  msg):

    import smtplib

    # Parameters
    sender = 'login@gmail.com'
    password = 'password'
    smtpStr = 'smtp.gmail.com'
    smtpPort = 587
    # /Parameters

    smtp_serv = smtplib.SMTP(smtpStr, smtpPort)
    smtp_serv.ehlo_or_helo_if_needed()
    smtp_serv.starttls()
    smtp_serv.ehlo()

    recepientExists = smtp_serv.verify(recepient)
    if recepientExists[0] == 250:
        smtp_serv.login(sender, password)
        try:
            smtp_serv.sendmail(sender, recepient, msg)
        except smtplib.SMTPException:
            print(recepientExists[1])
    else:
        print('Error',   recepientExists[0], ':',  recepientExists[1])

    smtp_serv.quit()

for in in range(10):
    sendmail('receiver@gmail.com',  'hi')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:45:04+00:00Added an answer on May 26, 2026 at 4:45 pm

    In this script it takes five times more time to setup SMTP connection (5 seconds) than to send a e-mail (1 second) so it could make sense to setup a single connection and send several e-mails instead of creating the connection each time:

    #!/usr/bin/env python3
    import smtplib    
    from contextlib import contextmanager
    from datetime   import datetime
    from email.mime.text import MIMEText
    from netrc      import netrc
    from timeit     import default_timer as timer
    
    @contextmanager
    def logined(sender, password, smtp_host='smtp.gmail.com', smtp_port=587):
        start = timer(); smtp_serv = smtplib.SMTP(smtp_host, smtp_port, timeout=10)
        try: # make smtp server and login
            smtp_serv.ehlo_or_helo_if_needed()
            smtp_serv.starttls()
            smtp_serv.ehlo()
            print('smtp setup took (%.2f seconds passed)' % (timer()-start,))
            start = timer(); smtp_serv.login(sender, password)
            print('login took %.2f seconds' % (timer()-start,))
            start = timer(); yield smtp_serv
        finally:
            print('Operations with smtp_serv took %.2f seconds' % (timer()-start,))
            start = timer(); smtp_serv.quit()
            print('Quiting took %.2f seconds' % (timer()-start,))
    
    smtp_host = 'smtp.gmail.com'
    login, _, password = netrc().authenticators(smtp_host)
    with logined(login, password, smtp_host) as smtp_serv:
        for i in range(10):
            msg = MIMEText('#%d timestamp %s' % (i, datetime.utcnow()))
            msg['Subject'] = 'test #%d' % i
            msg['From'] = login
            msg['To'] = login
            smtp_serv.send_message(msg) 
    

    Output

    smtp setup took (5.43 seconds passed)
    login took 0.40 seconds
    Operations with smtp_serv took 9.84 seconds
    Quiting took 0.05 seconds
    

    If your Python version doesn’t have .send_message() then you could use:

    smtp_serv.sendmail(from, to, msg.as_string())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tested the below script on a demo page which is not using
I have a python script which outputs lots of data, sample is as below.
I have a small script which im using to test PHP mail(), as below:
I'm using the zend framework and have a script which sends emails. However my
I have a script in the link below which works in IE8 nicely. However,
I have a user who randomly gets a Script Error box (shown below) which
I have a Perl script which nests foreach loops as seen below. It takes
so I have this jQuery script below that works. But since I'm just learning
i Have an arguments like the one below which i pass to powershell script
I have a javascript script like this below which the 'args' contain a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.