Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6750193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:46:46+00:00 2026-05-26T12:46:46+00:00

Please forgive me up front. When I’ve tried to research this question I end

  • 0

Please forgive me up front. When I’ve tried to research this question I end up looking at code that I simply can’t comprehend. I have about 3 hours of experience with Python and am probably attempting more than I can handle.

The problem is simple. I can successfully call Python from R (my analysis software) to send an e-mail. Adding the message, subject, to, and from fields I can do. I’d like to be able to send an attachment. Life would be great if I could send just one attachment.

The code I have thus far is

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()

When I run this code, I get the message
string payload expected: [type ‘list’] (but with < not [)

I’m at my limit for self-learning today. I’m hoping this is an obvious fix to someone more experienced than myself.

I hope you’re all having a great day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:46:46+00:00Added an answer on May 26, 2026 at 12:46 pm

    I know it’s bad form to answer my own question, but it started working miraculously with no changes. What a way to make my first impression, right?

    Anyway, I wrapped it into an R function. This will send from gmail, but I haven’t tried sending it from other accounts yet. I’m most interested in sending from Outlook, since I’d be using this to send analysis reports from within my scripts. When I entered my employer’s SMTP server, it gave the error “SMTP AUTH extension not supported by server.” I suspect I’ll have to work this out with my tech support guys.

    This will probably only work on Windows, thanks to the winDialog() functions. But it’s a good start.

    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")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please forgive my long question. I have an idea for a design that I
Veterans please forgive me for asking silly question. I understand that a class having
Please forgive if this question has been asked numerous times. I recently installed Eclipse
please forgive me if this is a noob question, but i'm a beginner at
Please forgive the clumsy question (if you can figure out a better way to
Please forgive the awkwardness of this question, I honestly don't know how to phrase
Please forgive me if this question has been asked and answered already. If so,
Please forgive the newbie question. I have an iphone game that currently uses lots
Please forgive the noob question. I've been developing this little app (with Unity) for
this code crashes with EXC_BAD_ACCESS (please forgive me the formatting, I seem to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.