sendmailr(part2):メール添付ファイルとしてファイルを送信
-
01-10-2019 - |
質問
で提供された指示に従ってください これ 関連する質問では、HTML形成されたメールメッセージを送信することができました。ここで問題は次のとおりです。このメッセージに1つ以上のファイルを(任意のタイプの)添付するには、次のコードをどのように変更する必要がありますか?
library(sendmailR)
from <- "<sendmailR@myserver.mycompany.com>"
to <- c("<someone@mycompany.com>","<anotherone@mycompany.com>")
subject <- iconv("Message Title", to = "utf8")
msg <- "<hr size='2' width='33%' style='text-align: left;'><font size='2'>
<i>This email was sent automatically using <a href='http://finzi.psych.upenn.edu/R/library/sendmailR/html/00Index.html' rel='nofollow' target='_blank'>sendmailR</a>.<br>
Please do not reply directly to this e-mail.</i></font>"
msg <- iconv(msg, to = "utf8")
sapply(to,function(x) sendmail(from, x, subject, msg, control=list(smtpServer="###.###.###.###"), headers=list("Content-Type"="text/html; charset=UTF-8; format=flowed")))
解決
Mailrパッケージ付き(https://github.com/rpremraj/mailr)、HTMLメールを送信して、以下のように簡単にファイルを添付することができます。
send.mail(from = "sender@gmail.com",
to = c("recipient1@gmail.com", "recipient2@gmail.com"),
subject = "Subject of the email",
body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>",
html = TRUE,
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
attach.files = c("./download.log", "upload.log"),
authenticate = TRUE,
send = TRUE)
編集(2014-05-13):
Mailrは、異なる文字エンコーディングを許可するために更新されました。以下は、メッセージをUTF-8として送信する例です。
send.mail(from = "Sender Name <sender@gmail.com>",
to = "recipient@gmail.com",
subject = "A quote from Gandhi",
body = "In Hindi : थोडा सा अभ्यास बहुत सारे उपदेशों से बेहतर है।
English translation: An ounce of practice is worth more than tons of preaching.",
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = T),
authenticate = TRUE,
send = TRUE)
他のヒント
(少なくとも私にとっては)機能:
sendMessage<-function(contents,subject,from,to,attMIME,attachment,control){
msg<-list(contents,sendmailR:::.file_attachment(attachment,attachment,attMIME));
sendmail(from=from,to=to,subject=subject,msg=msg,control=control);
}
このように使用できます:
png('a.png');hist(rnorm(700));dev.off()
sendMessage('Here you have a nice histogram:',
'Nice picture',
'from@example.com',
'to@example.com',
'image/png',
'a.png',list(smtpServer="..."))
この例から送信されたメッセージは、おそらく短いテキストであり、全体像であるため、おそらくスパムとしてマークされることに注意してください。そうでない場合は、メッセージのテキストバージョンも追加することを検討できます。
編集(現在はあまり関連性がありません):mimeメッセージの作成方法に関する最も深い洞察を見つけることができます ここ.
現在のバージョンに注意してください sendmailR
作成して、箱から出して添付ファイルをサポートします msg
のリスト mime_type
オブジェクト、つまりあなたは今でしょう
sendmail( from,to,subject,
msg=list(mime_part("Here's an attachment for you!"),
mime_part(attachmentFileName)), control, headers)`
私はこれにrを使うことをあきらめます。 Pythonでこれを行うための作業、クロスプラットフォーム、安定したソリューションが存在し、RからPythonに電話することができます。
Pythonプログラムで混合エフェクトモデルを適合しなければならなかった場合は、Rに電話してrを呼び出します。Rで電子メールを送信するなどのシステムタスクを実行したい場合は、Pythonに電話して電話します。まだわからない場合は学ぶ価値があります。
複数の添付ファイル(1つのCSV、1つのPDF)を備えたR(パッケージSendMailrで使用可能)でsendmail()を使用するなど、毎日のバッチジョブに設定されている例を示します。
ファイル名で参照する日付情報を設定します。
> yesterday_date_stuff <- new.env()
> yesterday_date_stuff[['month']] <- strftime(Sys.Date()-1, format="%m")
> yesterday_date_stuff[['day']] <- strftime(Sys.Date()-1, format="%d")
> yesterday_date_stuff[['year']] <- strftime(Sys.Date()-1, format="%y")
> yesterday_date_stuff$month
[1] "03"
> yesterday_date_stuff$day
[1] "29"
> yesterday_date_stuff$year
[1] "17"
この投稿の最後に、sendmail()関数に必要な情報の一部を作成します。
> from <- "youremail@whateveryourmailserveris.com"
> to <- c("person_A_to_send_email_to@whatever.com", "person_B_to_send_email_to@whatever.com", "person_C_to_send_email_to@whatever.com")
> subject <- paste("whatever you want subject line to read for batch job analyzing data for ", yesterday_date_stuff$month, "/", yesterday_date_stuff$day, "/", yesterday_date_stuff$year, sep="")
> body <- "Text to insert into the body of your email"
ここでメールサーバーを指定します:
> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")
添付ファイル1パスと名前を定義します。
> attachmentPath1 <- paste("file1name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".csv", sep="")
> attachmentName1 <- paste("file1name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".csv", sep="")
添付ファイルを定義する1オブジェクト:
> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)
添付ファイル2パスと名前を定義します。
> attachmentPath2 <- paste("file2name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".pdf", sep="")
> attachmentName2 <- paste("file2name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".pdf", sep="")
添付ファイル2オブジェクトを定義します。
> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)
次に、メールの本文を添付ファイルと組み合わせます。
> bodyWithAttachment <- list(body,attachmentObject1, attachmentObject2)
> bodyWithAttachment
[[1]]
[1] "Text to insert into the body of your email"
[[2]]
<environment: 0x000000004efff188>
attr(,"class")
[1] "mime_part"
[[3]]
<environment: 0x00000000407a1b68>
attr(,"class")
[1] "mime_part"
sendmail()関数を使用して電子メールを送信します:
> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)