k-Dev Posted December 24, 2006 Share Posted December 24, 2006 Hey allI 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 toThe 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!! Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/ Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 Are you asking how to use mail()? Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/#findComment-147247 Share on other sites More sharing options...
k-Dev Posted December 24, 2006 Author Share Posted December 24, 2006 I guess so, to do what is required above. Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/#findComment-147251 Share on other sites More sharing options...
k-Dev Posted December 24, 2006 Author Share Posted December 24, 2006 [s]I am willing to Pay someone US$15 via PayPal to code a script that will do this.Needed ASAP.Email / MSN me if you are interested: [removed][/s][EDIT]: We have found a fix for this, the offer is now closed. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/#findComment-147256 Share on other sites More sharing options...
k-Dev Posted December 25, 2006 Author Share Posted December 25, 2006 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$messageEOF;$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] Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/#findComment-147450 Share on other sites More sharing options...
j5uh Posted June 3, 2008 Share Posted June 3, 2008 how can i modify that code to pull the email addresses to forward to from a mysql db? Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/#findComment-556588 Share on other sites More sharing options...
j5uh Posted June 3, 2008 Share Posted June 3, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/31750-help-with-piped-php-email-forwarder-script/#findComment-556688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.