Mat
Mat

Reputation: 253

How to speed up sending file using smtp

I'm setting up a program which takes a screen shot of the screen and sends it to my email and I want to speed up the process since it will be taking more than one screenshot in a second and sending it to my email but because of the long process of verifying it breaks my program itself and cause lag.

I have tried to separate the log in process and sending process placing it outside of the function but it didn't send the email. I am wondering how I should structure it.

def sendingemail(filename): # creates SMTP session sendss = smtplib.SMTP('smtp.gmail.com', 587)

# start TLS for security
sendss.starttls()
email = '[email protected]'
password = 'password'


email = '[email protected]'
password = 'pass'
send_to_email = '[email protected]'
subject = 'Person on Cam 1'
message = 'Person sighted'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))

attachment = open(filename, 'rb') 

part = MIMEBase('application', 'octet_stream')

part.set_payload((attachment).read())


encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= "+filename) 

msg.attach(part) 
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()

It sends the email but its slow. I want to reduce the amount of time it wastes sending one email.

Upvotes: 0

Views: 1864

Answers (2)

Olaleye Abiola
Olaleye Abiola

Reputation: 77

Depending on your provider, SMTP connection timesout after some seconds. You can use the noop command to test connection status and catch the error (in case of a timeout error) and then call a reconnect function in the Exception.

A simple code to test how long a before a connection timesout:

import smtplib
import time

def login():
   global server

   username = '[email protected]'
   password = 'password'


   server = smtplib.SMTP_SSL("mail.domain.org", 465)
   logged = server.login(username, password)

   print('logded in:', logged, '\n')
   return logged

for i in range(30):
  def test_conn_open():
     try:
        stat = server.noop()[0]
        #Do anything for example send mail
        #server.sendmail(username, dest_mail, fullmessage)
     except:  
        stat = -1
        login() # login again if timeout happens
        test_conn_open()  #Ensures mail is sent on the current loop even if timeout happens
   
    print(stat, " ", i + 1)
    return True if stat == 250 else False

  r = test_conn_open()
  print(r)
  time.sleep(1)

Upvotes: 0

AnFi
AnFi

Reputation: 10903

Use one SMTP session to send many emails

You may keep open smtp session (reuse server object) and use it to send multiple email messages.

SMTP servers use RSET (reset) command before sendind "not first" email to check that SMTP session may be reused.

https://docs.python.org/2/library/smtplib.html

Low-level methods corresponding to the standard SMTP/ESMTP commands HELP, RSET, NOOP, MAIL, RCPT, and DATA are also supported. Normally these do not need to be called directly, so they are not documented here. For details, consult the module code.

Upvotes: 1

Related Questions