<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Looking for a secure PHP email script</title>
	<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script/</link>
	<description>Comments on Ask MetaFilter post Looking for a secure PHP email script</description>
	<pubDate>Wed, 16 Nov 2005 15:29:57 -0800</pubDate>
	<lastBuildDate>Wed, 16 Nov 2005 15:29:57 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Looking for a secure PHP email script</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script</link>	
		<description>I need a PHP-based email script that will take the name, email, subject, and message form fields I pass to it and send them to me with the name and email used as the &quot;From&quot; field in the message header. I also need to put a value in the header that identifies the sender&apos;s IP address. And, of course, I need a script that is not vulnerable to header injection spam attacks. Any recommendations?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2005:site.27356</guid>
		<pubDate>Wed, 16 Nov 2005 15:18:41 -0800</pubDate>
		<dc:creator>punishinglemur</dc:creator>
		
			<category>php</category>
		
			<category>email</category>
		
			<category>script</category>
		
	</item> <item>
		<title>By: thanotopsis</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#430665</link>	
		<description>PHP has a built-in mail() function that you can use and supply all sorts of customized headers.  &lt;a href=http://us2.php.net/function.mail&gt;Here&apos;s the manual page on it.&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
As to how secure your script is, it depends on how you accept input.  If you&apos;re accepting it from a FORM post, make sure that the referrer is from your own URL (so, no foreign POST actions).  If it&apos;s from a GET, do the same, but god help you -- it&apos;s the easiest thing to overpower or just get wrong.&lt;br&gt;
&lt;br&gt;
To get the sender&apos;s IP address, you have to depend on the Server variables from the web request, and then inject that into your custom headers.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-430665</guid>
		<pubDate>Wed, 16 Nov 2005 15:29:57 -0800</pubDate>
		<dc:creator>thanotopsis</dc:creator>
	</item><item>
		<title>By: phearlez</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#430670</link>	
		<description>Is the information in &lt;a href=&quot;http://securephp.damonkohler.com/index.php/Email_Injection&quot;&gt;this article&lt;/a&gt; not sufficient for you to roll your own simple one? That combined with &lt;a href=&quot;http://us2.php.net/manual/en/function.mail.php#56788&quot;&gt;this tip&lt;/a&gt; would likely be sufficient to head off injection issues, though if your mail reader does ROT13 you could always just ROT the subject area...</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-430670</guid>
		<pubDate>Wed, 16 Nov 2005 15:34:52 -0800</pubDate>
		<dc:creator>phearlez</dc:creator>
	</item><item>
		<title>By: meta87</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#430679</link>	
		<description>There are thousands of downloadle scripts online that do just that. Download some and take a look at how they handled it.&lt;br&gt;
&lt;br&gt;
Here is one I&apos;ve used in the past:&lt;br&gt;
&lt;a href=&quot;http://www.dtheatre.com/scripts/formmail.php&quot;&gt;&lt;br&gt;
http://www.dtheatre.com/scripts/formmail.php&lt;/a&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-430679</guid>
		<pubDate>Wed, 16 Nov 2005 15:44:55 -0800</pubDate>
		<dc:creator>meta87</dc:creator>
	</item><item>
		<title>By: delfuego</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#430685</link>	
		<description>Punishinglemur, I&apos;d recommend using PHP&apos;s built-in mail() function as well unless you have a lot of tricky stuff going on; in that case, I&apos;ve had good luck with &lt;a href=&quot;http://phpmailer.sourceforge.net/&quot;&gt;PHPMailer&lt;/a&gt; in a few of my projects.  (I&apos;m not too terribly pleased with the pace of development on PHPMailer, though; after finding a few bugs and having my reports of them ignored, I flat-out asked to just get sufficient access to the project and fix them myself, and even that was rebuffed.)  Either way, there won&apos;t be a way for a web client to inject headers into the email without it either being caused by you allowing their input to get fed into the email as a header or by being a bug in the underlying functions (mail() or PHPMailer&apos;s classes), so you&apos;re reasonably safe from that.&lt;br&gt;
