Skip to content

Instantly share code, notes, and snippets.

@jswatson0
Last active December 19, 2015 04:18
Show Gist options
  • Save jswatson0/5895967 to your computer and use it in GitHub Desktop.
Save jswatson0/5895967 to your computer and use it in GitHub Desktop.
email a formatted .csv file Been working on this and am not getting anywhere. Any help would be great. Thanks
Name Description Due
A task The desscrption of the task dd/mm/yy
More tasks More descriptions of tasks dd/mm/yy
#get input from user: task_name, task_desc, task_due, task_status
#write to .csv with headers: Name, Description, Due, Status
#email list of to do tasks two sections.. Due Today and Past due
#EXTENSION
#allow the user to update status
#strip completed from file
#need more explanation on protecting gmail account information name/password
#read the file and send an email to user
require 'csv'
require 'gmail'
# @gmail_username = ENV['GMAIL_USERNAME']
# @gmail_password = ENV['GMAIL_PASSWORD']
# Opens .csv file and writes headings
def create_file
if File.exist?('to_do_genie.csv') == false
CSV.open('to_do_genie.csv', 'ab') do |csv|
csv << ["Name", "Description", "Due"]
end
end
def get_task
puts "Enter a task name."
@task_name = gets.chomp
puts "Enter a description of task."
@task_desc = gets.chomp
puts "Enter a due date in the format dd/mm/yy"
@task_due = gets.chomp
until @task_due.length == 8 && @task_due.include?('/')
puts "Please enter date in dd/mm/yy format"
@task_due = gets.chomp
end
puts "task is saved"
CSV.open('to_do_genie.csv','ab') do |csv|
csv << [@task_name, @task_desc, @task_due]
puts "Do you want to add another task? (y or n)"
answer = gets.chomp
if answer == 'y'
get_task
else
puts "Goodbye"
end
end
end
end
# Trying to figure out how to inject .csv file into html_email
def see_csv
html_email = ""
CSV.read('to_do_genie.csv').each_with_index do |row|
# puts row.class
html_email << "<tr><td>#{row[0]}</td></tr><tr><td>#{row[1]}</td></tr><tr><td>#{row[2]}</td></tr>\n"
puts html_email
end
end
# send function...
def send_email
html_email = ""
CSV.read('to_do_genie.csv').each_with_index do |row|
# puts row.class
html_email << "<tr><td>#{row[0]}</td></tr><tr><td>#{row[1]}</td></tr><tr><td>#{row[2]}</td></tr>\n"
# puts html_email
end
Gmail.new('username', 'password') do |gmail|
gmail.deliver do
to "jswatson0@gmail.com"
from "jswatson0@gmail.com"
subject "ToDo test"
# Attempt to put .csv into HTML table
html_part do
body '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="100%" cellpadding="3" cellspacing="3">
<tr style="text-align:center">
<td>Task</td>
<td>Description</td>
<td>Due Date</td>
</tr>
<tr>
<td>Here is a task</td>
<td>This is an example of a longish description to see how it fits in a cell</td>
<td>Table Cell</td>
</tr>
<tr>
<td>Table Cell</td>
<td>Table Cell</td>
<td>Table Cell</td>
</tr>
</table>'
end
end
end
end
# send_email
# If you pass a block, the session will be passed into the block,
# and the session will be logged out after the block is executed.
# gmail = Gmail.new(username, password)
# # ...do things...
# gmail.logout
# Gmail.new(username, password) do |gmail|
# # ...do things...
# end
# gmail.deliver do
# to "email@example.com"
# subject "Having fun in Puerto Rico!"
# text_part do
# body "Text of plaintext message."
# end
# html_part do
# body "<p>Text of <em>html</em> message.</p>"
# end
# add_file "/path/to/some_image.jpg"
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment