简体   繁体   中英

CC email and multiple TO email addresses not working

I've created a function in Python which will build an email in html format and send it anonymously through an Exchange server which accepts anonymous email sends.

However, no one in the CC list gets a copy and only the FIRST person in the TO list gets a copy. I've tried 'comma' and 'semicolon' as the email separator but neither works. Curiously, in the email received ALL of the email addresses can be seen (and in the correct format) despite the fact that only one person (the person listed first in the TO list) gets a copy.

This is my code

def send_email(sender, subject, sendAsDraft = True): # sendAsDraft not implemented

    global toEmail
    global ccEmail

    # create the email message
    htmlFile = open(saveFolder + '\\' + htmlReportBaseName + '.html',encoding = 'utf-8')
    message = htmlFile.read().replace('\n', '')
    message = message.replace(chr(8217), "'") # converts a strange single quote into the standard one
    message = message.replace("'",''') # makes a single quote html friendly
    htmlFile.close()

    # get the email body text
    emailBody = open('emailBody.txt')
    bodyText = emailBody.read()
    emailBody.close()

    now = dt.datetime.today().replace(second = 0, microsecond = 0)
    timeStr = returnReadableDate(now) # returns the date as a string in the 'normal' reading format

    # insert the current date/time into the marker in the email body
    bodyText = bodyText.replace('xxxx', timeStr)

    # insert the email body text into the HTML
    message = message.replace('xxxx', bodyText)

    msg = MIMEMultipart('Report Email')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = '; '.join(toEmail) 
    msg['Cc'] = '; '.join(ccEmail) 
    msg.add_header('Content-Type','text/html')
    
    msg.set_payload(message)

    # complete the email send
    message = ''
    try:
        smtpObj = sl.SMTP('1.2.3.4:5') # obviously IP address anonymised
        smtpObj.sendmail(sender, '; '.join(toEmail), msg.as_string()) 
        message = 'Email successfully sent'        
    except:
        message = 'Email could not be sent due to an error'
    finally:
        # write the outcome to the Instance log
        global InstanceLog
        # write the log
        log(InstanceLog, message)

    # close object and exit function    
    smtpObj.quit()

Is it possible that the issue is at the email server? That where an un-authenticated email can only be sent to one person? That would seem strange in one way, and make sense in another.

Thanks all.

Seamie

Looking at the docs , this should be a comma seperated string:

msg['To'] = ', '.join(toEmail)

Beware. send_message can use the headers of a message to build its recipient list, but sendmail requires a list of addresses or handle a string as a single recipicient address.

And the headers should contain comma instead of semicolons.

So you should have:

...
msg['To'] = ' '.join(toEmail) 
msg['Cc'] = ','.join(ccEmail) 
...

and later either:

smtpObj.sendmail(sender, toEmail + ccEmail, msg.as_string())

or:

smtpObj.send_message(msg)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM