3
I am beginner in sending emails via script and am facing a problem. I use Python 3.5. When sending attachments with the following script, they lose the extension and are renamed:
def enviaremail(usuario,senha,mensagem,listadestinatarios):
from smtplib import SMTP
smtp=SMTP('smtp.live.com',587)
smtp.starttls()
smtp.login(usuario,senha)
smtp.sendmail(usuario,listadestinatarios,mensagem)
smtp.quit()
print('E-mail enviado com sucesso')
def anexoimagem(path):
from email.mime.image import MIMEImage
with open(path,'rb') as f:
mime=MIMEImage(f.read(),subtype='jpg')
return mime
msg=anexoimagem('foto.jpg')
msg['From']='[email protected]'
msg['To']='[email protected]'
msg['Subject']='testando mensagem com anexo'
enviaremail('[email protected]','xxxx',msg.as_string,['[email protected]']
For example, the photo.jpg file appears as ATT0001 in the email. Renamed and without the . jpg extension it has. The same is true for text and audio files. How do I keep your name and extension when sending as attachments?
I wanted something related to your first suggestion, but it was a mistake. I said that the "name" argument does not exist in init from Mimeimage. Maybe you don’t have something like this that works, @?
– Benedito
Well, I just tested it here (again) and it worked. And my version is also 3.5. There must be something wrong with your code. I will edit the answer and add the test code I used.
– Luiz Vieira
I tried your second tip and it worked. Even the first one didn’t work for me, it should work for others, since it worked on your script. Anyway, thank you. The second tip solved my problem.
– Benedito