Creating an E-mail account trough PHP

Discussion in 'PHP' started by Eps, Jul 1, 2007.

  1. #1
    Right, the server I'm working with is Apache and it runs a Postfix mail server.

    1. Is it possible to create a PHP script that takes data from a form and creates an E-mail account automatically?

    2. If yes, then HOW?
     
    Eps, Jul 1, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    i think your best bet is to cURL into cpanel and do it that way. or you could use something like this. (fetched with a quick google search)

    
    <?php
    /*--------------------------*/
    /*    Coded by Dub/iNod     */
    /* CP Email Account Creator */
    /*      Version 1.2         */
    /*--------------------------*/
    
    // Email settings
    define("CPUSER", "username"); // CPanel Username
    define("CPPASS", "password"); // CPanel Password
    define("CPIP", "10.1.1.1"); // CPanel IP or Domain (without :2082)
    define("CPSKIN", "x"); //Commonly x2 or x (Go into cpanel and after the frontend part it should say x2, moonson, x,etc)
    define("SITEURL", "http://yourname.com"); // Site url (with http:// or www.) to CPEmail file.
    
    
    
    // Config Variables
    /* EDIT BELOW */
    define("QUOTA", "10"); // Quota is amount of space in megabytes. Default is 10
    define("EMAILS", "2"); // Number of email accounts allowed begin created
    // Ending is an array which holds email ending (after the @) so. Lets say your site is domain.com and you want to make emails for domain1.com you would enter domain1.com. If you are going to have more than 1 domain please use , on all values except last. Like I did below.
    $ending = array(
    
            "yourname.com" // Last value with no , (comma)
    );
    
    class CPEmail {
      // Set variables to class
      var $cpuser = CPUSER;
      var $cppass = CPPASS;
      var $cpip = CPIP;
      var $cpskin = CPSKIN;
      var $siteurl = SITEURL;
      var $quota = QUOTA;
      var $emails = EMAILS;
    
      function spam() {
        if($_SESSION['emails'] == $this->emails) {
          die('You created '. $this->emails .' account so far. No more!<br> Click <a href="'. $this->siteurl .'">here</a> to go back!');
        }
      }
    
      function create_email() {
          // Do email creation
          $file = fopen ("http://$this->cpuser:$this->cppass@$this->cpip:2082/frontend/$this->cpskin/mail/doaddpop.html?email=$_POST[NewEmail]&domain=$_POST[domain]&password=$_POST[Password]", "r");
          if (!$file) {
            $ok = FALSE;
            // Error creating account
            die('Error creating account.<br> Click <a href="'. $this->siteurl .'">here</a> to go back!');
          }
        // Get results
        while (!feof ($file)) {
          $line = fgets ($file, 1024);
          if (ereg ("already exists!", $line, $out)) {
            $ok = FALSE;
            // Account already exists
            die('Error creating account. May already exist, try another account.<br> Click <a href="'.$siteurl.'">here</a> to go back!');
          }
        }
        fclose($file);
        if ($ok) {
          $email = $_POST[NewEmail] . "@" . $_POST[domain];
          $_SESSION['emails'] = '1';
          $form_fields=array_keys($_POST);
          $temp="Welcome to our Free Email Service!\nThis is a test email to verify everything works.\nApparently it does!";
          while($field=array_pop($form_fields)){
            $temp.=" $field : = $_POST[$field] \n";
          }
          mail($email,"Free Email",$temp);
        }
    }
    }
    // Process form
    $do = new CPEmail;
    // Do spam processing
    $do->spam();
    // Do processing
    if($_POST[NewEmail] != "") {
      $ok = TRUE;
      $do->create_email();
           // Display the new email information
           print 'Sucessfully created your email account. Please print or save the following information.<br>
           Webmail Url: http://'.$_POST[domain].'/webmail<br>
           POP3 Server: mail.'.$_POST[domain].'.com<br>
           Username: '.$_POST[NewEmail].'<br>
           Password: '.$_POST[Password].'<br> Click <a href="'.$siteurl.'">here</a> to go back!';
      }else{
    ?>
    <br><br><center>
    <form method="post" action="index.php"><input type=hidden name=id value=cpemail>Email Address: <input type="hidden" name="NewEmail" value="<?php echo"$c_uuname";?>"><?php echo "$c_uuname";?>@<select name="domain">
    <?php
    foreach ($ending as $key) {
      print "<option>$key</option>";
    }
    ?>
    </select><br/>Password: <input type="password" name="Password"><br><input type="submit" name="submit" value="Create!"></form>
    <br><br><br><form method="get" action="http://your_ip_addr:2095/login/" target=_blank><br>Email Address: <input type="text" name="user">
    </select>
    <br/>Password: <input type="password" name="pass"><br><input type="submit" name="submit" value="Login!"></form></center>
    
    ?>
    
    PHP:
    or check out this cpanel api class http://www.phpclasses.org/browse/package/3534.html
     
    ansi, Jul 1, 2007 IP
    Weizheng likes this.
  3. jalte

    jalte Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What type of mail authentication does the server use? Is is using linux user account (/etc/passwd)? or Mysql/LDAP as the back-end?
     
    jalte, Jul 2, 2007 IP
  4. Eps

    Eps Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's using Linux accounts.
     
    Eps, Jul 3, 2007 IP
  5. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You have to figure out how the postfix server creates accounts and then make the PHP script do those steps.

    When I wrote a PHP front end for Mercury Mail I had all the accounts stored in a mysql database. When an account was created a row would be inserted, the directory with the password file would be created and a server restart command would be issued so the account would become active.

    You don't need a db backend if you can get access to the mail servers folders and check to see if accounts already exist.
     
    KalvinB, Jul 3, 2007 IP
  6. jalte

    jalte Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That would be quite tricky. So far I never find any PHP based administration interface for Postfix users that are based on linux account. You can use php exec function to run linux bash command in PHP for user creation but that would involve some troublesome permission handling (sudo, privileges etc). Thats why people opt for a Mysql or LDAP user storage back-end for postfix. It should be easier to be handled by PHP.
     
    jalte, Jul 3, 2007 IP
  7. fbnewtz

    fbnewtz Peon

    Messages:
    160
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I agree with jalte. I think that a MySQL db would be your best bet. Otherwise you will have to add the apache user to root and that would be a very bad mistake. Unless you use SUDO of course, but then you have to put a very important password in a script, that is not a good idea either.
     
    fbnewtz, Jul 4, 2007 IP
  8. Weizheng

    Weizheng Peon

    Messages:
    93
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This is what I've been looking for!

    Thanks.
     
    Weizheng, Jul 4, 2007 IP