Tuesday, March 24, 2009

sending email from php on leopard server 10.5.6

The default installation of leopard server comes with postfix and a sendmail wrapper for postfix that you can use to send email from php. You have to jump through a few hoops to get it to work though.

First you must make sure that you have apache and php installed correctly. you can verify this by creating a phpinfo.php file with contents and then viewing it at your servers ip address (or domain name) in your client computers web browser. If the phpinfo.php renders correctly then you can move on to the next steps: (taken from: http://jspr.tndy.me/2008/05/php-mail-and-osx-leopard/)


There are 4 files I used for the following:

  • /etc/hostconfig
  • /etc/postfix/main.cf
  • php.ini (this could be anywhere depending on your installation, mine’s in /usr/local/php5/lib/)
  • /var/log/mail.log

firstly, sudo nano -w /etc/hostconfig and add the following line:

MAILSERVER=-YES-

then sudo nano -w /etc/postfix/main.cf, find the myhostname variable (by default it’s host.domain.tld), uncomment it and change it to your domain (if you’re on a machine that doesn’t have a DNS, you can make it a domain that you’re responsible for so that it doesn’t get shut down at the receiving end, but please don’t make it google.com or something like that!)

now, open php.ini and look for the sendmail_path variable, uncomment it, make its value sendmail -t -i, save then restart apache. I’m not really sure if this is 100% necessary as there’s a comment above that says this is the default value anyway, but it can’t hurt!

now open a terminal window and execute the next couple of commands:



My machine already had postfix running for some reason. That might have been because I had been playing with sendmail for the hour or so before I found the tutorial above. For that reason I had to "restart sendmail" in order to get it to read in the new main.cf configuration. So I did the following:

sudo postfix reload

however that made sendmail report the following error:
postfix/postfix-script: warning: not set-gid or not owner+group+world executable: /usr/sbin/postdrop

SO after a quick google search I found that this can be fixed by performing the following command:
chmod g+s /usr/sbin/postdrop

Then you can:
sudo postfix stop
sudo postfix start

and then for good measure restart apache:
sudo apachectl restart

Finally double check its all working by finishing the tutorial:


% sudo postfix start
% tail -f /var/log/mail.log

finally, create a file called mail.php (or whatever!) and add the following to it:

mail(
'you@yourdomain.com', // your email address
'Test', // email subject
'This is an email', // email body
"From: Me rn" // additional headers
);


?>
obviously replace you@yourdomain.com with your email address and me@mydomain.com with a valid email address (domain at least, as some mail servers will bounce your email if the sender’s domain isn’t real). Now navigate to your mail.php file (likely http://localhost/mail.php) and watch your terminal window to see that it’s been sent successfully.

0 Comments:

Post a Comment

<< Home