22

この関連する質問に記載されている指示に従って、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")))
4

5 に答える 5

9

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)
于 2014-05-08T20:29:46.287 に答える
8

機能する(少なくとも私にとっては)機能:

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="..."))

この例で送信されたメッセージは、短いテキストと大きな画像であるため、おそらくスパムとしてマークされることに注意してください。ただし、より大きなメッセージや、たとえば pdf の添付ファイルの場合は通過する必要があります。そうでない場合は、メッセージのテキスト バージョンも追加することを検討してください。

編集 (現在はあまり関係ありません): MIME メッセージの作成方法に関する最も深い洞察は、ここにあります。

于 2010-08-26T08:31:54.343 に答える
6

オブジェクトのリストを作成することにより、現在のバージョンのsendmailRサポート添付ファイルをすぐに使用できることに注意してください。msgmime_type

sendmail( from,to,subject,
          msg=list(mime_part("Here's an attachment for you!"), 
          mime_part(attachmentFileName)), control, headers)`
于 2013-01-17T09:43:19.390 に答える
3

私はこれにRを使うのをあきらめるでしょう。Pythonでこれを行うための、機能するクロスプラットフォームの安定したソリューションが存在し、RからPythonを呼び出すことができます。

混合効果モデルをPythonプログラムに適合させる必要がある場合は、Rを呼び出して実行します。Rで電子メールを送信するなどのシステムタスクを実行する場合は、Pythonを呼び出して実行します。あなたがまだそれを知らないならば、それは学ぶ価値があります。

于 2010-08-26T07:14:18.033 に答える