Posts Tagged ‘email’

Using the PHP Mail Function with Additional Headers

Posted 07 Feb 2010 — by admin
Category php

Pass a fourth parameter to the mail() function with the header information.

<?php
$to = "jane@example.com";
$subject = "Hello World!";
$body = "This will be sent from email-addr@example.com";
$headers = "From: email-addr@example.com\r\nX-Mailer: php";
mail($to, $subject, $body, $headers);

Send Mail in Ruby with a Pony

Posted 19 Aug 2009 — by admin
Category Programming

Great little gem that let’s you quickly and easily send out mail from your ruby scripts.

gem install pony

require 'rubygems'
require 'pony'

Pony.mail :from=>"me@example.com", :to=>"you@example.com", :subject=>"hello", :body=>"world"

Using sendmail to send mail on ubuntu box

Posted 07 Aug 2009 — by admin
Category Linux

I normally install postfix for my MTA. However, I’ve never really used sendmail so I’d decide to give it a whirl for a new application I’m working on. I don’t use it for anything but handling the mail that the application needs to send out, like new user welcome emails, password resets, etc.

apt-get install sendmail

Sendmail, unlike postfix, won’t work out of the box. Postfix will prompt you for the necessary config setup when running the install. Sendmail won’t, and therefore it’s not ‘out of the box’. You’ll have to make some modifications on your own. Nothing major but this is what I’ve found in order to get it to work, reliably and quickly. The first thing I did was add the fully qualified domain name to my /etc/hosts file

#vim /etc/hosts
127.0.0.1 www.mydomain.com

After this I added the fully qualified domain name to my apache default configuration file

#/etc/apache2/sites-available/default
ServerName www.mydomain.com
#vhost info etc...

Reload and restart…

/etc/init.d/apache2 force-reload
/etc/init.d/sendmail restart

You can test sendmail like so

sendmail email@example.com
hello
from
me
. 

This should deliver a message to you (the “.” on a new line, followed by a new line, closes the message).

Email Obfuscation and Extraction from Text with Rails

Posted 10 Jul 2009 — by admin
Category Ruby on Rails

There is a helper method for handling the obfuscation of email addresses in Rails.

mail_to "me@domain.com", "My email", :encode => "hex"
 # => My email

If you want to then extract an email address(or all email addresses) from a block of text here is the code. I created a helper function called “emailitize” and put it in the ApplicationHelper module inside helpers/application_helper.rb

module ApplicationHelper
  #takes a string and will return the same string but with email addresses encoded and hyperlinked
  def emailitize(text)
    text.gsub(/([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})/i) {|m|
        mail_to(m, m.gsub("@", "[at]"), :encode=>:hex)
    }
  end
end

It’s important to remember that you’ll need to pass a block to the gsub method. You can’t do something like this instead

text.gsub( /([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})/i, mail_to('\\1@\\2', '\\1@\\2', :encode=>:hex) )

It will work except the encode will fail. It will evaluate the ‘\\1@\\2′ strings rather than as dynamic variables.

You can then use this function in your views

<%= emailitize @job.how_to_apply %>

More information is available in the Rails and Ruby docs:

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001887

http://ruby-doc.org/core/classes/String.html#M000817

Postfix, ActionMailer and OpenSSL Fix on Ubuntu

Posted 23 Jun 2009 — by admin
Category Ruby on Rails

If you run into problems using ActionMailer > 2.2, Postfix and OpenSSL while sending mail from your application, try changing the following:

  vim /etc/postfix/main.cf

Change

 smtpd_use_tls=yes

to

smtpd_use_tls=no

OpenSSL support with Postfix does not work out of the box. You can either generate valid certificates or tell Postfix not to use the certificates. More information is available in this discussion forum.

http://forum.slicehost.com/comments.php?DiscussionID=2656

Sending eMail with Rails on Mac OS X Development Environment

Posted 10 May 2009 — by admin
Category Programming

You’ll need a mail transport agent (MTA). I installed and used postfix using Mac Ports.

sudo port install postfix

You’ll need to start postfix, to send mail from your Rails application. You can set it as a startup item and it will start on boot. However, since I don’t send too much mail from my Rails app, just for testing normally, I start it manually.

sudo postfix start

That should do it!