Kategorien
Business

How to code your own email sender – or how to spam the world :)

This short blog will teach you how to code your own spam email-sender in R. Nevertheless, I do not recommend to use this code for spam emails!;)

Yes, I know there are several online tools that allow you to send multiple emails (e.g. mailchimp). However, coding is fun and this code is faster, respectively can easily be changed according to your needs.

So let’s get started: You will need to following packages. Notice that RDCOMClient must be installed directly from github. Use the correct path to the github directory and install the package [line 2]. The „gender“ package will help you to find the gender when your email database only includes first name, last name and email adress. A professional email should include the correct salutation. Check out the package yourself – I am only sharing a basic email-sender code.

library("devtools")
install_github('omegahat/RDCOMClient')
library(RDCOMClient)
require(gender)
require(huxtable)
require(dplyr)
require(xlsx)

Step 1: Create some sample data

final <- data.frame("first.name" = c("Luca","Patrick","James"), "last.name" = c("Liebi","Star","Bond"), "email.adress" = c("luca.liebi@helloworld.ch", "patrick.star@helloworld.ch","james.bond@helloworld.ch"), "gender"=c("male","male","male"), stringsAsFactors = FALSE)

Step 2: Code your email sender

# start a loop that sends each row (name) an email
for(i in 1:nrow(x)){

# this function simply catches errors in your loop
tryCatch({

# you need outlook :)
OutApp <- COMCreate("Outlook.Application")

# create an email 
outMail = OutApp$CreateItem(0) 

# make the solution correct
anrede <- NA 
if(final$gender[i]=="male"){ 
anrede <- paste("Dear Mr.",final$last.name[i])
}else{ 
anrede <- paste("Dear Mrs.",final$last.name[i])
} 

# here enter your text for your email:
body1 <- "Hello - it's me Luca and this is an awesome code :)" 

body1.1 <-"Paragraph 2: Insert some other information"

# past your email text together - notice that you can use HTML code to make # a new paragraph
body1 <- paste(body1,body1.1,sep="<br><br>")

# complete your email by including the solution (anrede)
text <- paste(anrede,body1,sep="<br><br>")

# configure important email parameter

    # who should receive the email?
    email.adress <- final$email.adress[i]
    outMail[["To"]] <- email.adress
    # Subject of the email
    outMail[["subject"]] = "I want to spam you"
    # from which email do you want to sent your email
    outMail[["sentonbehalfofname"]] = "luca.liebi@smartman.ch"
    
    outMail[["HTMLBody"]] = text
    
    outMail$Send()
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}

Now you are already done 🙂 – this code will send 3 email only. However, dependant on your database you can send much more emails!

Step 3: Some advanced tips

PDF attachements in your email can easily be done:

outMail[["attachments"]]$Add("Path\\to\\your\\pdf.pdf")

HTML formatting – simply use HTML in the body of your text:

body1 <- "Hello - it's me <b>Luca</b> and this is an awesome code :)" 

Inserting an image:

outMail[["attachments"]]$Add("Path\\to\\your\\PNG\\file\\logo.PNG")

# specify the size of the logo
logo <- paste0("<img src='cid:",
                basename("logo.PNG"),
               "' width = '340' height = '70'>")