&lt;br&gt;
Thanotopsis, checking referrers is equally insecure; a referrer is as much a user-supplied piece of information as anything else on a POSTable form.  (It&apos;s the client browser that provides the string... and there are scores of ways for a client to inject whatever it wants into that field of the HTTP request.)  Really, the only way to feel safe that your script isn&apos;t going to be used to send out mass spam is to &lt;strong&gt;never ever&lt;/strong&gt; let a user custom-define a recipient of the email without having to go through some level of authentication first.  So for some of my PHP mailing scripts, the recipient of the final email is predetermined (like my website contact form, which sends email to me and me alone); for other scripts, the user has to log into the larger website, and even then can only send mail to a predetermined dropdown of recipients.  (And that dropdown isn&apos;t foolish enough to contain email addresses which are fed into the script, but rather contains ID numbers that the script has to lookup in a database for the corresponding email addresses.)&lt;br&gt;
&lt;br&gt;
Punishinglemur, your premise is correct -- there&apos;s a lot of thought that needs to go into creating a script that does what you want without exposing itself as a tool for evildoers.  But it&apos;s entirely possible, and doesn&apos;t take a lot of actual effort in the end.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-430685</guid>
		<pubDate>Wed, 16 Nov 2005 15:50:35 -0800</pubDate>
		<dc:creator>delfuego</dc:creator>
	</item><item>
		<title>By: evariste</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#430733</link>	
		<description>I stole this from somewhere (probably php.net) and slightly modified it for my homebrewed blogware (I &lt;i&gt;think&lt;/i&gt; I modified it, but I no longer remember). It&apos;s pretty foolproof, whether they have a proxy or not:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;function GetIP()&lt;br&gt;
{&lt;br&gt;
   if (getenv(&quot;HTTP_CLIENT_IP&quot;) &amp;amp;&amp;amp; strcasecmp(getenv(&quot;HTTP_CLIENT_IP&quot;), &quot;unknown&quot;))&lt;br&gt;
           $ip = getenv(&quot;HTTP_CLIENT_IP&quot;);&lt;br&gt;
       else if (getenv(&quot;HTTP_X_FORWARDED_FOR&quot;) &amp;amp;&amp;amp; strcasecmp(getenv(&quot;HTTP_X_FORWARDED_FOR&quot;), &quot;unknown&quot;))&lt;br&gt;
           $ip = getenv(&quot;HTTP_X_FORWARDED_FOR&quot;);&lt;br&gt;
       else if (getenv(&quot;REMOTE_ADDR&quot;) &amp;amp;&amp;amp; strcasecmp(getenv(&quot;REMOTE_ADDR&quot;), &quot;unknown&quot;))&lt;br&gt;
           $ip = getenv(&quot;REMOTE_ADDR&quot;);&lt;br&gt;
       else if (isset($_SERVER[&apos;REMOTE_ADDR&apos;]) &amp;amp;&amp;amp; $_SERVER[&apos;REMOTE_ADDR&apos;] &amp;amp;&amp;amp; strcasecmp($_SERVER[&apos;REMOTE_ADDR&apos;], &quot;unknown&quot;))&lt;br&gt;
           $ip = $_SERVER[&apos;REMOTE_ADDR&apos;];&lt;br&gt;
       else&lt;br&gt;
           $ip = &quot;unknown&quot;;&lt;br&gt;
   return($ip);&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
and &lt;a href=&quot;http://www.gerd-riesselmann.net/archives/2005/09/sending-spam-through-contact-forms&quot;&gt;this guy&lt;/a&gt; has a nice function for sanitizing your email headers.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-430733</guid>
		<pubDate>Wed, 16 Nov 2005 16:31:31 -0800</pubDate>
		<dc:creator>evariste</dc:creator>
	</item><item>
		<title>By: fidelity</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#431432</link>	
		<description>This is basically what I use for my personal site&apos;s contact form,  quickly altered. It probably is still functional. It doesn&apos;t do exactly what you&apos;ve described, but should be fairly easy to alter. Let me know if you need clarification or whatever; email&apos;s in my profile.&lt;br&gt;
&lt;br&gt;
File 1, form.php:&lt;br&gt;
&amp;lt;form action=&quot;send.php&quot; method=&quot;post&quot;&amp;gt;&lt;br&gt;
&amp;lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;name&quot; / &amp;gt;&amp;lt;br /&amp;gt;&lt;br&gt;
&amp;lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;email&quot; /&amp;gt;&amp;lt;br /&amp;gt;&lt;br&gt;
&amp;lt;textarea name=&quot;message&quot;&amp;gt;&amp;lt;/textarea&amp;gt;&lt;br&gt;
&amp;lt;input type=&quot;submit&quot; value=&quot;Send it.&quot; /&amp;gt; &amp;lt;input type=&quot;reset&quot; value=&quot;Clear it.&quot; /&amp;gt;&lt;br&gt;
&amp;lt;/form&amp;gt;&lt;br&gt;
&lt;br&gt;
File 2, send.php:&lt;br&gt;
&amp;lt;?php&lt;br&gt;
function GetIP() {&lt;br&gt;
// use evariste&apos;s function &lt;a href=&quot;http://ask.metafilter.com/mefi/27356#430733&quot;&gt;above&lt;/a&gt;&lt;br&gt;
};&lt;br&gt;
$message = str_replace(&quot;\n.&quot;, &quot;\n..&quot;, $_POST[&apos;message&apos;]);&lt;br&gt;
        $name = $_POST[&apos;name&apos;];&lt;br&gt;
        $email = $_POST[&apos;email&apos;];&lt;br&gt;
$subject = $_POST[&apos;subject&apos;];&lt;br&gt;
        $ipaddr = GetIP(); &lt;br&gt;
        if (($email!=&quot;&quot;) AND ($email!=&quot;@&quot;)) $semail = $email;&lt;br&gt;
        else $semail = &quot;[[a default sender email address if user leaves it blank]]&quot;;&lt;br&gt;
        if ($message!=&quot;null&quot;) $mailsent = mail(&quot;[[your email address]]&quot;, $subject,&lt;br&gt;
&quot;$message\n\n$name\n$email\n$ipaddr&quot;, &quot;From: $name &lt; $semail&gt;\r\nReturn-Path:&lt;br&gt;
$semail&quot;);&lt;br&gt;
if ($mailsent) echo &quot;[[Some pretty text about how the message was sent &amp;amp; thanks.]]&quot;;&lt;br&gt;
?&amp;gt;&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-431432</guid>
		<pubDate>Thu, 17 Nov 2005 09:52:30 -0800</pubDate>
		<dc:creator>fidelity</dc:creator>
	</item><item>
		<title>By: weston</title>
		<link>http://ask.metafilter.com/27356/Looking-for-a-secure-PHP-email-script#441620</link>	
		<description>punishinglemur -- I have &lt;a href=&quot;http://weston.canncentral.org/misc/form2mail/index.html&quot;&gt;a  php form mail script&lt;/a&gt; that I&apos;ve been meaning to update against a few attacks and add the remote IP header feature to for a while, and coming upon your question seemed as good a reason as any to stop procrastinating so... I just did it. Feel free to download, and I&apos;d be happy to help you make it work if needed and take feedback.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.27356-441620</guid>
		<pubDate>Mon, 28 Nov 2005 20:37:15 -0800</pubDate>
		<dc:creator>weston</dc:creator>
	</item>
	</channel>
</rss>
