Jump to content

Recommended Posts

Hey all

I need a PHP script that will receive any email (via Email Piping setup in cPanel), then change the "TO" field of the email (to a variable predefined in the script), then forward the MODIFIED email to another email address (also predefined in the script).


Example:

===================================================

Predefined Variables:
$ConvertTo = "[email protected]"  //  NEW 'TO' address
$ForwardTo = "[email protected]"      //  Address to FORWARD email to


The message below is received by "[email protected]" and Piped to "http://mydomain.123/EmailConvertScript.php"

--- Original Message: ---
From: [email protected]
To:  [email protected]
Body: 12345
-------------------------


The "TO" Field would then be modified, and the modified message forwarded to "[email protected]"

--- Modified Message: ---
From: [email protected]
To:  [email protected]
Body: 12345
-------------------------

===================================================


Note: ONLY the 'TO' Field is modified, all other headers MUST remain original/intact.


Anyone have an idea of how I could achieve this ??


Reason:

I have an email account ([email protected]) that will ONLY accept emails sent to it DIRECTLY (sent to: [email protected]).

I need to be able to setup a 'catchall' email account on another domain, then have it forward all emails to my "[email protected]" address.

This is the only way around it.



Any help would be much appeciated, Thanks!!
Thanks Glen Elkins for the script below.  Hope this will be of use to others aswell. (Still needs a little fine-tuning with HTML messages)

[quote]
<?
// Set some vars
$ChangeTo = "[email protected]";
$ForwardTo = "[email protected]";

// Read the pipe
$open_file = fopen("php://stdin","r");

$email = "";
while (!feof($open_file)) {
$email .= fread($open_file,1024);
}
fclose ($open_file);

// Take info from emai;
$lines = explode("\n",$email);

$from = "";
$subject = "";
$message = "";
$to = $ChangeTo;
$splittingheaders = true;

for ($i=0;$i<count($lines);$i++) {
if ($splittingheaders) {
if (preg_match("/^From: (.*)/",$lines[$i],$matches)) {
if (strpos($lines[$i],"<")) {
// The name is before the email
$data = explode ("<",$lines[$i]);
$from = substr(trim($data[1]),0,-1);
} else {
$from = $matches[1];
}
}

if (preg_match("/^Subject: (.*)/",$lines[$i],$matches)) {
$subject = $matches[1];
}
} else {
$message .= $lines[$i]."\n";
}

if (trim($lines[$i]=="")) {
$splittingheaders = false;
}
}

$message = <<< EOF
$message
EOF;

$headers = "Content-type: text/html\n";
$headers .= "From: $from\n";
$headers .= "Return-Path: $from\n";
//$headers .= "To: $to\n";

mail ($ForwardTo,$subject,$message,$headers);

?>
[/quote]
  • 1 year later...

ok here is what i used to pull the email addresses from a db... hope this will help someone else in the future.

 

#!/usr/bin/php -q

 

<?php

ini_set('error_reporting',E_ALL);   

 

$db_host = "asd";

$db_user = "asd";

$db_pwd = "asd";

$db_name = "asd";

$db_table = "users";

$db_emailfield = "email";

 

mysql_connect($db_host, $db_user, $db_pwd);

mysql_select_db($db_name);

 

$sql = "SELECT `$db_emailfield` FROM `$db_table`;";

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)){

$emails = $row['email'];

$ChangeTo = "[email protected]";

$ForwardTo = $emails;

 

// Read the pipe

$open_file = fopen("php://stdin","r");

 

$email = "";

while (!feof($open_file)) {

  $email .= fread($open_file,1024);

}

fclose ($open_file);

 

// Take info from emai;

$lines = explode("\n",$email);

 

$from = "";

$subject = "";

$message = "";

$to = $ChangeTo;

$splittingheaders = true;

 

for ($i=0;$i<count($lines);$i++) {

  if ($splittingheaders) {

      if (preg_match("/^From: (.*)/",$lines[$i],$matches)) {

        if (strpos($lines[$i],"<")) {

            // The name is before the email 

            $data = explode ("<",$lines[$i]);

            $from = substr(trim($data[1]),0,-1);

        } else {

            $from = $matches[1];

        }

      }

   

      if (preg_match("/^Subject: (.*)/",$lines[$i],$matches)) {

        $subject = $matches[1];

      }

  } else {

      $message .= $lines[$i]."\n";

  }

 

  if (trim($lines[$i]=="")) {

      $splittingheaders = false;

  }

}

 

$message = <<< EOF

$message

EOF;

 

$headers = "Content-type: text/html\n";

$headers .= "From: $from\n";

$headers .= "Return-Path: $from\n";

//$headers .= "To: $to\n";

 

mail ($ForwardTo,$subject,$message,$headers);

}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.