отправка письма с файлом на python + jquery
Есть код отправки писем на python с одной почты на другую через smtplib. Кроме самих писем, мне нужно отправлять еще и приложение(картинку например). Я могу загружать и видеть любую загруженную картинку через jquery. Вопрос:
Как передать ее во Flask, чтобы ее тоже отправлять?
python
@app.route('/upload', methods=['GET','POST'])
def upload():
from email.mime.multipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
password = "your_password"
msg['From'] = "your_address"
msg['To'] = "to_address"
msg['Subject'] = "Photos"
msg.attach(MIMEImage(file("google.jpg").read()))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print "successfully sent email to %s:" % (msg['To'])
return render_template('upload.html')
upload.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<input type='file' class="form-control" id="imgInp" style="width: 80%;">
<img id="blah" src="#" alt="" style="width: 20%; height: 20%; margin-left: 10px;">
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]); // convert to base64 string
}
}
$("#imgInp").change(function() {
readURL(this);
});
</script>
</body>
Ответы (1 шт):
Автор решения: abby
→ Ссылка
Решение таково. Нужно добавить MIMEApplication
file = request.form['file']
file_to_send = MIMEApplication(open(file, 'rb').read())
file_to_send.add_header('Content-Disposition', 'attachment', filename=file)
msg.attach(file_to_send)