How to save a backup of mails sent through MIME::Lite in Perl 5? - mime

I am sending mail through MIME::Lite perl module.I want that my sent mails should be saved in Sent items folder of my mail inbox.Please suggest me a way to do this.
Thanks
script-
use MIME::Lite;
my $msg = MIME::Lite->new(
From => 'xyz#abc.com',
To => 'abcd#gmail.com',
Subject => 'sending mail',
Type => 'multipart/mixed',
);
$msg->attach(
Type => 'TEXT',
Path => '/home/user/myName/cppLab/a.txt',
Filename => 'aabc.txt',
Disposition => 'attachment',
);
$msg->attach(
Type => 'TEXT',
Data => "learning to send mail",
);
#$msg->print(\*STDOUT);
#$msg->print_header(\*STDOUT);
$msg->send;
print "mail sent.."

Depends on how you access your inbox. In any case, you'll probably need MIME::Lite's as_string method that you can call on messages to retrieve the entire text including headers and possibly attachments.
For local mailboxes:
If your mail folders use the Maildir format, you could use Maildir::Lite to simply write the returned string to an appropriately named (Maildir::Lite does that for you) file.
For mbox folders, have a look at Mail::Box::Mbox
Local mailboxes can be handled in just a few lines of Perl. If your inbox is remote and needs IMAP, there's always IMAP::Client but I don't really have a clue about it. Perhaps a local Maildir plus offlineimap would do, too?

Related

Oracle APEX print with API

I need to export a report saving it in PDF format. I created a button that submits to a new page, which performs the following piece of code:
APEX_UTIL.DOWNLOAD_PRINT_DOCUMENT (
p_file_name => 'myreport123',
p_content_disposition => 'attachment',
p_application_id => :APP_ID,
p_report_query_name => 'MY_REPORT_QUERY',
p_report_layout_name => 'MY_REPORT_LAYOUT',
p_report_layout_type => 'xsl-fo',
p_document_format => 'pdf'
)
;
MY_REPORT_QUERY and MY_REPORT_LAYOUT are defined in the shared components. When I press the button, it downloads a pdf file, but it looks empty or corrupted.
What could be wrong? Do I need to specify the print server? And what about the field APP_ID?
I can confirm that both the query and layout work when I download the report by directly redirecting the page to the url of MY_REPORT_QUERY.
Thanks in advance and best regards.

Zip multiple files and download using php codeigniter

I am trying to create a zip file including 2 text files and download it. Here is my code. $fda and $fwl are 2 array datas.
$dataZip = array(
'./downloads/fda.in' => $data1,
'./downloads/fwl.in' => $data2
);
$this->zip->add_data($dataZip);
$this->zip->archive('./downloads/files_backup.zip');
$this->zip->download('files_backup.zip');
But it always returns error as a wrongly formatted string and nothing is downloaded.
fda.in�X�n�0}�+�
Could anybody tell me where I was wrong?
I found out the solution. All I have to do is add ob_start(); in the open of the controller file.
ob_start();
$dataZip = array(
'./downloads/fda.in' => $fda,
'./downloads/fwl.in' => $fwl
);
$this->zip->add_data($dataZip);
$this->zip->archive('./downloads/files_backup.zip');
$this->zip->download('files_backup.zip');

Ruby hipchat gem invalid send file

So this is related to an earlier post I made on this method. This is essentially what I am using to send files via hipchat:
#!/usr/bin/env ruby
require 'hipchat'
client = HipChat::Client.new('HIPCHAT_TOKEN', :api_version => 'v2', :server_url => 'HIPCHAT_URL')
client.user('some_username').send_file('message', File.open('./output/some-file.csv') )
client['some_hipchat_room'].send_file('some_user', 'message', File.open('./output/some-file.csv') )
Now for some reason the send_file method is invalid:
/path/to/gems/hipchat-1.5.4/lib/hipchat/errors.rb:40:in `response_code_to_exception_for': You requested an invalid method. path:https://hipchat.illum.io/v2/user/myuser#myemail/share/file?auth_token=asdfgibberishasdf method:Net::HTTP::Get (HipChat::MethodNotAllowed)
from /path/to/gems/gems/hipchat-1.5.4/lib/hipchat/user.rb:50:in `send_file'
I think this indicating that you should be using POST instead of GET, but I'm not sure because I haven't used this library nor Hipchat.
Looking at the question you referenced and the source posted by another user they're sending the request using self.class.post, and your debug output shows Net::HTTP::Get
To debug, could you try,
file = Tempfile.new('foo').tap do |f|
f.write("the content")
f.rewind
end
user = client.user(some_username)
user.send_file('some bytes', file)
The issue is that I was attempting to connect to the server via http instead of https. If the following client is causing issues:
client = HipChat::Client.new('HIPCHAT_TOKEN', :api_version => 'v2', :server_url => 'my.company.com')
Then try adding https:// to the beginning of your company's name.
client = HipChat::Client.new('HIPCHAT_TOKEN', :api_version => 'v2', :server_url => 'https://my.company.com')

