I have written a ruby cli script which takes a CSV and generates a PDF report, based on said CSV. I'm fairly new to Ruby, so while it's probably not the greatest code, I'm pretty proud of what I've made.
At any rate, what I would really like to do now, is make my script email said PDF as an attachment. I'm sure there is a library that understands SMTP and can send this on my behalf, but I would like to modify the email body, and review the attachments before sending. So it seems like the simplest thing would be to have the script start a new email in my system default mail client, providing the recipient, subject, and boiler plate text, and attaching the generated file, kind of like a mailto: link in a web page (does mailto support attachments?).
Seems like there could be a system command that does this, completely unrelated to Ruby, which I could have my Ruby script call. That would be fine. If it's platform dependent, I'm on OSX, but I move around, so am interested in Windows and Linux solutions, too.
I guess plan B would be a way to jam a simple CLI editor into my Ruby script, to let me edit the email text, and then use an SMTP library to send the email. That seems harder, unless it's already been done.
Actually you can execute any ruby file from command-line interface (CLI) using console_runner gem. If you already have written code you can run it from command line. All you need is to add annotations (YARD-like syntax) to your Ruby code and then execute it from command line:
$ c_run /path/your_file.rb say_hello
/path/your_file.rb:
# #runnable
class MyClass
# #runnable
def say_hello
puts 'Hello!'
end
end
Related
I'm writing an application where I have to send an email with an attachment using the default mail application.
Before the email is sent, I want the user to be able to edit the text, i.e. the application should just open the mail client with pre-filled recipient and attachment and give the user the opportunity to send it.
At very minimum I need the same effect I'd got if I selected "SendTo/Mail Recipient" from the context menu of the file.
Solutions based on the "mailto:" trick won't work as there are mail clients that do not support the "attachment=" part.
The most complete solution I've found is this one:
http://www.codeproject.com/Articles/3839/SendTo-mail-recipient
but it seems a lot of code for something so simple! (and also crashes when compiled with VS2008)
Is there any other option? It would be ok even if it was an external tool or script (e.g. a .vbs script to be launched with cscript).
I would advise you to use MAPI (Messaging Application Program Interface).
If dotNet can be part of the solution, here's a ready-to-use class in C# : Class for creating MAPI Mail Messages. It would give you something like this:
MapiMailMessage message = new MapiMailMessage("Test Message", "Test Body");
message.Recipients.Add("Test#Test.com");
message.Files.Add(#"C:\del.txt");
message.ShowDialog();
Otherwise, you can always do it in C++ if you feel confortable with it, like this answer suggest.
Then, you will be able to ShellExecute the binary executable and pass it some parameters.
Hope this helps :-)
I am writing an automated test suit for a program that has mailing lists. I am trying to decide on the best practice for structuring the tools that I am going to use. The tests need to send email to a variety of email addresses then use the application to perform an action (approve, reject, discard). Then the script finally needs to check its mail and compare the email it has received against the list of emails it expects to receive. Here is the list of tools I am using.
Ruby,
Rake,
Selenium Webdriver,
Test-unit,
Jenkins
What I wanted to do was to treat everything as a dependency (in rake) of the last step(checking the email). My problem came when tried to make every email unique. I plan to embed the time the test was run at and a number assigned to each email in the test into the email (this number will be the same for each run of the test so I can identify where it should go). I need a way to pass the time stamp from the beginning of the test to the end of the test.
The solutions I see to my problems are to get rid of rake (because I can't or don't know how to pass a variable between tasks) or to write to a file then access the file in the seperate tasks.
Any recommendations?
I would advise setting an ENV variable in your Rakefile before each test is run, like this:
ENV['TIMESTAMP_CONTROL'] = Time.now.to_s
You can then reference the variable anywhere in your scripts and Rakefile until you reset it again like any other Ruby variable:
assert_equal ENV['TIMESTAMP_CONTROL'], #email_response_text
I'm trying to write some specs for my application aimed to process and resend incoming emails. It's built with Mailman App. I didn't find any good examples on how to do that. What am I actually trying is to create email (with Mail gem) and process it with Mailman. But there is only option of using stding for testing.
So, are there any examples of specs written to test the mailman apps?
I think it is useful to extract as much as possible out of your Mailman::Application block and put it into your app somewhere. Then test that processing.
This Railscast (pro-only, sorry) has an example of "An Alternative to Routing" that is pretty good.
Basically instead of doing something like:
Mailman::Application.run do
subject(/Update (\d+)/) do |ticket_id|
...
you just say:
Mailman::Application.run do
default do
MailProcessor.receive_mail(message)
end
end
Then the MailProcessor class would handle reading the message and calling the right other functions in your app (depending on the message's subject, recipient, sender, etc.) I would then RSpec test the MailProcessor class for doing the right thing.
This is similar to how I approach testing rake tasks (put the functionality in a lib file that you test, and have the rake task just call the lib file.) Then you can be fairly certain that the functionality is being tested properly. If you were paranoid about the rake task, you can try an approach like: http://robots.thoughtbot.com/post/11957424161/test-rake-tasks-like-a-boss.
I am trying to send email using a Perl script from my Mac, for which I have installed
MIME::Lite module. I am using a basic script to test:
#!/usr/bin/perl
use MIME::Lite;
$msg = MIME::Lite->new(
From =>"abc\#gmail.com",
To =>"xyz\#gmail.com",
Subject =>"Demo",
Data =>"Sent :-):-)"
);
$msg->send();
I have already set up my email account in my macbook.
Please guide me if I need something else to check for as i am unable to send the email.
Gone are the days when you could just use a system call out to the command line:
mail boss#megacorp.net -s "I QUIT!" < body_of_message.txt
But if you install and configure mutt to talk to your mail server, you can do something pretty close:
mutt -s "I QUIT" boss#megacorp.net < body_of_message.txt
The hardest bit is configuring mutt, and that's not too bad. There are a ton of docs and howtos out there, like Mutt Configuration Doc ...or just google for "mutt configure" and the type of mail server that you're using; gmail, exchange, etc.
From there, in perl, you would just:
system("/path/to/mutt", "-s", "I QUIT", "boss\#megacorp.net", ...)
or die "Could not send Email";
I haven't used this module but I note the docs for it say
MIME::Lite is not recommended by its current maintainer. There are a
number of alternatives, like Email::MIME or MIME::Entity and
Email::Sender, which you should probably use instead. MIME::Lite
continues to accrue weird bug reports, and it is not receiving a large
amount of refactoring due to the availability of better alternatives.
Please consider using something else.
http://metacpan.org/pod/MIME::Lite
Having said that, you may need to do something like
Specify default send method:
MIME::Lite->send('smtp','some.host',Debug=>0);
MIME::Lite->send('smtp','some.host', AuthUser=>$user, AuthPass=>$pass);
I am creating a chat client/server system in Ruby.
My server will be hosted on a laptop or something (this is a class project, so not much processing power will be needed) and then I plan for the client to take place in a web browser.
I will feed it the HTML for two textboxes: one in which the user can type and the other will display the chat history.
My problem is that while I can easily feed the HTML code to the browser and get it to display the chat (navigate to the ip address:port) I can't figure out how I can return what is input in the textbox to the server.
Does anybody know how I could do this?
I'd suggest using a lightweight framework like Sinatra to handle this. It's simple enough to get things done quickly without a lot of required reading, but powerful enough to expand your chat application significantly, should you want.
The downside of using a web-based client is that the chat log will only be refreshed on the client after they ask the server for the newest information; namely, at each page refresh, instead of in real time.
You can get around this with some slick Javascript (mostly XMLHTTPRequest) to ask for new content at a regular interval, like how Stack Overflow shows you when new answers have been posted as you're typing an answer of your own.
It sounds like you need a basic knowledge of how CGIs work. Once you know that you will find it easier to work with Sinatra, as #echoback recommended, or Padrino, Rails, or work with other languages.
This is a pretty basic CGI. It generates a simple form, along the lines of what you were talking about, then walks through the environment table passed to Ruby by the web server, sorts by the keys, and outputs a table in sorted order. Most of the fields directly apply to either the web server itself, or to the CGI, such as the query sent by the browser, along with its headers sent to the server saying what its capabilities are:
#!/usr/bin/env ruby
puts "Content-Type: text/html"
puts
puts "<html><head><style type='text/css'>body{font-family: monospace;}</style></head><body>"
puts "<form name='foo' action='test_cgi.rb'>"
puts "<input type='textinput' name='inputbox'></input><br />"
puts "<textarea name='textareabox'></textarea><br />"
puts "<input type='submit'></input>"
puts "</form>"
puts "<h4>ENVIRONMENT:</h4>"
puts "<table>"
ENV.keys.sort.each do |k|
puts "<tr><td>#{k}</td><td>#{ENV[k]}</td></tr>"
end
puts "</table>"
puts "</body></html>"
Copy that code, store it into a Ruby file called test_cgi.rb, then set the executable bit on the file. Move that file into the cgi-bin directory of your web server on your machine. Use your browser to access the file (http://localhost:8080/cgi-bin/test_cgi.rb or something similar), and watch the output in the table change as you enter different values in the form and submit them.
Once you understand that round-trip from server to browser to server you'll be in a good place to learn how Sinatra builds on Rack to supply more features, more easily, than doing it all yourself with a CGI.