Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .Rbuildignore
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified DESCRIPTION
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions NAMESPACE
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2 (4.0.1): do not edit by hand

export(cell2email)
export(gmail)
export(scrape.cell)
Expand All @@ -6,3 +8,4 @@ import(XML)
import(rJava)
import(rJython)
import(rjson)
import(txtutils)
Empty file modified R/cell2email.R
100644 → 100755
Empty file.
142 changes: 74 additions & 68 deletions R/email.helper.R
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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")
}
134 changes: 86 additions & 48 deletions R/gmail.R
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
)
}
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)
Empty file modified R/scrape.cell.R
100644 → 100755
Empty file.
Empty file modified R/zzz.R
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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.


Expand All @@ -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).
## Help
For the package pdf help manual [click here](https://dl.dropbox.com/u/61803503/gmailR.pdf).
Empty file modified data/UNAME.rda
100644 → 100755
Empty file.
Empty file modified data/cell.ext.rda
100644 → 100755
Empty file.
Empty file modified inst/CITATION
100644 → 100755
Empty file.
Empty file modified inst/maintenance.R
100644 → 100755
Empty file.
13 changes: 7 additions & 6 deletions man/cell2email.Rd
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
% Generated by roxygen2 (4.0.1): do not edit by hand
\name{cell2email}
\alias{cell2email}
\title{Generate an Email from a Cell Phone Number}
\usage{
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.
Expand Down
Loading