tags:

views:

623

answers:

4

If I write a PHP script to connect to an SMTP server (in my case, this is Postfix installed locally) and send an email, the first "received" header in the e-mail turns out as:

Received: from [SERVER_IP] (SERVER_HOSTNAME [127.0.0.1]) by SERVER_HOSTNAME (Postfix) with ESMTP id D682A34381 for <TO_EMAIL>; Thu,  5 Mar 2009 17:25:18 +0000 (GMT)

This is because the PHP script is being accessed through the browser and Apache is bound to the SERVER_IP.

However, if I execute the same script from the PHP CLI, then the first "received" header is instead:

Received: from localhost.localdomain (SERVER_HOSTNAME [127.0.0.1]) by SERVER_HOSTNAME (Postfix) with ESMTP id AB51934381 for <TO_EMAIL>; Thu,  5 Mar 2009 17:18:01 +0000 (GMT)

This is because the PHP CLI is being called by a regular Linux user (through a cron).

How can I cause the PHP CLI to be bound to the hostname/IP so that when it is called by the user through the cron job, the "received" header shows the server hostname/IP instead of localhost.localdomain?

Note 1: the hostname is correctly set in hostname --fqnd, /etc/hosts, /etc/sysconfig/network and /proc/sys/kernel/hostname

Note 2: I'm using Swift Mailer in PHP, although this is probably irrelevant.

A: 

I believe it will work better if you don't connect to the server by the localhost address, and instead connect by to its assigned real ip address or the fully qualified domain name.

Zoredache
This didn't make any difference.
DavidM
A: 

Can you paste the code used to send the mail (connection, etc).

Whats the value of $_SERVER["SERVER_ADDR"] in PHP?

Jona
A: 

Consider seriously using Swift Mailer's "NativeMail" or even the "Sendmail" interface.

Both of these options will avoid the network traffic, the internal IP address, and the Received header in-question. They will also be significantly faster, which may be important to you.

If you insist on using SMTP, note that Swift "autodetects" the local host name (that is supplied in the SMTP dialog) using the $_SERVER["SERVER_ADDR"] variable, which won't be set by the CLI.

If you cannot arrange for this to have a better value, you can pass it to the Swift constructor as the second argument, as in:

$swift = new Swift(new Swift_Connection_SMTP("localhost"), "my_domain_here");
geocar
I needed to set the second Swift constructor parameter because, as you say, SERVER_ADDR isn't set in the CLI.
DavidM
A: 

Here is some php code I used to get the value of the hostname into my crons:

//get hostname info from /etc/sysconfig/network
preg_match('/HOSTNAME=(.*)/', file_get_contents('/etc/sysconfig/network'), $network);
$hostname = split("\=", $network[0]);
echo $hostname[1]; //this equals the value of your HOSTNAME
jjriv