1

I have a somewhat unique challenge: I have a CSV file that I need to extract text from and then send emails using that text. It's a one time issue and the emails need to be sent on the same day. Google has an option for something similar to this, but I need to send more than the daily quota of 100 emails. Using SMTP instead of the script uses a different quota, so I'll be able to send all of the emails I need to in one day if I go that route.

I've done some digging and the basic code to send the emails seems to be something like:

    $ echo "This will go into the body of the mail." | mail -s "Subject" [email protected]

or

    $ mail -s "Subject" [email protected] < /file/location/address.csv

Great so far, but I need it to extract the subject line from a CSV file and the text of the body, stopping when it hits the comma delimiter and then mark or remove the text in the source CSV file so it knows it already sent that email and doesn't repeat it. Finally, it needs to move onto the next row and repeat the process until it's gone through the entire CSV file.

Ideally, I should be able to execute it from terminal in OS X and watch the progress. (If possible, I'd like to include the 'sleep' command to introduce small pauses after it reaches each delimiter, to make the shell output intelligible instead of an endless stream of text.)

I have most of the basic snippets of code, but I'm not sure about how to extract the info from the CSV file and pipe it into what I already have. If anyone can point me in the right direction for that, I think I should be able to cobble it all together.

EDIT: If this is easier to do outside of bash, with another scripting language, I can probably cobble together everything but extracting the text and piping it into another command/output. Most languages have straightforward commands and libraries for that, so I should be able to Google and cannibalize the rest of the code. It's getting it from the CSV file into the emails that I have no idea how to do, despite repeated Google searches. (Also, if you think I was using the wrong search terms please feel free to suggest better ones.)

8
  • This is a Q&A site for programmers and this is clearly a job that could use some coding. But it's obviously not feasible to learn a programming language for a one time task. I think the question is not really answerable unless you provide a little background of what technologies you have available. (I've deduced from your code samples that bash is one of them.) Feb 20, 2016 at 12:49
  • Sorry Alvaro, I saw similar questions on the site and assumed this sort of question was acceptable.
    – Horus
    Feb 20, 2016 at 13:03
  • Well, there's nothing wrong with the question as long as we can provide an answer like "use library X" or "try command Y". (Nitpicker's corner: a spreadsheet is a particular piece of software that normally happens to be able to import/export data in CSV format but I don't think the tag applies to your question —you are not asking how to do something in LibreOffice Calc or write a Microsoft Excel macro). Feb 20, 2016 at 13:10
  • Thanks for clarifying about how to better phrase the question. I edited it to something more like that. While it may be nitpicking, that's really good to know about the spreadsheet/CSV distinction. In my mind, spreadsheets are a type of layout but it's useful to know that in the coding community it has a different meaning. It definitely makes it harder for people to help me when I use words incorrectly...
    – Horus
    Feb 20, 2016 at 13:13
  • @Horus please provide and example of the contents of the csv and desired output.
    – jkdba
    Feb 20, 2016 at 13:13

1 Answer 1

2

You could do something like this:

#!/bin/bash

#get line count
linecount=$(cat mycsv.csv  | wc -l)
#set counting variable
counter=1
#loop through file
while read line; do 
    #split each line into an array by comma
    IFS=, read -a arr <<< "$line"; 
    #set  array values to variables
    subject="${arr[0]}"; 
    intro="${arr[1]}"; 
    details="${arr[2]}"; 
    closing="${arr[3]}"; 
    #create body
    body=$(printf "Intro: %s \n Details: %s \n Closing: %s" "$intro" "$details" "$closing")
    #echo progress
    echo "Sending email $counter of $linecount: Subject: $subject, Body: $body"
    #send email
    echo "$body" | mail -s "$subject" [email protected] 
    #advance counter
    counter=$(( $counter +1 ))
    #sleep for a second
    sleep 1
done < mycsv.csv 

The above would output:

Sending email 1 of 2: Subject: subject, Body: Intro: intro 
 Details: details 
 Closing: closing

You could also format the body of the email this way. Note if you do this the second EOF that closes the email body must all the way to the left side of the file with no white space between the beginning of the line and the EOF.

    #send email
    mail -s "$subject" [email protected] <<EOF
    "$intro"
    "$details"
    "$closing"
EOF

Where mycsv.csv looks like:

subject,intro,details,closing
subject1,intro1,details1,closing1

I don't have access to OSX so this was tested with GNU bash, version 4.3.42:

bash --version
GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu)
2
  • I just tested it out and confirmed it works! I'll need to tweak it a little bit to get exactly what I want, but you did all the heavy lifting. Nicely, if this type of thing comes up again the script is adaptable enough that I should be able to make it work.
    – Horus
    Feb 20, 2016 at 15:45
  • @Horus, awesome glad to help
    – jkdba
    Feb 20, 2016 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.