#import os #import smtplib #from email import encoders #from email.mime.base import MIMEBase #from email.mime.multipart import MIMEMultipart #COMMSPACE = ', ' def sendfailedreport(attachments,email,fieldaddress,date,whitelabel, whitelabelEmail, whitelabelPassword): import os import smtplib from email import encoders from email.mime.text import MIMEText from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart #COMMASPACE = ', ' if whitelabelEmail is not None: sender = whitelabelEmail gmail_password = whitelabelPassword else: sender = 'support@farmonaut.com' gmail_password = 'ankur@A8' recipient = email outer = MIMEMultipart() outer['Subject'] = 'Satellite Based Crop Health Monitoring Results - Field Address: '+fieldaddress outer['To'] = recipient outer['From'] = sender year = date[0:4] month = date[4:6] day = date[6:] date = year+'-'+month+'-'+day+' (in yyyy-mm-dd)' message = 'Hello,'+ '\n\nThe new results for your field at the address: '+fieldaddress+' are available now. The Satellite crossed over your field on '+date+' . There was too much cloud cover over your field for this satellite imagery capture date, hence we could not generate results and processed images for your field for this date. This satellite imagery date will not be counted in your subscription package and your package will be increased by one more satellite visit The next satellite movement is expected in the next 4-10 days. Please find attached below a failed report of the satellite visit.\n\nRegards' outer.attach(MIMEText(message)) outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' attachments = attachments for file in attachments: try: with open(file,'rb') as fp: msg = MIMEBase('application',"octet-stream") msg.set_payload(fp.read()) encoders.encode_base64(msg) msg.add_header('Content-Disposition','attachment',filename=os.path.basename(file)) #text = "sample text" outer.attach(msg) except: print("Unable to open one of the attachments. Error: ",sys.exc_info()[0]) composed = outer.as_string() try: with smtplib.SMTP('smtp.gmail.com',587) as s: s.ehlo() s.starttls() s.ehlo() s.login(sender,gmail_password) s.sendmail(sender,recipient, composed) s.close() print("Email sent!") except: print("Unable to send the email") if __name__ == '__main__': main()