0
At the moment I’m developing an application that uses a JS code to generate a photo of a div and a python script that sends an email with a photo attached, I wonder if there is a viable way to perform an integration, where I press a button on the screen, which calls the first JS script and then executes the Python script. I tried using Brython but could not import MIME libraries...
Code in JS:
function screenshot(){
html2canvas(document.getElementById('photo')).then(function(canvas){
document.body.appendChild(canvas);
onRendered:
var a = document.createElement('a');
a.href=canvas.toDataURL("image/png");
a.download="outweb.png";
a.click();
})
}
python code:
import smtplib
import imgkit
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
try:
smtp_server = 'smtp.gmail.com'
smtp_port = 587
acc_addr = '[email protected]'
acc_pwd = 'senha'
to_addr = '[email protected]'
subject = 'Teste de envio!'
body = 'Este é um teste de envio de email via Python!'
#Gera a imagem da tela do html
#Referenciando o arquivo que está junto com o script
imgkit.from_file('index.html', 'outweb.jpg')
# Configura o servidor de envio (SMTP)
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(acc_addr, acc_pwd)
# Cria o documento com várias partes
msg = MIMEMultipart()
msg["From"] = acc_addr
msg["To"] = to_addr
msg["Subject"] = subject
# Anexa a imagem
imgFilename = 'Gauge.jpg' # Repare que é diferente do nome do arquivo local!
with open('outweb.jpg', 'rb') as f:
msgImg = MIMEImage(f.read(), name=imgFilename)
msg.attach(msgImg)
# Anexa o corpo do texto
msgText = MIMEText('<b>{}</b><br><img src="cid:{}"><br>'.format(body, imgFilename), 'html')
msg.attach(msgText)
# Envia!
server.sendmail(acc_addr, to_addr, msg.as_string())
server.quit()
print('\nEmail enviado com sucesso!')
except:
print('\nErro ao enviar email!')
Hi Heitor, I had decided to integrate Flask in this application, but it caught my attention the use of other resources, such as sending emails by Javascript itself. For this, I am currently studying about the implementation of SMTPJS.
– Kayama
Show Kayama !! I ask you to think about the architecture of your system, I advise that the sending of emails stay in the backend. If you want to do in JS, you will have to use Node JS. Hugs
– Heitor Penha do Carmo