def quote_request(): if request.method=="GET": return render_template("quote_request.html") else: #request.method=="POST" msg = MIMEMultipart('related') session = smtplib.SMTP('smtp.gmail.com', 587) session.ehlo() session.starttls() session.ehlo() session.login('*****@*****.**', os.environ['GMAIL_PASSWORD']) msg['From'] = "*****@*****.**" msg['Subject'] = "Someone has filled out the quote request form!" msg['To'] = "[email protected];[email protected];[email protected];[email protected];[email protected];[email protected]" body = "Name: " + request.form['name'] + "<br>" + "Procedure: " + request.form['procedure'] + "<br>" + "Weight: " + request.form['weight'] + "<br>" + "Zip: " + request.form['zip'] + "<br>" + "Breed: " + request.form['breed'] + "<br>" + "Age: " + request.form['age'] + "<br>" + "Sex: " + request.form['sex'] + "<br>" + "Customer's email address: " + request.form['email_addr'] + "<br>" + "Customer's First Name: " + request.form['user_fname'] + "<br>" + "Customer's Last Name: " + request.form['user_lname'] content = MIMEText(body, 'html') msg.attach(content) f = request.files['the_file'] f = MIMEImage(f.read()) msg.attach(f) session.sendmail("*****@*****.**", recipients, msg.as_string()) flash("Thanks! Your quote request has been successfully submitted.") return render_template("quote_request.html")
def send_template_email(subject, body, to_email, from_email=settings.EMAIL_HOST_USER): email = EmailMultiAlternatives(subject, body, settings.EMAIL_HOST_USER , [to_email]) email.content_subtype='html' email.mixed_subtype = 'related' logo_path = os.path.join(settings.BASE_DIR, 'static/images/logo.png') with open(logo_path, 'rb') as logo_image: logo_image = MIMEImage(logo_image.read()) logo_image.add_header('Content-ID', '<logo.png>') email.attach(logo_image) email.send()
def send_email(current_time): filename = current_time + '.jpg' msg = MIMEMultipart() msg['Subject'] = 'Motion detected at the door at ' + current_time msg['From'] = from_address msg['To'] = to_addresses with open(filename, 'rb') as pic: pic = MIMEImage(pic.read()) msg.attach(pic) server = smtplib.SMTP('smtp-mail.outlook.com', 587) server.starttls() server.login(from_address, from_password) server.ehlo() server.send_message(msg, from_address, to_addresses) server.quit()
def email_send(to,file): current_time = str(datetime.datetime.now()) current_time = current_time[0:19] msg = MIMEMultipart() msg['Subject'] = 'ALERT - AT '+current_time+' THE POST HAS ARRIVED' msg['From'] = email msg['To'] = to with open(takepic.file, 'rb') as pic: pic = MIMEImage(pic.read()) msg.attach(pic) server = smtplib.SMTP('smtp.gmail.com',587) server.starttls() server.login(email,password) server.ehlo() server.send_message(msg) server.quit()
with open("template.html", "r") as html: template = Template(html.read()) date = datetime.now().strftime("%d/%m/%Y") message_body = template.safe_substitute(name="Misa", date=date) msg = MIMEMultipart() msg["from"] = "Yuuki" msg["to"] = my_email msg["subject"] = "Attention this is a test email" body = MIMEText(message_body, "html") msg.attach(body) with open("a1.jpg", "rb") as img: img = MIMEImage(img.read()) msg.attach(img) port = 587 smtp_server = "smtp.gmail.com" context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: try: server.ehlo() server.starttls(context=context) server.login(my_email, my_passwd) server.send_message(msg) print("Success") except Exception as e: print("Error:", e)
from email.mime.image import MIMEImage import smtplib from config import email, password, sender, subject path = 'useful-modules/email' with open(f'{path}/template.html', 'r') as html: template = Template(html.read()) current_date = datetime.now().strftime('%d/%m/%Y %H:%M:%S') page_body = template.substitute(name='Luiz Otavio', date=current_date) message = MIMEMultipart() message['from'] = sender message['to'] = email message['subject'] = subject body = MIMEText(page_body, 'html') message.attach(body) with open(f'{path}/image.jpeg', 'rb') as image: image = MIMEImage(image.read()) message.attach(image) with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp: smtp.ehlo() smtp.starttls() smtp.login(email, password) smtp.send_message(message) print('Email successfully sent')
def send_notification_to_client(mailer, website_json): receiver = os.environ["EMAIL_RECEIVER"] client_email = website_json["email"] client_name = website_json["name"] # Create auto-reply to confirm that client's message did send # Will try to send html first; send text if not possible pref_message = '' if website_json["pref_contact"] == 'both': pref_message = 'by phone call and/or email' elif website_json["pref_contact"] == 'phone': pref_message = 'by phone call' else: pref_message = 'by email' reply = MIMEMultipart("mixed") reply["Subject"] = "MFP Got Your Message!" reply["From"] = f"Mims Painting<{receiver}>" reply["Reply-To"] = receiver reply["To"] = f"{client_name}<{client_email}>" reply_alt = MIMEMultipart('alternative') text = f'Hi {client_name},\n\n \ Thank you for contacting us at Mims Family Painting. We will review\ your message and reply to you within 2 business days (Monday - Friday) {pref_message}.\n\n \ We look forward to speaking with you soon.\n\n \ Best regards,\n \ Mims Family Painting' reply_alt.attach(MIMEText(text, "plain")) image_cid = email.utils.make_msgid(domain='mimspainting.com')[1:-1] reply_rel = MIMEMultipart('related') html = """\ <html> <table style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif; font-size: 16px; border-collapse: separate; border-spacing: 0 1em; max-width: 680px; min-width: 375px; border: 20px solid black; padding: 16px;"> <tbody style=" font-size: 16px; box-sizing: border-box; margin: 0px auto; color: black;"> <tr> <td> <table> <tbody> <td style="width: 25%;"></td> <td style="width: 50%;"> <img style="background: rgba(141, 141, 141, 0.5); border: black solid 4px; padding: 8px; box-sizing: border-box; display: block; width: 100%; " src="cid:{img}" alt="Mims Family Painting" /> </td> <td style="width: 25%;"></td> </tbody> </table> </td> </tr> <tr> <td> <p>Hi {name},</p> </td> </tr> <tr> <td> <p> Thank you for contacting us at Mims Family Painting. We will review your message and reply to you within 2 business days (Monday - Friday) by phone and/or email. </p> </td> </tr> <tr> <td> <p>We look forward to speaking with you soon.</p> </td> </tr> <tr> <td> <p>Best regards,</p> <p>Mims Family Painting</p> </td> </tr> </tbody> </table> </html> """.format(name=client_name, pref_message=pref_message, img=image_cid) reply_rel.attach(MIMEText(html, "html")) with open(os.getcwd() + '/client/public/assets/img/NEWNEWLOGO.png', 'rb') as img: maintype, subtype = mimetypes.guess_type(img.name) img = MIMEImage(img.read(), subtype, cid=image_cid) img.add_header('Content-ID', f'<{image_cid}>') reply_rel.attach(img) reply_alt.attach(reply_rel) reply.attach(reply_alt) raw_string = base64.urlsafe_b64encode(reply.as_string().encode()).decode() try: mailer.users().messages().send(userId='me', body={ 'raw': raw_string }).execute() return 200 except Exception as e: raise RuntimeError( "Failed to send email to client. Email address likely at fault.")
nome="Gabriel", data=data_atual) #Gera erro caso nao use o placeholder $variavel # corpo_msg = template.safe_substitute(nome="Renan", data=data_atual) #Nao gera erro caso nao use o placeholder $variavel msg = MIMEMultipart() msg['From'] = "Renan Bertolotti" #Meu nome msg['To'] = email_cliente1 #Email do cliente msg["Subject"] = 'Atenção: este é um e-mail de testes.' #para escrever uma mensagem é so usar a funcao abaixo e escrever a mensagem como argumento #corpo = MIMEText("ola Mundo") #Ou entao pode enviar o corpo html utilizando templates tambem, para isso fazer: corpo = MIMEText(corpo_msg, 'html') msg.attach(corpo) #Enviando imagem em anexo with open(r'C:\Users\Reinan\Pictures\.thumbnails\7140.jpg', 'rb') as imagem: imagem = MIMEImage(imagem.read()) msg.attach(imagem) with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp: try: smtp.ehlo() smtp.starttls() smtp.login("*****@*****.**", "#Renan#123") smtp.send_message(msg) print("Email enviado com sucesso.") except Exception as e: print("Email nao enviado...") print("Erro:", e)