Good afternoon community. I am creating a script in python 2.7.13 to send mail to multiple destinations with an attached file. But it only sends it to the first one on the mailing list I have. Despite this, the other emails on my list appear in the email that arrives in my inbox from my script.
I import what is necessary to create my script.
# send_attachment.py
# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import mimetypes
Here is I create my target mailing list.
msg = MIMEMultipart()
Destino=['[email protected]','[email protected]','[email protected]','[email protected]' ]
password = "mipassword"
msg['From'] = "[email protected]"
msg['To'] = ','.join(Destino)
msg['Subject'] = "Enviando un correo a mas de un destino"
I check and attach the type of file sent
ctype, encoding = mimetypes.guess_type("prueba1.csv")
if ctype is None or encoding is not None:
ctype="applecation/actet-stream"
maintype, subtype = ctype.split("/",1)
#Se adjunta archivo y se configura
fp=open("prueba1.csv","rb")
adjunto=MIMEBase(maintype,subtype)
adjunto.set_payload(fp.read())
fp.close()
encoders.encode_base64(adjunto)
adjunto.add_header("Content-Disposition","adjunto",filename="prueba1.csv")
msg.attach(adjunto)
I create the server and send the mail (with a mail message sent successfully)
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'])
The problem is not with the attached file, but that it is only sent to the first email on my list.
Does anyone have an idea what could happen? or how to fix it?