<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Moose56 blog &#187; PHP</title>
	<atom:link href="http://www.moose56.com/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.moose56.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 13 Jan 2012 07:24:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Sending an email with PHP and Ruby</title>
		<link>http://www.moose56.com/blog/2008/01/23/sending-an-email-with-php-and-ruby/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sending-an-email-with-php-and-ruby</link>
		<comments>http://www.moose56.com/blog/2008/01/23/sending-an-email-with-php-and-ruby/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 12:26:48 +0000</pubDate>
		<dc:creator>moose56</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://moose56.com/blog/2008/01/23/sending-an-email-with-php-and-ruby/</guid>
		<description><![CDATA[The other day I was trying to code a back end to a simple web form. I wanted the data entered into the form to be emailed back to me. Here I describe two very simple methods to send an email from both PHP and Ruby code. So why am I posting this? Well it [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was trying to code a back end to a simple web form. I wanted the data entered into the form to be emailed back to me. Here I describe two very simple methods to send an email from both PHP and Ruby code.</p>
<p><span id="more-13"></span></p>
<p>So why am I posting this? Well it took a bit of fishing to find some of the information so I thought this might help someone in the future. Also if you google anything to do with Ruby and web development you can&#8217;t get away from heaps Rails stuff which is not what I was after so this post might help.</p>
<h2>PHP</h2>
<p>PHP was built for this sort of thing so I was expecting it to be pretty easy and I was not disappointed. The actual function to send a mail is called&#8230;yep mail. The php documentation lists it as:</p>
<pre class="viewsource"><code class="php">bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )</code></pre>
<p>With this its simple to code up a working version:</p>
<pre class="viewsource"><code class="php">
// pick up values from form
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

// create email message
$from = $email;
$to = "some@address.com";
$subject = "Some subject";
$message = wordwrap(stripcslashes($comment), 70);
$headers = "From: $name &lt;$from&gt;" . "rn";

// send email
mail($to, $subject, $message, $headers);
</code></pre>
<p>Thats it, simply point the action attribute of your form to this script file and it will pick up the form data and send it off in an email to who ever you want.</p>
<p>Obviously this is a pretty basic example and you would need to add some form validation and a redirect when the script has completed otherwise the user will be directed to a totally blank page.</p>
<p>One other cool thing is that because the mail funciton returns a bool to can stick it in an if statement:</p>
<pre class="viewsource"><code class="php">
if (!mail($to, $subject, $message, $headers)){
  // redirect to fatal error page
  // or stick an entry in a log file
  // exit the script
}

//everything fine and dandy, continue
</code></pre>
<h2>Ruby</h2>
<p>Ok so I tried it in Ruby because it is my current language du jour and I did not want to see it outdone by PHP. Sadly however I have to admit it was not as simple as the PHP method.  Here is the listing:</p>
<pre class="viewsource"><code class="ruby">
require "net/smtp"

# set up smtp object
server = Net::SMTP.new('you.domain.com', 25,
  'localhost.localdomain', 'username', 'password', :login)

# message template
myMessage = &lt;&lt;END_OF_MESSAGE
From: Name &lt;some.address@mail.com&gt;
To: Name &lt;some@address.com&gt;
Subject: Some subject

This is the message text
END_OF_MESSAGE

# send the email
server.start do |smtp|
  smtp.sendmail myMessage, "from@mail.com",
    "to@mail.com", "to@mail.com"
end
</code></pre>
<p>There are a few more things to set up in the Ruby version, namely an SMTP object. This is because to send the email you need to provide a SMTP server. You do this by providing the domain, port number and if needed the username and password plus the authentication type of your domains SMTP server.</p>
<p>The sendmail method then takes the message, from address, and a list of addresses to send the email to.</p>
<p>One thing to note is that both sendmail and start can kick out a load of different errors so it would be better to wrap it in a begin rescue statement.</p>
<p>A few resources for the ruby version are:</p>
<ul>
<li><a href="http://ruby.about.com/od/tutorials/ss/ruby_email.htm">A fuller tutorial than this</a></li>
<li><a href="http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html">Ruby doc for NET::SMTP</a></li>
<li><a href="http://spariamsgems.blogspot.com/2007/12/sending-e-mail-with-ruby-netsmtp.html">Another good example of the Ruby method</a></li>
</ul>
<p>Ok I admit this was a very brief intro into sending email from code using Ruby or PHP, but I hope It might point some people in the right direction.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moose56.com/blog/2008/01/23/sending-an-email-with-php-and-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

