파이썬: TO, CC 및 BCC로 메일을 보내는 방법은 무엇입니까?
수백 개의 전자 메일 상자에 다양한 메시지를 입력하기 위해 테스트 목적으로 smtplib를 사용하려고 했습니다.하지만 무엇보다도 저는 특정 우편함뿐만 아니라 CC와 BCC에도 메시지를 보낼 수 있어야 합니다.이메일을 보내는 동안 smtplib가 CC-ing 및 BCC-ing을 지원하지 않는 것 같습니다.
Python 스크립트에서 메시지를 보내는 CC 또는 BCC 방법에 대한 제안을 찾고 있습니다.
(그리고 테스트 환경 외부에서 스팸을 보내는 스크립트를 만들지 않습니다.)
이메일 헤더는 SMTP 서버에 중요하지 않습니다.다음에 CC 및 BCC 수신인 추가toaddrs
전자 메일을 보낼 때.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()
중요한 것은 수신자를 발송 메일 통화의 전자 메일 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()
2011년 11월에 출시된 Python 3.2부터는 smtplib에 새로운 기능이 추가되었습니다.send_message
의 대신에sendmail
To/CC/BCC를 쉽게 처리할 수 있습니다.Python 공식 e-메일 예제에서 약간의 수정을 통해 다음과 같은 이점을 얻을 수 있습니다.
# 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()
send_message는 설명서에 설명된 대로 BCC를 존중하므로 헤더를 사용하는 것이 좋습니다.
send_message는 msg로 표시될 수 있는 BCC 또는 Recent-Bcc 헤더를 전송하지 않습니다.
와 함께sendmail
다음과 같은 작업을 수행하여 메시지에 CC 헤더를 추가하는 것이 일반적이었습니다.
msg['Bcc'] = blind.email@adrress.com
또는
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"
문제는 sendmail 기능이 이러한 모든 헤더를 동일하게 처리한다는 것입니다. 즉, BCC의 목적을 무시하고 모든 수신인: 및 BCC: 사용자에게 전송됩니다.여기에 나와 있는 많은 다른 답변에서 볼 수 있듯이, 해결책은 머리글에 BCC를 포함하지 않고 대신 전달된 이메일 목록에만 포함하는 것이었습니다.sendmail
.
주의할 점은send_message
메시지 개체가 필요합니다. 즉, 클래스를 가져와야 합니다.email.message
단순히 …에 현을 두는 것이 아니라.sendmail
.
BCC 헤더를 추가하지 마십시오.
다음을 참조하십시오. http://mail.python.org/pipermail/email-sig/2004-September/000151.html
그리고 다음과 같습니다. ""메일()을 보내는 두 번째 인수, 즉 수신자가 목록으로 전달됩니다.목록에 원하는 수의 주소를 포함하여 메시지가 각 주소로 차례로 배달되도록 할 수 있습니다.봉투 정보는 메시지 헤더와 별개이므로 메시지 헤더가 아닌 메소드 인수에 포함하여 다른 사용자를 BCC할 수도 있습니다.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 수준에서는 모든 사용자가 수신인입니다.
받는 사람 - 이 받는 사람의 주소를 가진 받는 사람: 머리글이 있습니다.
CC - 이 수신자의 주소를 가진 CC: 헤더가 있습니다.
BCC - 이 수신인은 머리글에 전혀 언급되지 않았지만 여전히 수신인입니다.
가지고 계신다면,
TO: abc@company.com
CC: xyz@company.com
BCC: boss@company.com
수신자가 세 명 있습니다.전자 메일 본문의 헤더에는 받는 사람: 및 참조만 포함됩니다.
MIME 텍스트를 사용할 수 있습니다.
msg = MIMEText('text')
msg['to'] =
msg['cc'] =
그런 다음 msg.as _string(으)로 보냅니다.
https://docs.python.org/3.6/library/email.examples.html
나는 'to'와 'cc' 모두에 여러 명의 수신자가 있었기 때문에 위의 것들 중 하나도 나에게 효과가 없었습니다.그래서 저는 아래와 같이 노력했습니다.
recipients = ['abc@gmail.com', 'xyz@gmail.com']
cc_recipients = ['lmn@gmail.com', 'pqr@gmail.com']
MESSAGE['To'] = ", ".join(recipients)
MESSAGE['Cc'] = ", ".join(cc_recipients)
그리고 'cc_recipients'로 'recipients'를 확장하고 사소한 방법으로 메일을 보냅니다.
recipients.extend(cc_recipients)
server.sendmail(FROM,recipients,MESSAGE.as_string())
다음을 만들기 전까지는 효과가 없었습니다.
#created cc string
cc = ""someone@domain.com;
#added cc to header
msg['Cc'] = cc
다음과 같이 수신자 [목록]에 추가된 CC보다 더 많습니다.
s.sendmail(me, [you,cc], msg.as_string())
언급URL : https://stackoverflow.com/questions/1546367/python-how-to-send-mail-with-to-cc-and-bcc
'programing' 카테고리의 다른 글
가장 많이 발생한 값을 포함하는 모든 행 반환 (0) | 2023.07.20 |
---|---|
막대 그래프에서 사용되지 않는 수준 유지 (0) | 2023.07.20 |
com.google.common.util.current 클래스가 중복됩니다.들을 수 있는모듈 guava-20.0.jar(com.google.guava:guava:20.0)에서 미래가 발견되었습니다. (0) | 2023.07.15 |
Postgre에 대한 장고 연결SQL: "피아 인증 실패" (0) | 2023.07.15 |
.git 폴더 축소 방법 (0) | 2023.07.15 |