Page 1 of 2
Mailer Code
Posted: Tue Apr 03, 2007 2:50 pm
by Benjamin
Anyone see anything wrong with this? It's just a simple mailer, sometimes it works perfectly for months and sometimes it just won't send mail at all...
Code: Select all
function mailer($f, $t, $s, $b) // from to subject body
{
$headers = null;
if (preg_match("#<.*?>#s", $b)) //autodetect html
{
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
}
$headers .= "From: $f\n";
$headers .= "Reply-To: $f\n";
mail($t, $s, $b, $headers);
}
// call example..
mailer("Firstname, Lastname <email@address.com>", somebody@someplace.com, 'Message', 'Body');
Posted: Tue Apr 03, 2007 2:55 pm
by Oren
Hey astions! Good to see you around again...
Any good reason for not using Swift Mailer?
Posted: Tue Apr 03, 2007 3:05 pm
by Benjamin
Yeah it's just a small script which get's distrubuted so I don't want to add a bunch of code to it for mailing. Swiftmail would be overkill for the needs.
Posted: Tue Apr 03, 2007 3:46 pm
by feyd
Looks like a lovely opportunity for someone to inject headers into.
Posted: Tue Apr 03, 2007 3:49 pm
by Benjamin
Feyd, anything from a user has already been validated yo

Posted: Tue Apr 03, 2007 3:55 pm
by feyd
mailer() isn't protecting itself however. Stuff that may get past your other filters...
Posted: Tue Apr 03, 2007 3:59 pm
by Benjamin
In the context that this function is used, it would be impossible for header injection unless someone cracked into the MySQL server and modified a varchar field.
Do you see anything in it which would cause emails not to be sent though? I know it's receiving valid data.
Posted: Tue Apr 03, 2007 4:02 pm
by feyd
Potentially the \n character needing to be \r\n.
Since I'm not as well versed in the eccentricities of mail as some, I can't really say. Once d11 comes back around, he may have some comments however. Sit tight.
Posted: Tue Apr 03, 2007 4:12 pm
by Benjamin
Hmm,
That could be it, as per the manual..
Multiple extra headers should be separated with a CRLF (\r\n).
Not: When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing.
Not: If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with RFC 2822.
I'll go ahead and change them but I'm not sure that would cause it to work intermitently on a server when nothing has been changed.
Posted: Tue Apr 03, 2007 4:15 pm
by feyd
It may not be a direct problem on you server, but on a relay down the line. This can be especially true if the route often taken changes for some reason. Some email servers are more strict than others.
Posted: Tue Apr 03, 2007 4:18 pm
by Benjamin
Ok, that makes sense, just so we are all on the same page, here is a note from a client..
now I called host they said no mail is going out from the script it has no returnpath so it just goes into thin air... he said the script is not sending any mail its in QUE and he need an error message to find out whats wrong
Posted: Tue Apr 03, 2007 4:23 pm
by Chris Corbyn
Firstname, Lastname <address>
This isn't valid since the comma has a special meaning. Usually you can leave out the "quotes" but not if you have a comma there. You need it to be:
"Firstname, Lastname" <address>
If you're really sending such a short email then that can cause you a problem. Also, if you want to send anything outside US-ASCII ranges then you almost certainly need to use Swift (or an alternative if you don't like Swift). 8-bit sequences (the £ sign is one that commonly catches people out) are a definite no-no.
Using Swift or otherwise, have a read over this, check the blacklist database linked to in there and check you have a SPF record.
http://www.swiftmailer.org/wikidocs/v3/tips/spam
Posted: Tue Apr 03, 2007 4:30 pm
by Benjamin
Ok, thanks for the tips...
I added quotes around the name. In this case there are spaces in it but no commas, but in some cases there will be commas.
It is US ascii text, I have no problem using swift but not in this case as it's just a small standalone script.
I'll change the line feeds to \r\n
It's not a blacklist issue, as per the customer comment above, it would appear the mails are not even leaving the server.
Hopefully that will fix it?
Posted: Tue Apr 03, 2007 4:35 pm
by Oren
That's why you should use Swift - it has been tested many times by different people on different servers with different configurations.
Posted: Tue Apr 03, 2007 11:26 pm
by Benjamin
Oren, in this situation using swift would be like using a freight train to pull a pack of cigarettes down an icy hill.
Anyway, I just tested the following on two servers, one worked, one didn't. I am going to assume that this is a server related issue of some sort.
Code: Select all
<?php
function mailer($f, $t, $s, $b) // from to subject body
{
$headers = null;
if (preg_match("#<.*?>#s", $b))
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
}
$headers .= "From: $f\r\n";
$headers .= "Reply-To: $f\r\n";
mail($t, $s, $b, $headers);
}
mailer('"Testy, Tester" <xxxxxxx@xxxxxx.com>', 'xxxxxxxx@xxxxxxx.com', "Mail Test", "This is a test message. This is a test message.");
echo 'sent message';
EDIT: Added "down an icy hill"
