Sendmail Alias in PHP-scripts

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

Post Reply
jeex
Forum Newbie
Posts: 3
Joined: Thu Mar 09, 2006 3:42 am

Sendmail Alias in PHP-scripts

Post by jeex »

Sendmail Alias in PHP-scripts
Hello,

I want to make a PHP-script that makes, edits and deletes mail-aliasses on the fly.

My server is Linux with Ensim (using Sendmail). Within Ensim I can of course do these things, but I want a regular PHP-script to manage aliasses.

I have my own Linux + Apache server with root access, but want to solve the problem within my regular PHP scripts.

Thanks

Jeex
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Sendmail Alias in PHP-scripts

Post by Chris Corbyn »

jeex wrote:Sendmail Alias in PHP-scripts
Hello,

I want to make a PHP-script that makes, edits and deletes mail-aliasses on the fly.

My server is Linux with Ensim (using Sendmail). Within Ensim I can of course do these things, but I want a regular PHP-script to manage aliasses.

I have my own Linux + Apache server with root access, but want to solve the problem within my regular PHP scripts.

Thanks

Jeex
Are you referring to the files under /etc/aliases ?

You'll need to set your permissions on the file to be writable by the web user and that could be dangerous but in any case these are very basic colon separated files so it should be easy with a combination of file(), fopen() and fwrite().

Something like:

Code: Select all

function addAlias($alias, $user)
{
    `echo "$alias : $user" >> /etc/aliases`; //This is easy peasy
}

function delAlias($alias)
{
    $tmp = array();
    $lines = file('/etc/aliases');
    foreach ($lines as $l)
    {
        if (!preg_match('/^'.$alias.' :/', $l)) $tmp[] = $l;
    }
    $data = implode('', $tmp);
    $handle = fopen('/etc/aliases', 'w+');
    fwrite($handle, $data);
    fclose($handle);
}
Obviously, to edit an alias you just delete it and then re-add it.
jeex
Forum Newbie
Posts: 3
Joined: Thu Mar 09, 2006 3:42 am

sendmail

Post by jeex »

Thanks a lot,

I was thinking of changing the location defined in sendmail.cf to a location in on of the accessable webdirectories.

If possible with more domains on the same server.

gr

Jeex
jeex
Forum Newbie
Posts: 3
Joined: Thu Mar 09, 2006 3:42 am

Not possible

Post by jeex »

If the rights were World-writable, it still wouldn't work because user /root must execute newaliases for every change made.

Maybe virtual hosting is the answer

gr

Jeex
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Not possible

Post by Chris Corbyn »

jeex wrote:If the rights were World-writable, it still wouldn't work because user /root must execute newaliases for every change made.

Maybe virtual hosting is the answer

gr

Jeex
Maybe exim is the answer ;) You can get it to read from any config files you like... even ones you just made up yourself since it's config files essentially use a programming language of their own. I love it :)

I use exim to serve multiple domains from one server, all with aliases. I have the files secured however but there's no reasn you couldn't make them web writable. No need to reload the server for things like domain additions or alias additions ;)
Post Reply