diff --git a/.Rbuildignore b/.Rbuildignore old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/DESCRIPTION b/DESCRIPTION old mode 100644 new mode 100755 diff --git a/NAMESPACE b/NAMESPACE old mode 100644 new mode 100755 index d92cc0c..339f319 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,5 @@ +# Generated by roxygen2 (4.0.1): do not edit by hand + export(cell2email) export(gmail) export(scrape.cell) @@ -6,3 +8,4 @@ import(XML) import(rJava) import(rJython) import(rjson) +import(txtutils) diff --git a/R/cell2email.R b/R/cell2email.R old mode 100644 new mode 100755 diff --git a/R/email.helper.R b/R/email.helper.R old mode 100644 new mode 100755 index 64970fc..6050f36 --- a/R/email.helper.R +++ b/R/email.helper.R @@ -1,76 +1,82 @@ email.helper <- -function(to, from, subject, username, password, server, message, attachment, - confirmBeforeSend){ +function(to, from, subject, username, password, server, message, attachment, + confirmBeforeSend){ +# if (!is.null(attachment)) { +# if (attachment == FALSE) attachment <- NULL +# } + 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 (attachment == FALSE) attachment <- NULL + for(att in attachment) { + if (!file.exists(att)) stop(paste("'", att, "' does not exist!", sep="")) + } } - 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", "") - 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") + if (missing(username)) username <- winDialogString("Please enter your e-mail username", "") + if (missing(password)) password <- winDialogString("Please enter your e-mail password", "") + 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, "','plain','utf-8'))", 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)") - } + mail<-c( + #Email settings + paste("fromaddr = '", from, "'", sep=""), + paste("toaddrs = '", to, "'", sep=""), + "msg = MIMEMultipart()", + paste("msg.attach(MIMEText('", message, "','plain','utf-8'))", 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)){ + for(att in attachment) { + mail <- c(mail, + paste("f = '", att, "'", 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)") + } + } - mail <- c(mail, - paste("username = '", username, "'", sep=""), - paste("password = '", password, "'", sep=""), - 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()") + mail <- c(mail, + paste("username = '", username, "'", sep=""), + paste("password = '", password, "'", sep=""), + 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="") + 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") + 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") } diff --git a/R/gmail.R b/R/gmail.R old mode 100644 new mode 100755 index a6a3c1f..8947cd4 --- a/R/gmail.R +++ b/R/gmail.R @@ -16,60 +16,98 @@ #' @references \url{http://r.789695.n4.nabble.com/Email-out-of-R-code-td3530671.html} #' @export #' @import rJython rJava rjson +#' @import txtutils #' @examples #' \dontrun{ -#' gmail(to=c("bob@@gmail.com", "janr@@hotmail.com"), password = "password", +#' gmail(to=c("bob@@gmail.com", "janr@@hotmail.com"), password = "password", #' attachment="path/to/file.pdf") #' gmail(to=cell2email(5555555555, "sprint"), password = "password") #' } gmail <- -function(to, password, subject="R message", message="EOM", from=NULL, - attachment=NULL, server="smtp.gmail.com:587", username=NULL, - confirmBeforeSend=FALSE, clear.username = FALSE){ - data(UNAME) - cells <- sapply(strsplit(to, "@"), function(x) x[2]) %in% cell.ext[, 2] - loc <- paste0(find.package("gmailR"), "/data") - if ((is.null(username) & is.na(UNAME) & !exists(".UNAME", - envir = .GlobalEnv)) | clear.username) { - cat("\n","Enter gmail Username","\n") - UNAME <- scan(n=1,what = character(0), quiet=T) - .UNAME <<- UNAME - unlink(paste0(loc, "/UNAME.rda"), recursive = TRUE, - force = FALSE) - save(UNAME, file = paste0(loc, "/UNAME.rda")) - } - if (exists(".UNAME", envir = .GlobalEnv)) { - if (is.null(username)) { - username <- .UNAME - } else { - username <- UNAME - } - } - if (is.null(username)) { - username <- UNAME + function(to, password, subject="R message", message="EOM", from=NULL, + attachment=NULL, server="smtp.gmail.com:587", username=NULL, + confirmBeforeSend=FALSE, clear.username = FALSE){ + cells <- sapply(strsplit(to, "@"), function(x) x[2]) %in% cell.ext[, 2] + loc <- paste0(find.package("gmailR"), "/data") + if (is.null(from)) { + from <- username + } + + # if (!is.null(attachment) && + # (length(strsplit(unlist(attachment), "\\", fixed=TRUE))) == 1 && + # length(strsplit(unlist(attachment), "/", fixed=TRUE)) == 1)) { + # attachment <- paste0(getwd(), "/", attachment) + # } + + + if (is.vector(attachment)) { + attachment = list(attachment) + } + + if(!is.null(attachment)) { + attachment = lapply(attachment, addPwd) + } + + + nRcpt = length(to) + nAttach = length(attachment) + if(nAttach == 1 && nRcpt > 1) { + attachment = rep(attachment, nRcpt) + } + nAttach = length(attachment) + if(nAttach != nRcpt) { + stop("Number of attachments not equal to number of recepients!") + } + + atts = attachment + atts[cells] <- FALSE + lapply(seq_along(to), + function (i){ + email.helper(to=list(to[i]), + from = list(from), + subject = subject, + message = message, + attachment = atts[[i]], + username = username, + password = password, + server = server, + confirmBeforeSend = confirmBeforeSend) + } + ) } - if (is.null(from)) { - from <- username - } - if (!is.null(attachment) && - (length(unlist(strsplit(attachment, "\\", fixed=TRUE))) == 1 & - length(unlist(strsplit(attachment, "/", fixed=TRUE))) == 1)) { - attachment <- paste0(getwd(), "/", attachment) + + +addPwd = function(files = character()) { + if(is.list(files)) files = unlist(files) + idx = grepl("/", files) + idx = which(!idx) + if(length(idx) > 0) { + message("Some files are relative to PWD, need to prepend PWD..") + wd = getwd() + for(i in idx) { + files[i] = file.path(wd, files[i]) + } } - atts <- rep(attachment, length(to)) - atts <- lapply(atts, c) - atts[cells] <- FALSE - lapply(seq_along(to), - function (i){ - email.helper(to=list(to[i]), - from = list(from), - subject = subject, - message = message, - attachment = atts[[i]], - username = username, - password = password, - server = server, - confirmBeforeSend = confirmBeforeSend) + + idx = grepl("[*~]", files) + idx = which(idx) + if(length(idx) > 0) { + newFiles = list() + message("It looks like you used globbing, I will expand them..") + for(i in 1:length(files)) { + if(i %in% idx) { + newItem = Sys.glob(files[i]) + newFiles[[length(newFiles) + 1]] = newItem + } + else { + newFiles[[length(newFiles) + 1]] = files[i] + } } - ) -} \ No newline at end of file + files= unlist(newFiles) + } + files +} + +# require(txtutils) +# files = c("/tmp/*.txt", "~/Downloads/PhD-dag 2014_programme_PhD Day_def.pdf", "/Users/kaiyin/.autoGmailrc", "tmp.txt") +# addPwd(files) diff --git a/R/scrape.cell.R b/R/scrape.cell.R old mode 100644 new mode 100755 diff --git a/R/zzz.R b/R/zzz.R old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index cec0776..799758a --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ #gmailR -![gmailicon](https://dl.dropbox.com/u/61803503/gmail.png) +![gmailicon](https://dl.dropbox.com/u/61803503/gmail.png) A way to send gmail message from R with attachments. @@ -14,10 +14,10 @@ You can, however, download the [zip ball](https://github.com/trinker/gmailR/zipb # install.packages("devtools") library(devtools) -install_github("gmailR", "trinker") +install_github("gmailR", "kindlychung") ``` Note: Windows users need [Rtools](http://www.murdoch-sutherland.com/Rtools/) and [devtools](http://CRAN.R-project.org/package=devtools) to install this way. -## Help -For the package pdf help manual [click here](https://dl.dropbox.com/u/61803503/gmailR.pdf). \ No newline at end of file +## Help +For the package pdf help manual [click here](https://dl.dropbox.com/u/61803503/gmailR.pdf). diff --git a/data/UNAME.rda b/data/UNAME.rda old mode 100644 new mode 100755 diff --git a/data/cell.ext.rda b/data/cell.ext.rda old mode 100644 new mode 100755 diff --git a/inst/CITATION b/inst/CITATION old mode 100644 new mode 100755 diff --git a/inst/maintenance.R b/inst/maintenance.R old mode 100644 new mode 100755 diff --git a/man/cell2email.Rd b/man/cell2email.Rd old mode 100644 new mode 100755 index ea0a39f..634a629 --- a/man/cell2email.Rd +++ b/man/cell2email.Rd @@ -1,3 +1,4 @@ +% Generated by roxygen2 (4.0.1): do not edit by hand \name{cell2email} \alias{cell2email} \title{Generate an Email from a Cell Phone Number} @@ -5,14 +6,14 @@ cell2email(cell.number, carrier = NULL, omit.old = TRUE) } \arguments{ - \item{cell.number}{The 10 digits number of the cell - you're trying to send a text to.} +\item{cell.number}{The 10 digits number of the cell you're trying to send a +text to.} - \item{carrier}{The name of the carrier. If NULL - interactive selection of a carrier is used.} +\item{carrier}{The name of the carrier. If NULL interactive selection of a +carrier is used.} - \item{omit.old}{logical. If TRUE removes all the old - Canadian carriers from the list.} +\item{omit.old}{logical. If TRUE removes all the old Canadian carriers from +the list.} } \description{ Generates an email adress based on cell number nad carrier. diff --git a/man/gmail.Rd b/man/gmail.Rd old mode 100644 new mode 100755 index d48128c..6f24501 --- a/man/gmail.Rd +++ b/man/gmail.Rd @@ -1,3 +1,4 @@ +% Generated by roxygen2 (4.0.1): do not edit by hand \name{gmail} \alias{gmail} \title{Email With Attachments} @@ -7,35 +8,28 @@ gmail(to, password, subject = "R message", message = "EOM", from = NULL, confirmBeforeSend = FALSE, clear.username = FALSE) } \arguments{ - \item{to}{The recipient's email address.} +\item{to}{The recipient's email address.} - \item{password}{Yor password.} +\item{password}{Yor password.} - \item{subject}{The message (default is \code{R - message}).} +\item{subject}{The message (default is \code{R message}).} - \item{message}{The subject line (default is \code{EOM}).} +\item{message}{The subject line (default is \code{EOM}).} - \item{from}{Your email address (gmailR will try to store - this information for you).} +\item{from}{Your email address (gmailR will try to store this information for you).} - \item{attachment}{Path to an attachment you wish to - include.} +\item{attachment}{Path to an attachment you wish to include.} - \item{server}{email server.} +\item{server}{email server.} - \item{username}{Your email address (gmailR will try to - store this information for you).} +\item{username}{Your email address (gmailR will try to store this information for you).} - \item{confirmBeforeSend}{logical. If TRUE gmail will - confirm before sending.} +\item{confirmBeforeSend}{logical. If TRUE gmail will confirm before sending.} - \item{clear.username}{logical. If TRUE clears the stored - username.} +\item{clear.username}{logical. If TRUE clears the stored username.} } \description{ -Send emails (including gmails) with attachments from within -R. +Send emails (including gmails) with attachments from within R. } \examples{ \dontrun{ diff --git a/man/scrape.cell.Rd b/man/scrape.cell.Rd old mode 100644 new mode 100755 index 044f6d3..ce2e293 --- a/man/scrape.cell.Rd +++ b/man/scrape.cell.Rd @@ -1,3 +1,4 @@ +% Generated by roxygen2 (4.0.1): do not edit by hand \name{scrape.cell} \alias{scrape.cell} \title{Scrape Cell Phone Extensions} @@ -5,12 +6,10 @@ scrape.cell(URL = "http://www.emailtextmessages.com/") } \arguments{ - \item{URL}{The URL of the cell phone extension website to - scrape from.} +\item{URL}{The URL of the cell phone extension website to scrape from.} } \description{ -Grabs cell phone extensions from -http://www.emailtextmessages.com/ +Grabs cell phone extensions from http://www.emailtextmessages.com/ } \examples{ \dontrun{