Jump to content

Email forwarding Program


j5uh

Recommended Posts

  • Replies 57
  • Created
  • Last Reply

Top Posters In This Topic

so I've figured out the From and Subject not showing up issue but the email message is not going through ...  ???

 

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 = "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 = "noreply@123.com";
$subject = "123";
$message = "";
$to = $ForwardTo;
$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

so...you ARE receiving the message, but it's empty? 

 

what is the content of $lines[$i], $message?

 

Why don't you make a non-executable version of this script, so you can visit it as a web page and then try echoing these variables.  And then post them.

 

Also, send it to a yahoo account that won't filter it out as junk.

Link to comment
Share on other sites

Ok so this is what I need the script to do... an email is sent with data in the body to a certain email every day at a certain time. When that email is sent it should pipe it to the script and the script should auto pull the emails from a db I made using mysql and forward that email on to the list.

 

I'm still a noobie in php so thanks for baring with me.

Link to comment
Share on other sites

ok i've tested $message = "testing msg"; and it sends  the msg through.

 

The following part of the script:

 

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

 

I don't understand at all...  ???

Link to comment
Share on other sites

this part of your script is  parsing the $lines array by REGEX for the subject, from, and message contents. 

 

I am not sure why the following is an if-else statement (the latter part should assign the message contents to $message and be mandatory):

 

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

         $subject = $matches[1];

      }

   } else {

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

   }

 

Link to comment
Share on other sites

Well...first try something like this, to make sure you can read an email in to standard input and then re-send it. (sorry the code key is not 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);

 

$ForwardTo='wherever@yahoo.com ';

$subject='whatever';

$headers='your headers';

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

?>

Link to comment
Share on other sites

ok so now I have stripped it down and followed the direction here at http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914/index.html

 

Here is the code that I am using now:

#!/usr/bin/php
<?php

// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

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

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // this is a header
        $headers .= $lines[$i]."\n";

        // look out for special headers
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."\n";
    }

    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}

$ForwardTo = 'aaaa@gmail.com';
mail ($ForwardTo,$subject,$message,$headers);
?>

 

The email piping WORKS! but i'm getting bunch of MS Word html mess along with the email. is there a way to filter all that out and just have the body of the email?

Link to comment
Share on other sites

This is what  I get

 

From xxx@asd.com Wed Jun 04 14:44:08 2008

Received: from adsl-02020202002.dsl.hstntx.sbcglobal.net ([00.00.00.00]:1234 helo=prexxix)

      by gator465.hostgator.com with esmtpa (Exim 4.68)

      (envelope-from <123@cs.com>)

      id 1K3yu0-0005O6-Hn

      for 123@cs.com; Wed, 04 Jun 2008 14:44:08 -0500

From: "john" <123@cs.com>

To: <aasl@cs.com>

Subject: 12323213123

Date: Wed, 4 Jun 2008 14:44:15 -0500

Message-ID: <asdioj@sokd.com>

MIME-Version: 1.0

Content-Type: multipart/alternative;

      boundary="----=_NextPart_000_00E2_01C8C651.766A7CC0"

X-Mailer: Microsoft Office Outlook 12.0

Thread-Index: AcjGe11qMOjrcJG4R7amtFB1Cvy/Uw==

Content-Language: en-us

x-cr-hashedpuzzle: yYE= AdPi Axu3 BAAg ECQt EaW5 EbYl Ey7J E3EF FHiq GPV0 HF4a IH/L I8i8 JXAS KX8S;1;cwBpAGcAbgBhAGwAQABmAGkAbgBhAG4AYwBpAGEAbAAtAHIAbwBiAG8AdABpAGMAcwAuAGMAbwBtAA==;Sosha1_v1;7;{FFA2519B-E285-46B0-92BB-9425F2DC2D68};agAuAHMAdQBoAEAAZgBpAG4AYQBuAGMAaQBhAGwALQByAG8AYgBvAHQAaQBjAHMALgBjAG8AbQA=;Wed, 04 Jun 2008 19:44:13 GMT;MQAyADMAMgAzADIAMQAzADEAMgAzAA==

x-cr-puzzleid: {FFA2519B-E285-46B0-92BB-9425F2DC2D68}

 

 

 

This is a multipart message in MIME format.

 

------=_NextPart_000_00E2_01C8C651.766A7CC0

Content-Type: text/plain;

      charset="us-ascii"

Content-Transfer-Encoding: 7bit

 

1231231232

 

 

------=_NextPart_000_00E2_01C8C651.766A7CC0

Content-Type: text/html;

      charset="us-ascii"

Content-Transfer-Encoding: quoted-printable

 

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =

xmlns:o=3D"urn:schemas-microsoft-com:office:office" =

xmlns:w=3D"urn:schemas-microsoft-com:office:word" =

xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =

xmlns=3D"http://www.w3.org/TR/REC-html40">

 

<head>

<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =

charset=3Dus-ascii">

<meta name=3DGenerator content=3D"Microsoft Word 12 (filtered medium)">

<style>

<!--

/* Font Definitions */

@font-face

      {font-family:"Cambria Math";

      panose-1:2 4 5 3 5 4 6 3 2 4;}

@font-face

      {font-family:Calibri;

      panose-1:2 15 5 2 2 2 4 3 2 4;}

/* Style Definitions */

p.MsoNormal, li.MsoNormal, div.MsoNormal

      {margin:0in;

      margin-bottom:.0001pt;

      font-size:11.0pt;

      font-family:"Calibri","sans-serif";}

