#import os #import smtplib #from email import encoders #from email.mime.base import MIMEBase #from email.mime.multipart import MIMEMultipart #COMMSPACE = ', ' def send_sar_email(attachments,email,fieldaddress,date): 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 = ', ' sender = 'support@farmonaut.com' gmail_password = 'ankur@A8' recipient = email outer = MIMEMultipart() 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)' outer['Subject'] = 'Farmonaut | Satellite Results | Radar Vegetation Data | Date: '+ date+' | Field Address: '+fieldaddress + ' Open on Android, iOS or Web-App' message = 'Hello,'+ '\n\nThe new radar vegetation results for your field at the address: '+fieldaddress+' are available now. The Satellite crossed over your field on '+date+' . The results are best understood when directly accessed from our app. Please check My Fields section of the app to check all the results. The next satellite movement is expected in the next 2-5 days. Our system has processed images with the best standards.\n\nPlatforms:\nAndroid: https://play.google.com/store/apps/details?id=com.farmonaut.android\niOS: https://apps.apple.com/us/app/farmonaut/id1489095847?ls=1\nWeb App: https://farmonaut.com/web-app\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()