Standalone Attachments in CouchDB + Ruby

Has anybody succeeded in sending standalone attachments using the standalone attachment API (if possible gziped) to couchDB from ruby?
I know there are a few CURL examples, but my tries with Typhoeus haven't been successful so far. It usually just stops and waits for > 1 minute after the first few documents.
CouchRest doesn't seem to support it, neither do any of the other libraries I've looked at
edit: clarification
I'm NOT looking for regular Base64 encoded attachments. CouchRest does that pretty fine.
Got it working with typhoeus
Typhoeus::Request.put("http://127.0.0.1:5984/db/document/my_attachment_name?rev=#{rev}", "content-type" => "text/html", "Content-Encoding" => "gzip", "Accept-Encoding" => "gzip", :body => my_html_body)
this will store the "my_html_body" string into couchDB as a gziped standalone attachment
For a binary standalone attachment, I just used IO.read("/path/to/my/file") to give a string to the put method as the :body. It looks like it is working but I don't know if it is the right way to do it.
It looks like this:
res = Typhoeus::Request.get("http://localhost:5984/_uuids")
uuid = JSON.parse(res.body)["uuids"].first
doc = {}
doc["name"] = name
...
res = Typhoeus::Request.put("http://localhost:5984/products/#{uuid}", :body => JSON.generate(doc))
res = Typhoeus::Request.put("http://localhost:5984/products/#{uuid}/image.jpg?rev=#{rev}", :headers => {"Content-Type" => "image/jpeg" }, :body => IO.read("output/images/#{image}"))

Multipart File Upload in Ruby

I simply want to upload an image to a server with POST. As simple as this task sounds, there seems to be no simple solution in Ruby.
In my application I am using WWW::Mechanize for most things so I wanted to use it for this too, and had a source like this:
f = File.new(filename, File::RDWR)
reply = agent.post(
'http://rest-test.heroku.com',
{
:pict => f,
:function => 'picture2',
:username => #username,
:password => #password,
:pict_to => 0,
:pict_type => 0
}
)
f.close
This results in a totally garbage-ready file on the server that looks scrambled all over:
alt text http://imagehub.org/f/1tk8/garbage.png
My next step was to downgrade WWW::Mechanize to version 0.8.5. This worked until I tried to run it, which failed with an error like "Module not found in hpricot_scan.so". Using the Dependency Walker tool I could find out that hpricot_scan.so needed msvcrt-ruby18.dll. Yet after I put that .dll into my Ruby/bin-folder it gave me an empty error box from where on I couldn't debug very much further. So the problem here is that Mechanize 0.8.5 has a dependency on Hpricot instead of Nokogiri (which works flawlessly).
The next idea was to use a different gem, so I tried using Net::HTTP. After short research I could find out that there is no native support for multipart forms in Net::HTTP and instead you have to build a class that encodes etc. for you. The most helpful I could find was the Multipart-class by Stanislav Vitvitskiy. This class looked good so far, but it does not do what I need, because I don't want to post only files, I also want to post normal data, and that is not possible with his class.
My last attempt was to use RestClient. This looked promising, as there have been examples on how to upload files. Yet I can't get it to post the form as multipart.
f = File.new(filename, File::RDWR)
reply = RestClient.post(
'http://rest-test.heroku.com',
:pict => f,
:function => 'picture2',
:username => #username,
:password => #password,
:pict_to => 0,
:pict_type => 0
)
f.close
I am using http://rest-test.heroku.com which sends back the request to debug if it is sent correctly, and I always get this back:
POST http://rest-test.heroku.com/ with a 101 byte payload,
content type application/x-www-form-urlencoded
{
"pict" => "#<File:0x30d30c4>",
"username" => "s1kx",
"pict_to" => "0",
"function" => "picture2",
"pict_type" => "0",
"password" => "password"
}
This clearly shows that it does not use multipart/form-data as content-type but the standard application/x-www-form-urlencoded, although it definitely sees that pict is a file.
How can I upload a file in Ruby to a multipart form without implementing the whole encoding and data aligning myself?
Long problem, short answer: I was missing the binary mode for reading the image under Windows.
f = File.new(filename, File::RDWR)
had to be
f = File.new(filename, "rb")
Another method is to use Bash and Curl. I used this method when I wanted to test multiple file uploads.
bash_command = 'curl -v -F "file=#texas.png,texas_reversed.png"
http://localhost:9292/fog_upload/upload'
command_result = `#{bash_command}` # the backticks are important <br/>
puts command_result

Resources