Jump to content

Email forwarding Program


j5uh

Recommended Posts

Please help!  ???

 

I'm in need of a software or php script that can forward an email to a list that is on a mysql db.

 

So an email will be sent to a specific email address and that should trigger the software or script to run through the email addresses in the db and forward that to everyone. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/108039-email-forwarding-program/
Share on other sites

  • Replies 57
  • Created
  • Last Reply

ok sweet!

i've switched providers to hostgator and i have cpanel running.

 

Does anyone have a script by any chance that can forward the email address to the correct mail list from a db?

 

Here is the script I'm using so far and it's not working...

 

 <?php 
include("config.php");
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

$sql = "SELECT $db_emailfield FROM $db_table";
$result = mysql_query($sql);

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

while($row = mysql_fetch_array($result)){
$email = $row['email'];

$message = $email_file; 

$mailhead = 'MIME-Version: 1.0' . "\n";
$mailhead .= 'Content-type: text/html; charset=UTF-8' . "\n";
$mailhead .= "From: $fromname\n";

if(mail ($email, $emailsubject, $message, "From: $fromname <$fromemail>", $mailhead))
echo "Messages sent";
}
?>

 

In cpanel configure your email forwarder to pipe received email to a php script like this:

 

[email protected] |/home/name/path/to/script.php

 

Now, make your php script executable by including the hash bang symbol (#!) and the path to your php installation on the top line before the opening php tag.  Also set error reporting to all.  Now, when you email this address, any errors will bounce back to your email client.  Let us know the errors you get. 

 

#!/usr/bin/php -q

<?php

ini_set('error_reporting',E_ALL); 

ok here is what i have now...

 

#!/usr/bin/php -q

<?php
ini_set('error_reporting',E_ALL);     
include("config.php");
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

$sql = "SELECT $db_emailfield FROM $db_table";
$result = mysql_query($sql);

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

while($row = mysql_fetch_array($result)){
$email = $row['email'];

$message = $email_file; 

$mailhead = 'MIME-Version: 1.0' . "\n";
$mailhead .= 'Content-type: text/html; charset=UTF-8' . "\n";
$mailhead .= "From: $fromname\n";

if(mail ($email, $emailsubject, $message, "From: $fromname <$fromemail>", $mailhead))
echo "Messages sent";
}
?>

 

The piping is correct ... I have /home/newhost/public_html/asd/ss/emailer.php

 

When I access the script directly, it's giving me a an error below

 

Warning: mail() expects parameter 3 to be string, resource given in /home/newhost/public_html/asd/ss/emailer.php on line 23

 

I've found this new script from here

 

<?

// 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);

 

?>

 

It's exactly what I need but I need it to pull the list of emails from a DB... how can I modify this to that?

Not tested, but something like this:

<?php
ini_set('error_reporting',E_ALL);     
include("config.php");
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_array($result)){
$email = $row['email'];
$ChangeTo = $email;
$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);
}
?>

jonsjava,

 

I've ran that code but it's giving an error at

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/newhost/public_html/asd/ss/mailer.php on line 11

 

Which is the code while($row = mysql_fetch_array($result)){

 

I'm having trouble with that line of code. I can't seem to figure it out.

jonsjava,

 

This is what  I have

 

#!/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 = "asd";

$db_emailfield = "emails";

 

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_array($result)){

$email = $row['email'];

$ChangeTo = "[email protected]";

$ForwardTo = $email;

 

// 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);

}

?>

 

Still giving me that line 11 error... =/

#!/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 = "asd";
$db_emailfield = "emails";

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)){
$email = $row['email'];
$ChangeTo = "[email protected]";
$ForwardTo = $email;

// 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);
}
?>

lets try this:

#!/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 = "asd";
$db_emailfield = "emails";

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

$sql = "SELECT `$db_emailfield` FROM `$db_table`;";
print $sql."\n"; //let me know all the output you receive, including your sql
$result = mysql_query($sql);
$array1 = array();
while($row = mysql_fetch_assoc($result)){
$array1[] .=$row['email'];
}
print_r($array1); 
?>

Lets see if it populates any data (making sure your query is working)

this is what i used and it's working!!!

 

#!/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);

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.