a:link, span.MsoHyperlink

      {mso-style-priority:99;

      color:blue;

      text-decoration:underline;}

a:visited, span.MsoHyperlinkFollowed

      {mso-style-priority:99;

      color:purple;

      text-decoration:underline;}

span.EmailStyle17

      {mso-style-type:personal-compose;

      font-family:"Calibri","sans-serif";

      color:windowtext;}

.MsoChpDefault

      {mso-style-type:export-only;}

@page Section1

      {size:8.5in 11.0in;

      margin:1.0in 1.0in 1.0in 1.0in;}

div.Section1

      {page:Section1;}

-->

</style>

<!--[if gte mso 9]><xml>

<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />

</xml><![endif]--><!--[if gte mso 9]><xml>

<o:shapelayout v:ext=3D"edit">

<o:idmap v:ext=3D"edit" data=3D"1" />

</o:shapelayout></xml><![endif]-->

</head>

 

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

 

<div class=3DSection1>

 

<p class=3DMsoNormal>1231231232<o:p></o:p></p>

 

</div>

 

</body>

 

</html>

 

------=_NextPart_000_00E2_01C8C651.766A7CC0--

Link to comment
Share on other sites

so, i assume the message you sent was this?  1231231232 ???

 

here's for a php snippet on how to parse this kind of page:

 

http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/

 

 

basically all you want to do is return all the text between </head> &  </html>  and then strip out all the html.

 

i think you are very close to solving your problem.  you just need to be a little bit more patient. 

 

 

 

Link to comment
Share on other sites

;D

 

ok ... so what I'm getting as a result is a getting better. But still some crap that gets emailed... here it is...

 

From asd@a.com Wed Jun 04 16:19:47 2008
Received: from 123123123.dsl.hs123123obal.net ([123123]:1421 helo=prexxix)
       by gator465.hostgator.com with esmtpa (Exim 4.68)
       (envelope-from <asd@aa.com>)
       id 1K40OY-0001MR-T5
       for oskdpsk@aol.com; Wed, 04 Jun 2008 16:19:47 -0500
From: "John" <12323@ao.com>
To: <asdsd@aol.com>
Subject: test
Date: Wed, 4 Jun 2008 16:19:54 -0500
Message-ID: <123434##.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
       boundary="----=_NextPart_000_00E7_01C8C65E.D3062580"
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: AcjGiLnI28Lp3X5tSLuLhc6u9F0ABA==
Content-Language: en-us
x-cr-hashedpuzzle: AB0P A5cy CTO+ CX7e CxSy DvBH ECBa HdzE Htgh Ic06 JKPY Jjka Jk2A KtuP LsxO L6XP;1;cwBpAGcAbgBhAGwAQABmAGkAbgBhAG4AYwBpAGEAbAAtAHIAbwBiAG8AdABpAGMAcwAuAGMAbwBtAA==;Sosha1_v1;7;{3A797A8D-B98B-4371-A084-67C4021C6B09};agAuAHMAdQBoAEAAZgBpAG4AYQBuAGMAaQBhAGwALQByAG8AYgBvAHQAaQBjAHMALgBjAG8AbQA=;Wed, 04 Jun 2008 21:19:51 GMT;dABlAHMAdAA=
x-cr-puzzleid: {3A797A8D-B98B-4371-A084-67C4021C6B09}



This is a multipart message in MIME format.

------=_NextPart_000_00E7_01C8C65E.D3062580
Content-Type: text/plain;
       charset="us-ascii"
Content-Transfer-Encoding: 7bit

Asidjasoijd asodj asod j


------=_NextPart_000_00E7_01C8C65E.D3062580
- Show quoted text -
Content-Type: text/html;
       charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<meta name=3DGenerator content=3D"Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
       {font-family:"Cambria Math";
       panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
       {font-family:Calibri;
       panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
       {margin:0in;
       margin-bottom:.0001pt;
       font-size:11.0pt;
       font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
       {mso-style-priority:99;
       color:blue;
       text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
       {mso-style-priority:99;
       color:purple;
       text-decoration:underline;}
span.EmailStyle17
       {mso-style-type:personal-compose;
       font-family:"Calibri","sans-serif";
       color:windowtext;}
.MsoChpDefault
       {mso-style-type:export-only;}
@page Section1
       {size:8.5in 11.0in;
       margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
       {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p class=3DMsoNormal>Asidjasoijd asodj asod j<o:p></o:p></p>

</div>

</body>

</html>

------=_NextPart_000_00E7_01C8C65E.D3062580--

Link to comment
Share on other sites

ok sweet... now I've modified it even more and this is what  I have:

 

#!/usr/bin/php
<?php

// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

function get_string_between($string, $start, $end){
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);   
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
}


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

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // this is a header
        $headers .= $lines[$i]."\n";

        // look out for special headers
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."\n";
    }

    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}

$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\n";
$headers .= "From: xxx";
$ForwardTo = 'xxx';
mail ($ForwardTo,$subject,$message,$headers);
?>

 

and I'm getting this :

 

This is a multipart message in MIME format. ------=_NextPart_000_010F_01C8C663.3932C670 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Testing 1234 ------=_NextPart_000_010F_01C8C663.3932C670 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Testing 1234
------=_NextPart_000_010F_01C8C663.3932C670--

 

Which means I've stripped away all that using this line:

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

 

But how can  I get rid of that other mess?

Link to comment
Share on other sites

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.