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?
The SMTP protocol uses an "envelope" that specifies each of the addresses to which the server will deliver the message. The envelope will never be visible to the end user, because once the message reaches its destination, the envelope is destroyed.
On the other hand, inside that "envelope" goes the message, which contains many headers, among which is the "To:" header, in which we usually see the list of recipients.
However, nothing forces both lists to coincide. A different recipient list may appear on the envelope than the one that appears later in the message headers. This is what happens if you use the Bcc field when writing a message. The addresses you put in Bcc will be used in the envelope, and the Bcc field itself will disappear from the message so those who receive that message won't know who else received it.
In your case you create the correct "To:" header on these lines:
which will be used only for the message, but not for the envelope. The envelope is created instead when you call this function:
The first parameter of
sendmail()
will be the email address to which the message should be delivered if it "bounces" (cannot be delivered to destination). You can put the same as in the "From:" header of the message, or something else.The second parameter is the recipient or list of recipients. This python function will assume a single recipient if what you pass to it is a string (as is your case), or multiple recipients if it's a list. But in your case you have used it wrong, since you have sent it a list of users in the form of a string. What happens in this case depends on how the server you're connected to handles that comma-separated string. It is seen that Gmail keeps only the first address.
Therefore, the solution will be to pass a list to this function instead of a string: