Mails not being sent to people in CC

2019-03-24 05:05发布

I have the following script for sending mails using python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os

FROMADDR = "myaddr@server.com"
PASSWORD = 'foo'

TOADDR   = ['toaddr1@server.com', 'toaddr2@server.com']
CCADDR   = ['ccaddr1@server.com', 'ccaddr2@server.com']

# Create message container - the correct MIME type is multipart/alternative.
msg            = MIMEMultipart('alternative')
msg['Subject'] = 'Test'
msg['From']    = FROMADDR
msg['To']      = ', '.join(TOADDR)
msg['Cc']      = ', '.join(CCADDR)

# Create the body of the message (an HTML version).
text = """Hi  this is the body
"""

# Record the MIME types of both parts - text/plain and text/html.
body = MIMEText(text, 'plain')

# Attach parts into message container.
msg.attach(body)

# Send the message via local SMTP server.
s = smtplib.SMTP('server.com', 587)
s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.login(FROMADDR, PASSWORD)
s.sendmail(FROMADDR, TOADDR, msg.as_string())
s.quit()

When I use the script, I see that the mail gets delivered to both toaddr1 and toadd2 However ccaddr1 and ccaddr2 does not receive the mail at all.

Interestingly, when I check the mails received by toaddr1 and toadd2, it shows that ccaddr1 and ccaddr2 are present in CC.

Is there any error in the script? Initially I thought that this might be an issue with my mail server. I tried it with Gmail and saw the same result. That is, no matter whether its an account in my current mail server or my Gmail account in the CC, the recipient will not receive the mail, even though the people in the 'To' field receive it properly and have the correct addresses mentioned in the CC field

3条回答
Deceive 欺骗
2楼-- · 2019-03-24 05:47

You specified the CC entries in the message, but not in the envelope. It's your job to make sure that the message is also sent to the CC and BCC entries.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-24 05:48

I think that you will need to put the CCADDR with the TOADDR when sending the mail:

s.sendmail(FROMADDR, TOADDR+CCADDR, msg.as_string())

You're correctly adding the addresses to your message, but you will need the cc addresses on the envelope too.

From the docs:

Note The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents.

查看更多
时光不老,我们不散
4楼-- · 2019-03-24 06:03
I got below error with TOADDR+CCADDR => 
TypeError: can only concatenate str (not "list") to str

I did below changes and it worked for me.
It sends email with attachment to - "To", "Cc" & "Bcc" successfully.


toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "\n  !! Hello... !!"

msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject

s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
查看更多
登录 后发表回答