def send_report(file): msg = EmailMessage() msg['from'] = EMAIL_SETTING['username'] msg['to'] = EMAIL_SETTING['receiver'] msg['cc'] = EMAIL_SETTING['cc'] msg['subject'] = 'xxxx' msg.set_boundary(EMAIL_CONTENT, 'html', 'utf-8') ctype, encoding = guess_type(file) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/') with open(file, 'rb') as f: msg.add_attachement(f.read(), maintype=maintype, subtype=subtype, filename=os.path.basename(file)) with SMTP('') as smtp_client: import smtplib try: smtp_client.login(EMAIL_SETTING['username'], EMAIL_SETTING['password']) smtp_client.send_message( EMAIL_SETTING['sender'], EMAIL_SETTING['receiver'] + EMAIL_SETTING['cc'], msg.as_string()) except smtplib.SMTPAuthenticationError: print('用户账号或密码出错')
def generate_email(sender, recipient, subject = "", body = "", attachement_path = ""): """ Constructs an email message based on received info with an optional attachement """ message = EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = subject message.set_content(body) if not attachement_path: attachement_filename = os.path.basename(attachement_path) mime_type, _ = mimetypes.guess_type(attachement_path) mime_type, mime_subtype = mime_type.split("/", 1) with open(attachement_path, "rb") as attachement: message.add_attachement(attachement.read(), maintype=mime_type, subtype=mime_subtype, filename=attachement_filename) return message
import os import smtplib import imghdr from email.message import EmailMessage EMAIL_ADDRESS = os.environ.get('EMAIL_USER') EMAIL_PASSWORD = os.environ.get('EMAIL_PASS') contacts = ['*****@*****.**','*****@*****.**'] msg = EmailMessage() msg['Subject'] = 'New python project' msg['From'] = EMAIL_ADDRESS msg['To'] = ','.join(contacts) msg.set_content('Image Attached') files = ['pypro1.png','pypro2.png'] for file in files: with open(file,'rb') as f: file_data = f.read() file_name = f.name msg.add_attachement(file_data,maintype='application',subtype='octet-stream', filename=file_name) with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(EMAIL_PASSWORD,EMAIL_PASSWORD) smtp.send_message(msg)
speak(f"Enter {i} image") img.append(input()) mssg = EmailMessage() mssg['Subject'] = sub mssg['From'] = config.Email mssg['To'] = ','.join(li) mssg.set_content(msg) for file in img: with open(f'C:\\Users\\Amaan\\Pictures\\Saved Pictures\\{file}','rb') as f: f_data = f.read() f_type = imghdr.what(f.name) f_name = f.name mssg.add_attachement(f_data,maintype="image",subtype=f_type,filename=f_name) with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp: smtp.login(config.Email,config.Password) smtp.send_message(mssg) print("Email sent successfully..") print("Email sent successfully..") except: print("sorry,Not able to send email") speak("sorry,Not able to send email") elif user1 == "D" or user1 == "d": try: doc = [] re = int(input("Enter the no of receiver email address: "))