IT story

파이썬 : TO, CC 및 BCC로 메일을 보내는 방법은 무엇입니까?

hot-time 2020. 9. 4. 08:06
반응형

파이썬 : TO, CC 및 BCC로 메일을 보내는 방법은 무엇입니까?


수백 개의 이메일 상자를 다양한 메시지로 채우려면 테스트 목적으로 필요하며이를 위해 smtplib를 사용하려고했습니다. 그러나 무엇보다도 특정 메일 함뿐만 아니라 참조 및 숨은 참조로 메시지를 보낼 수 있어야합니다. smtplib 가 이메일을 보내는 동안 CC-ing 및 BCC-ing을 지원하는 것처럼 보이지 않습니다 .

python 스크립트에서 메시지를 보내는 CC 또는 BCC를 수행하는 방법에 대한 제안을 찾고 있습니다.

(그리고 — 아니요, 테스트 환경 외부에있는 사람에게 스팸을 보내는 스크립트를 작성하지 않습니다.)


이메일 헤더는 smtp 서버에 중요하지 않습니다. 이메일을 보낼 때 참조 및 숨은 참조 수신자를 추가하기 만하면됩니다. CC의 경우 CC 헤더에 추가합니다.

toaddr = 'buffy@sunnydale.k12.ca.us'
cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us']
bcc = ['chairman@slayerscouncil.uk']
fromaddr = 'giles@sunnydale.k12.ca.us'
message_subject = "disturbance in sector 7"
message_text = "Three are dead in an attack in the sewers below sector 7."
message = "From: %s\r\n" % fromaddr
        + "To: %s\r\n" % toaddr
        + "CC: %s\r\n" % ",".join(cc)
        + "Subject: %s\r\n" % message_subject
        + "\r\n" 
        + message_text
toaddrs = [toaddr] + cc + bcc
server = smtplib.SMTP('smtp.sunnydale.k12.ca.us')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()

중요한 것은 수신자를 sendmail 호출에서 이메일 ID 목록 으로 추가하는 것입니다 .

import smtplib
from email.mime.multipart import MIMEMultipart

me = "user63503@gmail.com"
to = "someone@gmail.com"
cc = "anotherperson@gmail.com,someone@yahoo.com"
bcc = "bccperson1@gmail.com,bccperson2@yahoo.com"

rcpt = cc.split(",") + bcc.split(",") + [to]
msg = MIMEMultipart('alternative')
msg['Subject'] = "my subject"
msg['To'] = to
msg['Cc'] = cc
msg.attach(my_msg_body)
server = smtplib.SMTP("localhost") # or your smtp server
server.sendmail(me, rcpt, msg.as_string())
server.quit()

숨은 참조 헤더를 추가하지 마세요.

이것을보십시오 : http://mail.python.org/pipermail/email-sig/2004-September/000151.html

그리고 이것은 : "" "sendmail ()에 대한 두 번째 인수 인 수신자가 목록으로 전달된다는 점에 유의하십시오. 목록에 여러 주소를 포함하여 메시지가 차례로 각 주소로 전달되도록 할 수 있습니다. 정보는 메시지 헤더와 분리되어 있으므로 누군가를 메소드 인수에 포함하고 메시지 헤더에는 포함하지 않음으로써 숨은 참조를 할 수도 있습니다. "" "from http://pymotw.com/2/smtplib

toaddr = 'buffy@sunnydale.k12.ca.us'
cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us']
bcc = ['chairman@slayerscouncil.uk']
fromaddr = 'giles@sunnydale.k12.ca.us'
message_subject = "disturbance in sector 7"
message_text = "Three are dead in an attack in the sewers below sector 7."
message = "From: %s\r\n" % fromaddr
    + "To: %s\r\n" % toaddr
    + "CC: %s\r\n" % ",".join(cc)
    # don't add this, otherwise "to and cc" receivers will know who are the bcc receivers
    # + "BCC: %s\r\n" % ",".join(bcc)
    + "Subject: %s\r\n" % message_subject
    + "\r\n" 
    + message_text
toaddrs = [toaddr] + cc + bcc
server = smtplib.SMTP('smtp.sunnydale.k12.ca.us')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()

TO, CC 및 BCC의 구분은 텍스트 헤더에서만 발생합니다. SMTP 수준에서는 모든 사람이받는 사람입니다.

TO-이 수신자의 주소가있는 TO : 헤더가 있습니다.

CC-이 수신자 주소가 포함 된 CC : 헤더가 있습니다.

BCC - This recipient isn't mentioned in the headers at all, but is still a recipient.

If you have

TO: abc@company.com
CC: xyz@company.com
BCC: boss@company.com

You have three recipients. The headers in the email body will include only the TO: and CC:


You can try MIMEText

msg = MIMEText('text')
msg['to'] = 
msg['cc'] = 

then send msg.as_string()

https://docs.python.org/3.6/library/email.examples.html


It did not worked for me until i created:

#created cc string
cc = ""someone@domain.com;
#added cc to header
msg['Cc'] = cc

and than added cc in recipient [list] like:

s.sendmail(me, [you,cc], msg.as_string())

As of Python 3.2, released Nov 2011, the smtplib has a new function send_message instead of just sendmail, which makes dealing with To/CC/BCC easier. Pulling from the Python official email examples, with some slight modifications, we get:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())

# me == the sender's email address
# you == the recipient's email address
# them == the cc's email address
# they == the bcc's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
msg['Cc'] = them
msg['Bcc'] = they


# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

Using the headers work fine, because send_message respects BCC as outlined in the documentation:

send_message does not transmit any Bcc or Resent-Bcc headers that may appear in msg


With sendmail it was common to add the CC headers to the message, doing something such as:

msg['Bcc'] = blind.email@adrress.com

Or

msg = "From: from.email@address.com" +
      "To: to.email@adress.com" +
      "BCC: hidden.email@address.com" +
      "Subject: You've got mail!" +
      "This is the message body"

The problem is, the sendmail function treats all those headers the same, meaning they'll get sent (visibly) to all To: and BCC: users, defeating the purposes of BCC. The solution, as shown in many of the other answers here, was to not include BCC in the headers, and instead only in the list of emails passed to sendmail.

The caveat is that send_message requires a Message object, meaning you'll need to import a class from email.message instead of merely passing strings into sendmail.

참고URL : https://stackoverflow.com/questions/1546367/python-how-to-send-mail-with-to-cc-and-bcc

반응형