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
Share on other sites

  • Replies 57
  • Created
  • Last Reply

Yeah, i would just set up a forwarder.  So, that when this address is emailed its sent to a php script that retrieves your mailing list and mails everybody.

 

Are you using cpanel?

Link to comment
Share on other sites

could you explain the cPanel way too?

I guess I could use one of my other hosting companies to see how it would work... but if I can get it working on godaddy, it would be ideal.

Link to comment
Share on other sites

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";
}
?>

 

Link to comment
Share on other sites

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

 

email@youraddress.com |/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); 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

I've found this new script from here

 

<?

// Set some vars

$ChangeTo = "changeto@domain.com";

$ForwardTo = "forwardto@domain.com";

 

// 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?

Link to comment
Share on other sites

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 = "forwardto@domain.com";

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 = "noreply@asd.com";

$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... =/

Link to comment
Share on other sites

#!/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 = "noreply@financial-robotics.com";
$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);
}
?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 = "noreply@asd.com";

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

}

?>

Link to comment
Share on other sites

whatever you're trying to parse is probably not correct

 

just in case try sending some plain text, to make sure the mailer works.

 

You can use the mail extensions in PEAR, I don't think they will go to junk.

Link to comment
Share on other sites

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.