mauguerra
mauguerra

Reputation: 3858

Python sendmail with Cc

I already programmed a function which sends mails with atachments, images on text and other things, but now I need the function to use de Cc (Carbon Copy) function in order to send copies to different emails.

I have done some changes on the function and it works but not as I want.

THe email is sent to the address ("toaddr") and the mail shows that there are other emails added as Cc("tocc") emails, but the Cc emails do not recieve the email.

To be more clear (because I think I am not being very clear) here is an example:

Sender: [email protected]
Receiver: [email protected]
Copied: [email protected]

[email protected] receives the email and can see that [email protected] is copied on it.
[email protected] does not get the email.
if [email protected] reply to all the email, THEN cc@hotmail gets the email.

Can anyone help me telling me what do I need to change on the function?? I guees the problem is with the server.sendmail() function

This is my function:

def enviarCorreo(fromaddr, toaddr, tocc, subject, text, file, imagenes):
    msg = MIMEMultipart('mixed')
    msg['From'] = fromaddr
    msg['To'] = ','.join(toaddr)
    msg['Cc'] = ','.join(tocc)         # <-- I added this
    msg['Subject'] = subject
    msg.attach(MIMEText(text,'HTML'))
    #Attached Images--------------
    if imagenes:
       imagenes = imagenes.split('--')
       for i in range(len(imagenes)):   
        adjuntoImagen = MIMEBase('application', "octet-stream")
        adjuntoImagen.set_payload(open(imagenes[i], "rb").read())
        encode_base64(adjuntoImagen)
        anexoImagen = os.path.basename(imagenes[i])
        adjuntoImagen.add_header('Content-Disposition', 'attachment; filename= "%s"' % anexoImagen)
        adjuntoImagen.add_header('Content-ID','<imagen_%s>' % (i+1))
        msg.attach(adjuntoImagen)   
    #Files Attached ---------------
    if file:
       file = file.split('--')
       for i in range(len(file)):
        adjunto = MIMEBase('application', "octet-stream")
        adjunto.set_payload(open(file[i], "rb").read())
        encode_base64(adjunto)
        anexo = os.path.basename(file[i])
        adjunto.add_header('Content-Disposition', 'attachment; filename= "%s"' % anexo)
        msg.attach(adjunto)
    #Send ---------------------
    server = smtplib.SMTP('localhost')
    server.set_debuglevel(1)
    server.sendmail(fromaddr,[toaddr,tocc], msg.as_string())    #<-- I modify this with the tocc
    server.quit()
    return

Upvotes: 1

Views: 3895

Answers (1)

John Gaines Jr.
John Gaines Jr.

Reputation: 11534

In your sendmail call, you're passing [toaddr, tocc] which is a list of lists, have you tried passing toaddr + tocc instead?

Upvotes: 4

Related Questions