簡體   English   中英

是否可以在R中使用sendmail抄送收件人?

[英]Is it possible to cc recipients using sendmail in R?

我想一個單一的郵件發送到從R.我能夠做到這一點使用多個收件人sendmail功能,但是當收件人收到的電子郵件,他們看到的只是他們的電子郵件地址to現場。 看起來sendmail在內部循環並向每個收件人發送單獨的電子郵件,這不是真正carbon copy 每個收件人看到所有打算用於其特定電子郵件的收件人是很重要的(業務要求,因為他們需要回復此電子郵件的所有收件人)。 我如何使用R做到這一點?

我的代碼

require(sendmailR)
to <- c("vasudeva.naik@abc.com")
header <- list(cc=c("alok.jadhav@abc.com"))
x <- sendmail("toto@abc.com", to, "test", "testing", header=header,control=list(smtpServer=server,verbose=TRUE))
<< 220 equity.xyz.com ESMTP Sendmail 8.11.7p1+Sun/8.11.7; Thu, 11 Jul 2013 21:31:43 -0400 (EDT)
>> HELO  HKD03836654
<< 250 equity.xyz.com Hello HKD03836654.gbl.ad.net [169.34.175.142], pleased to meet you
>> MAIL FROM:  toto@abc.com
<< 250 2.1.0 toto@abc.com... Sender ok
>> RCPT TO:  vasudeva.naik@abc.com
<< 250 2.1.5 vasudeva.naik@abc.com... Recipient ok
>> DATA
<< 354 Enter mail, end with "." on a line by itself
>> <message data>
<< 250 2.0.0 r6C1Vh101169 Message accepted for delivery
>> QUIT
<< 221 2.0.0 equity.csfb.com closing connection

調試選項的輸出。 標頭信息在調試輸出中不存在。

> sendmail("toto@abc.com", to, "test", "testing", header=header,control=list(smtpServer=server,transport="debug"))
From: toto@abc.com
To: vasudeva.naik@abc.com
Subject: test
Date: Mon, 15 Jul 2013 02:15:29 -0000
MIME-Version: 1.0
Content-Type: multipart/mixed;             boundary="1a556aa6576e231876dabb67e5a4f58730d3a228654e14705503b6985a6a6707"

This is a message with multiple parts in MIME format.
--1a556aa6576e231876dabb67e5a4f58730d3a228654e14705503b6985a6a6707
Content-Type: text/plain; format=flowed

testing
--1a556aa6576e231876dabb67e5a4f58730d3a228654e14705503b6985a6a6707--

謝謝。

您需要合並所有真實的收件人,並將電子郵件分別發送給每個收件人。 CC只是提供信息而已。 忽略它,您會得到BCC ,例如:

to <- c("vasudeva.naik@abc.com")  # addresses for the "To" header
cc <- c("alok.jadhav@abc.com")    # addresses for the "CC" header
bcc <- c("...")                   # more addresses, but these won't be visible anywhere

recipients <- c(to, cc, bcc)  # need to send the email to everyone (FIXME: drop duplicates)
header <- list(cc=cc)         # let everyone know who else gets the mail

x <- sendmail("toto@abc.com", recipients, "test", "testing", header=header, control=list(smtpServer=server,verbose=TRUE))

如果要在“ To標頭中包含所有地址,則可以使用header <- list(to=to)

但是請注意,以上未經測試。 理論上應該可以使用,但是sendmailR可能還有其他計划來處理某些參數。


詳細闡述我先前的評論。 發送電子郵件有兩個部分,SMTP是將消息傳遞給收件人的協議,而SMTP是消息的內容本身。

處理傳遞的SMTP如下所示:

MAIL FROM: toto@abc.com
RCPT TO: vasudeva.naik@abc.com
DATA
<message content here, see below>
.

請注意,此處的FROMTO將參數與sendmail()函數匹配。 那確定誰接收郵件。

現在,消息的內容(只是標題)看起來像這樣:

From: no-reply@example.com
To: everyone@example.com
CC: everyone-else@example.com

是的, FromTo與之前的完全不同。 這是收件人收到和看到的內容 ,但與實際收到電子郵件的人無關。 這就是為什么收件箱中的垃圾郵件會顯示為已發送給其他人的內容,即使您是實際的收件人也是如此。

該問題是由於使用參數header而不是headers 但是,這並不是人們可能想到的那種愚蠢的錯字。 眾所周知,調用函數時可以縮寫參數名稱:

myfun <- function(xx = 1) print(xx)
myfun(x = 2)
# [1] 2

當有...時也可能:

myfun <- function(xx = 1, ...) print(xx)
myfun(x = 2)
[1] 2

但是在這種情況下,我們具有不同且不常見的參數順序:

sendmail(from, to, subject, msg, ..., headers = list(), control = list())

這毫不奇怪地導致了這樣的問題:

myfun <- function(..., yy = 1) print(yy)
myfun(y = 2)
[1] 1

嘗試:

> for (to in recipients) {
+ sendmail(from, to, subject, msg, control...

幾種可能性(未經測試):

  1. 將收件人列表作為郵件正文的第一行。 有點笨拙,但可以。 實際上,我已經用“密件抄送:”字段完成了此操作(這破壞了“密件抄送:”的意思,但不要緊)。

  2. 代替郵件收件人的向量,將to參數設為包含所有收件人的單個字符串,並用逗號分隔。

我以前曾遇到過這個問題,很想知道一個更直接的解決方案,但是我解決的方法是創建一個通訊組列表,然后向該通訊組列表發送一封郵件。 這樣,每個人都可以看到並可以將所有回復到列表。 希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM