简体   繁体   中英

Why email is not being send to CC list?

I have the following code where the Cc email list is not working meaning emails are not received to the folks/groups in Cc list,email to To list is working,I just can't figure out what is wrong?any guidance on how to debug and fix it?

import time,smtplib
import pprint,logging,os,time,json
from subprocess import Popen, PIPE, call
from pprint import pprint
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase

email = 'username2@company.com'
gerriturl = ""

def sendEmail2(type,data):
    global originalchange
    global gerriturl,email,username1
    body = '''%s''' % (data)
    #msg = MIMEMultipart(body)
    msg = MIMEMultipart()
    sender = 'techci@company.com'
    receivers = []
    cc = ['username1@company.com']
    REPLY_TO_ADDRESS = 'embedded-tech-integrators@group.company.com'
    if type =='failure':
        print("Inside  failure email %s"%email)
        b = '\U0001F6A8'
        print("b.decode('unicode-escape')")
        receivers.append('username2@company.com')
        #receivers.append(email)
        print('%s'%receivers)
        msg['Subject'] = '%s AUTO  FAILED FOR GERRIT %s :PLEASE TAKE IMMEDIATE ACTION!!!'%(b.decode('unicode-escape'),gerriturl)
    msg['From'] = sender
    msg['To'] = ', '.join(receivers)
    msg['Cc'] = ', '.join(cc)
    msg["Content-Type"] = "text/html"
    try:
        mail = smtplib.SMTP('relay.company.com', 25)
        msg.attach(MIMEText(body, 'html'))
        msg.add_header('reply-to', REPLY_TO_ADDRESS)
        print('Email sent successfully %s %s'%(receivers,cc))
    except Exception as e:
        logger.error('Problem sending email')
        logger.error('%s' % e)
def main():
    data = "THIS IS A TEST EMAIL"
    sendEmail2('failure',data)

if __name__ == "__main__":
    main()

The important thing is to pass the complete list of recipient email addresses to the sendmail() method - you need to concatenate the email addresses for the To , CC and BCC fields into a single list, not a string.

import smtplib

def send_email(subject, body_text, emails_to, emails_cc=[], emails_bcc=[]):
    # Configuration
    host = "localhost"
    port = 1025
    from_addr = "nobody@somewhere.com"
    # Build the email body
    body = "\r\n".join([
            "From: %s" % from_addr,
            "To: %s" % ', '.join(emails_to),
            "CC: %s" % ', '.join(emails_cc),
            "Subject: %s" % subject ,
            "",
            body_text
            ])
    # Prepare the complete list of recipient emails for the message envelope
    emails = emails_to + emails_cc + emails_bcc
    # Connect to the server
    server = smtplib.SMTP(host, port)
    # Send the email
    server.sendmail(from_addr, emails, body)
    # Close the server connection
    server.quit()

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