繁体   English   中英

将单个文件附加到电子邮件

[英]Attaching a single file to an e-mail

请原谅我。 当我试图研究这个问题时,我最终会看到我根本无法理解的代码。 我有大约3个小时的Python经验,我可能会尝试超过我能处理的。

问题很简单。 我可以成功地从R(我的分析软件)调用Python来发送电子邮件。 添加消息,主题,来自和我可以执行的字段。 我希望能够发送附件。 如果我只发送一个附件,生活会很棒。

我到目前为止的代码是

import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import email.utils

fromaddr = 'someone@gmail.com'
toaddrs  = 'recipient@gmail.org'
msg = MIMEMultipart(MIMEText('This is the body of the e-mail'))
msg['From'] = email.utils.formataddr(('Benjamin Nutter', fromaddr))
msg['To'] = email.utils.formataddr(('Benjamin Nutter', toaddrs))
msg['Subject'] = 'Simple test message'
f = 'filename.pdf'
part=MIMEBase('application', 'octet-stream')
part.set_payload(open(f, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename=\"%s\"' %    os.path.basename(f))
msg.attach(part)

"username = 'user'
"password = 'pw'

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()

当我运行此代码时,我得到预期的消息字符串有效负载:[type'list'](但是<not [)

我今天正处于自我学习的极限。 我希望这对于比我更有经验的人来说是一个明显的解决方案。

我希望你们都过得愉快。

您可以尝试使用“邮件程序”,而不是尝试直接使用SMTP。 梅勒可以在这里找到。

这是一些简单的代码,展示了它的工作原理。

messages=[]
message = mailer.Message()
message.attach('filename.txt')
message.From = 'Cool guy <cool.guy@example.com>'
message.To = 'Random Dude <random.dude@example.com>'
message.Cc = 'Cards Fan <cardsfan@example.com>'
message.Subject = 'Test Email'
message.body = 'Here is the body of the email.'
messages.append(message)

emailer = mailer.Mailer(smtphost.example.com)
emailer.send(messages)

我从本地的一些例子中拼凑了这些。 上面链接的邮件页面也显示了其他示例。 一旦我找到这个代码,我转换了所有其他python电子邮件代码来使用这个包。

我知道回答我自己的问题是不好的形式,但它开始奇迹般地工作,没有任何变化。 有什么方法可以给我留下第一印象,对吧?

无论如何,我把它包装成R函数。 这将从gmail发送,但我还没有尝试从其他帐户发送它。 我最感兴趣的是从Outlook发送,因为我将使用它从我的脚本中发送分析报告。 当我进入雇主的SMTP服务器时,它给出了错误“服务器不支持SMTP AUTH扩展”。 我怀疑我必须和我的技术支持人员一起解决这个问题。

由于winDialog()函数,这可能只适用于Windows。 但这是一个良好的开端。

send.email <- function(to, from, subject, 
  message, attachment=NULL,
  username, password,
  server="smtp.gmail.com:587",
  confirmBeforeSend=TRUE){
  # to: a list object of length 1.  Using list("Recipient" = "recip@somewhere.net") will send the message to the address but 
  #     the name will appear instead of the address.
  # from: a list object of length 1.  Same behavior as 'to'
  # subject: Character(1) giving the subject line.
  # message: Character(1) giving the body of the message
  # attachment: Character(1) giving the location of the attachment
  # username: character(1) giving the username.  If missing and you are using Windows, R will prompt you for the username.
  # password: character(1) giving the password.  If missing and you are using Windows, R will prompt you for the password.
  # server: character(1) giving the smtp server.
  # confirmBeforeSend: Logical.  If True, a dialog box appears seeking confirmation before sending the e-mail.  This is to 
  #                    prevent me to send multiple updates to a collaborator while I am working interactively.  

  if (!is.list(to) | !is.list(from)) stop("'to' and 'from' must be lists")
  if (length(from) > 1) stop("'from' must have length 1")
  if (length(to) > 1) stop("'send.email' currently only supports one recipient e-mail address")
  if (length(attachment) > 1) stop("'send.email' can currently send only one attachment")
  if (length(message) > 1){ 
    stop("'message' must be of length 1")
    message <- paste(message, collapse="\\n\\n")
  }

  if (is.null(names(to))) names(to) <- to
  if (is.null(names(from))) names(from) <- from
  if (!is.null(attachment)) if (!file.exists(attachment)) stop(paste("'", attachment, "' does not exist!", sep=""))

  if (missing(username)) username <- winDialogString("Please enter your e-mail username", "")
  if (missing(password)) password <- winDialogString("Please enter your e-mail password", "")

  require(rJython)
  rJython <- rJython()

  rJython$exec("import smtplib")
  rJython$exec("import os")
  rJython$exec("from email.MIMEMultipart import MIMEMultipart")
  rJython$exec("from email.MIMEBase import MIMEBase")
  rJython$exec("from email.MIMEText import MIMEText")
  rJython$exec("from email.Utils import COMMASPACE, formatdate")
  rJython$exec("from email import Encoders")
  rJython$exec("import email.utils")

  mail<-c(
  #Email settings
  paste("fromaddr = '", from, "'", sep=""),
  paste("toaddrs  = '", to, "'", sep=""),
  "msg = MIMEMultipart()",
  paste("msg.attach(MIMEText('", message, "'))", sep=""),
  paste("msg['From'] = email.utils.formataddr(('", names(from), "', fromaddr))", sep=""),
  paste("msg['To'] = email.utils.formataddr(('", names(to), "', toaddrs))", sep=""), 
  paste("msg['Subject'] = '", subject, "'", sep=""))

  if (!is.null(attachment)){
    mail <- c(mail,
      paste("f = '", attachment, "'", sep=""),
     "part=MIMEBase('application', 'octet-stream')",
     "part.set_payload(open(f, 'rb').read())",
     "Encoders.encode_base64(part)",
     "part.add_header('Content-Disposition', 'attachment; filename=\"%s\"' % os.path.basename(f))",
     "msg.attach(part)")
  }

#SMTP server credentials
  mail <- c(mail, 
    paste("username = '", username, "'", sep=""),
    paste("password = '", password, "'", sep=""),

#Set SMTP server and send email, e.g., google mail SMTP server
    paste("server = smtplib.SMTP('", server, "')", sep=""),
    "server.ehlo()",
    "server.starttls()",
    "server.ehlo()",
    "server.login(username,password)",
    "server.sendmail(fromaddr, toaddrs, msg.as_string())",
    "server.quit()")

  message.details <- 
    paste("To:               ", names(to), " (", unlist(to), ")", "\n",
          "From:             ", names(from), " (", unlist(from), ")", "\n",
          "Using server:     ", server, "\n",
          "Subject:          ", subject, "\n",
          "With Attachments: ", attachment, "\n",
          "And the message:\n", message, "\n", sep="")

  if (confirmBeforeSend) 
   SEND <- winDialog("yesnocancel", paste("Are you sure you want to send this e-mail to ", unlist(to), "?", sep=""))
   else SEND <- "YES"

  if (SEND %in% "YES"){ 
    jython.exec(rJython,mail)
    cat(message.details)
  }
  else cat("E-mail Delivery was Canceled by the User")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM