The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

create email alias/forwarder with a php-script

Discussion in 'E-mail Discussions' started by thrunxje, Oct 22, 2004.

  1. thrunxje

    thrunxje Member

    Joined:
    Oct 22, 2004
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Hello,

    I'm trying to create a script that allows every member on my website to create a emailalias 'on the fly'. I have read on the internet that there exists an aliasfile that contains all the forwarders. I thought it was in 'etc/', but I can't find it anywhere :confused:
    Or does anyone knows another why to create that on the fly?

    Thanks,

    Steven vd Braak
     
  2. fwwebs

    fwwebs Well-Known Member

    Joined:
    Feb 16, 2004
    Messages:
    329
    Likes Received:
    0
    Trophy Points:
    16
    http://squirrelmail.org , has a plugin that allows the creation of an alias for email logins. You'd need to install it as a standalone.
     
  3. SarcNBit

    SarcNBit Well-Known Member

    Joined:
    Oct 14, 2003
    Messages:
    1,010
    Likes Received:
    3
    Trophy Points:
    38
    /etc/valiases/domain.tld
     
  4. thrunxje

    thrunxje Member

    Joined:
    Oct 22, 2004
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    I have 9.9.1-RELEASE 47 of Cpanel, and Neomail, Horde and Squirrel are installed. I know how to create an alias but not where the mailserver keeps his information. If I could find that file, I could write a script that adds it. When I go to my FTP I can't find any directory named 'valiases'. Maby I just can't see it with FTP.

    Does anyone know how what the direct acces is to that file from my www or public_html folder? Then I can use fopen to acces and edit that file.

    It's not a problem to just add an alias, but I want to give the members of my site direct acces, so they can create there own alias 'on the fly'. Just through a html form with a php script.

    Thanks already!

    Steven vd Braak
     
  5. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    As SarcNBit said, that file is at /etc/valiases/domain.tld.

    That's from the root of the server, not the root of your account. It's not directly accessible by users, but can be written to with a perl or php script. Just specify in the script that the file is at /etc/valiases/domain.tld.
     
  6. thrunxje

    thrunxje Member

    Joined:
    Oct 22, 2004
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Oh, now I get it, that's why I can't see it on my FTP :D
    I'll try it!
     
  7. thrunxje

    thrunxje Member

    Joined:
    Oct 22, 2004
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    It doesn't work :(

    This is my script:
    PHP:
    <?
        if(
    fopen("/etc/valiases/domain.tld","r")){
            echo 
    "I can read it!";    
        }
    ?>
    This is the location

    Does anyone know what I do wrong?
     
  8. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Have to change the "domain.tld" to the actual domain URL.
    domain.tld doesn't exist
     
  9. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    Here is a forwarder script I created

    Feel free to use this one:
    I use it on my site:

    Code:
    <?php
    	$username="username";	// change username to suite accoutn
    	$password="password";	// change password to suite account
    	$domain="domainname.com";  // change to domain to use
    	$Host = explode("/",$_SERVER['DOCUMENT_ROOT']);
    	include('../header.html'); // comment if you do not use a header
    	$ok = TRUE;
    ?>
    <!-- email.php | create POP email account | Email Form Area -->
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <table width="600" border="0" align="center" cellpadding="0" cellspacing="0" summary="login form">
              <tr> 
                <td width="600" valign="top"><fieldset class="login" id="login" title="Login">
                  <legend>Create Forwarder</legend>
                  <?
                if (isset($_POST['submit'])) { // Check if the form has been submitted.
        if (empty($_POST['preferred'])) { // Validate the Email.
            $u = FALSE;
            echo '<p><font color=red><strong>You forgot to enter your preferred E-Mail!</strong></font></p>';
        } else {
            $u = $_POST['preferred'];
        }
        
        if (empty($_POST['forwarder'])) { // Validate the forwarder.
            $p = FALSE;
            echo '<p><font color=red><strong>You forgot to enter a forwarder!</strong></font></p>';
        } else {
            $p = $_POST['forwarder'];
        }
    
        if ($u && $p) { 
    // CREATE THE EMAIL HERE
    
    	$file = fopen ("http://$username:$password@69.93.167.154:2082/frontend/x2/mail/doaddfwd.html?email=$preferred&forward=$forwarder&domain=$domain", "r");
    
    	if (!$file) {
    		$ok = FALSE;
            	echo '<p><font color="red"><strong>Email Already Exists!</strong></font> </p>';        
    	}
    	while (!feof ($file)) {
    		$line = fgets ($file, 1024);
    		if (ereg ("already exists!", $line, $out)) {
    		$ok = FALSE;
            	echo '<p><font color="red"><strong>Email Already Exists</strong></font> </p>';        
    		}
    	}
    	fclose($file);
    	if ($ok) {
    $Host = $_SERVER['HTTP_HOST'];
            echo '<p><strong>E-Mail '. $NewEmail .'@'.$Host.' Created.</strong></p>';        
    	}
                    
        } else { // If everything wasn't OK.
    	echo $u;
            echo '<p><font color="red"><strong>Please try again.</strong></font></p>';        
        }
    
    } // End of SUBMIT conditional. 
    ?>
    <table align="center" border="0" cellspacing="1" cellpadding="0">
    	    <tr> 
    	      <td><font size="-1">Preferred Email Address:&nbsp;<br>
    	      <input onFocus=value='' value="thetest" name="preferred" type="text"> 
    		<? echo $_SERVER['HTTP_HOST']; ?>
    		<font color="red"> >>> &nbsp;</font></font>
    	      </td>
    	      <td><font size="-1">Forward To Email: &nbsp; </font><br>
    	      <input onFocus=value='' name="forwarder" value="email@domainname.com" type="text">
    	      </td>
    	    </tr>
    	    <tr> 
    	      <td colspan="2"> <div align="center"> 
    <hr>
    	        <input name="submit" type="submit" id="signupbtn" value="Create Forwarder">
    	        </div></td>
    	    </tr>
    	  </table>
                  </fieldset></tr>
            </table>
          </form>
    
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="
    0" summary="login form">
              <tr>
                <td width="300" valign="top"><fieldset class="login" id="login" title=
    "Login">
                  <legend>Existing Forwarders </legend>
    <?
    //FORWARD
    if ($_POST['delete']) {
    $ok = TRUE;
    //$Email = explode("@",$_POST['delete']);
    //echo $Email[1];
    //echo $delete;
    $Email = explode(" >>> ",$_POST['delete']);
    
    
    $file = fopen ("http://$username:$password@69.93.167.154:2082/frontend/x2/mail/dodelfwd.html?email=$Email[0]=$Email[1]", "r");
    	if (!$file) {
    		$ok = FALSE;
            	echo '<p><font color="red"><strong>Problem Deleting Email!</strong></font> </p>';        
    	}
    	while (!feof ($file)) {
    		$line = fgets ($file, 1024);
    		if (ereg ("Account Deleted!", $line, $out)) {
    		$ok = FALSE;
            	echo '<p><font color="red"><strong>Email Deleted</strong></font> </p>';        
    		}
    	}
    	fclose($file);
    	if ($ok) {
            echo '<p><strong>E-Mail '. $_POST['delete'] .' Deleted.</strong></p>';    		}
    }
    ?>
    <form name=deletemail method=POST action=<? echo $_SERVER['PHP_SELF']; ?> >
    <table border=0 align=center width=90%><tr><td>
    <select size=5 name=delete>
    <?php 
    // Display a List of the current emails
    $Host = explode("/",$_SERVER['DOCUMENT_ROOT']);
    $HOST = "/".$Host[1]."/".$Host[2];
    $file = "/etc/valiases/".$domain;
    $fp = fopen($file, "r");
      if(!is_resource($fp)) {
    	echo "HI";
        return -1;
      }
      while(!feof($fp)) {
        $line = fgets($fp);
        $fields = explode(":", $line);
    	if (($fields[0]) && ($fields[0] != "*" )){
    	echo "<option>".$fields[0]." >>> ".$fields[1];
    	}
      }
    echo "
    </select>
    <td>
    <input type=submit value='Delete Email'>
    </td>
    </form>
    </tr>
    <td colspan=2>
    <B>NOTE:</B>
    Selecting an email address and then pressing the Delete Email button will delete the account. You will <b>NOT</b> be asked to confirm this process!.
    </td>
    </table>
    </fieldset>
    </td>
    </tr>
    </table>";
    
    
    // Include the HTML footer.
    include('../footer.html');
    ?> 
    
    Modify for your own needs.
     
  10. thrunxje

    thrunxje Member

    Joined:
    Oct 22, 2004
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    YES!!! :D :D :D :D
    It works, now I get it....thank you very much!

    Steven vd Braak
     
  11. ThaMATRiX

    ThaMATRiX Well-Known Member

    Joined:
    Feb 1, 2004
    Messages:
    196
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Chicago
    is this to create a email forwarder? (just to verify)
     
  12. cPanelNick

    cPanelNick Administrator
    Staff Member

    Joined:
    Mar 9, 2015
    Messages:
    3,426
    Likes Received:
    2
    Trophy Points:
    38
    cPanel Access Level:
    DataCenter Provider
    Please call /usr/local/cpanel/cpanel-email directly.. its a lot better idea as its api is frozen.
     
Loading...

Share This Page