Mailer Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Mailer Code

Post 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');
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Hey astions! Good to see you around again...

Any good reason for not using Swift Mailer?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Looks like a lovely opportunity for someone to inject headers into.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Feyd, anything from a user has already been validated yo :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mailer() isn't protecting itself however. Stuff that may get past your other filters...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

That's why you should use Swift - it has been tested many times by different people on different servers with different configurations.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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" :P
Post Reply