Monday 20 June 2011

Some handly Rails snippets

To get url along with query string

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

To extract 'year' or 'month' or 'day' from a unix timestamp
strtotime to convert date to unix timestamp as date() accept unix timestamp not just any simple string.
date('Y',strtotime('2011-12-2') );

To get Month name:
      Date::MONTHNAMES[Date.today.month]
or

I18n.t("date.month_names") # [nil, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
I18n.t("date.abbr_month_names") # [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
I18n.t("date.month_names")[Date.today.month] # "December"
I18n.t("date.abbr_month_names")[Date.today.month] # "Dec"

Thursday 16 June 2011

How to extract Body and Subject From piped mail

To extract Subject and Mail from piped mail

Raw Email format:

Received: from gijs ([192.168.55.2])
 by debian with smtp (Exim 3.12 #1 (Debian))
 id 17AYXZ-0002BE-00
 for ; Wed, 22 May 2002 18:00:41 +0200
From: "Gijs van Tulder" 
To: 
Subject: Test
Date: Wed, 22 May 2002 18:00:41 +0200
Message-ID: <KNEOKJCOCHEDPIMPAIMIOEOJCJAA.gijs@debian>
MIME-Version: 1.0
Content-Type: text/plain;
 charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0)
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

This is my message.

Thank you.

Saturday 11 June 2011

email forwarding: pipe to a program

Ever tried to read your mail outside of your mail client?
Then here's your chance to try to read or play with your incoming mails.

go to your cpanel , look for email forwarding option.
Add a new email forwarder and from the drop down list choose "pipe to a program".
In the text box enter a pipe sign `|` followed by a path to your script file.
eg.
|/home/mysite/public_html/test.php


Set it permission to 0755 and put a hash bang as the very first line of your code


In your test.php (or another) structure your code as this:


#!usr/local/bin/php -q


   --- your php code goes here---
//To read mail use this : 
$fd = fopen("php://stdin", "r"); 



#end