I want to send a single mail to multiple recipients from R. I am able to achieve this using sendmail
function but when recipients recieve the email, they see only their email address in to
field. Looks like sendmail internally loops and sends individual emails to each recipient which is not a true carbon copy
. It is important that each recipient sees all the recipients intended for their specific email (business requirement, because they need to reply to all recipients of this email). how can I achieve this using R?
my code
require(sendmailR)
to <- c("[email protected]")
header <- list(cc=c("[email protected]"))
x <- sendmail("[email protected]", 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: [email protected]
<< 250 2.1.0 [email protected]... Sender ok
>> RCPT TO: [email protected]
<< 250 2.1.5 [email protected]... 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
Output from debug option. header information is not present in the debug output.
> sendmail("[email protected]", to, "test", "testing", header=header,control=list(smtpServer=server,transport="debug"))
From: [email protected]
To: [email protected]
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--
Thanks.
The problem is caused by the usage of parameter header
instead of headers
. However, it is not that stupid typo as one might think. As we know, we can abbreviate parameter names when calling functions:
myfun <- function(xx = 1) print(xx)
myfun(x = 2)
# [1] 2
It is also possible when having ...
:
myfun <- function(xx = 1, ...) print(xx)
myfun(x = 2)
[1] 2
But in this case we have a different and uncommon order of parameters:
sendmail(from, to, subject, msg, ..., headers = list(), control = list())
which not surprisingly cause such problems:
myfun <- function(..., yy = 1) print(yy)
myfun(y = 2)
[1] 1
You need to combine all the real recipients and send the email to each one of the them separately. CC
is just informational as such; leave it out and you get BCC
So, for example:
to <- c("[email protected]") # addresses for the "To" header
cc <- c("[email protected]") # 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("[email protected]", recipients, "test", "testing", header=header, control=list(smtpServer=server,verbose=TRUE))
If you want all addresses in the To
header you could use header <- list(to=to)
instead.
Note though, above is untested. Should work in theory, but sendmailR might have other plans wrt handling some of the arguments.
To elaborate on my earlier comment. There are two parts to sending email, SMTP that's the protocol for delivering the message to the recipients, and the message content itself.
SMTP that handles the delivery looks something like:
MAIL FROM: [email protected]
RCPT TO: [email protected]
DATA
<message content here, see below>
.
Note that the FROM
and TO
here match the arguments to the sendmail()
function. That determines who receives the mail.
Now, the message content (well, just the headers) looks something like this:
From: [email protected]
To: [email protected]
CC: [email protected]
And yes the From
and To
are completely different from the previous ones. This is what the recipients get and see, but it bears no relevance to who actually gets the email. That's why spam in your inbox appears as something you've sent to someone else, even though you're the actual recipient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With