OUTPUT raw email with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n8kindt
    New Member
    • Mar 2008
    • 221

    OUTPUT raw email with php

    not sure if anyone can help me with this. i'm using my default email address to pipe to a certain php script. all i want the php script to do is read who the recipients are and send the email to said recipients mostly untouched. i know how to parse the email and read the recipients (that's the easy part). all i need is a way to send out the raw email text (which will be pulled using fopen ) without having to plug it into a function that uses separate parameters for the 'to', 'subject', 'message', etc, etc, fields. essentially, i just want to dump a raw email file into a function and let her fly.

    the reason why i want to do this is so i can set up a dynamic email forwarding system. an email addressed to a username in our database is then forwarded to the email address they have specified in their account settings. i have a shared hosting plan (through hostgator) and i'm fairly sure that a method using php is my only option to perform a task such as this. i'm also willing to bet it has something to do with sendmail but i don't have the slightest clue on where to begin.

    perhaps also i could figure out how to do this by looking at the inner workings of the mail() function? i haven't figured out where to find that either, however.

    any help would be much appreciated!!
    thanks - nate
  • unauthorized
    New Member
    • May 2009
    • 81

    #2
    I don't think PHP has the capabilities you are looking for, unless you are willing to phrase the email headers yourself.

    Or you could run a local SMTP server on the server machine and use PHP's socket functions to connect to it. This is pretty much how mail() works. You can get all settings from the php.ini file using ini_get() for a zero configuration script.
    This is in fact, slightly faster than phrasing data.

    Comment

    • n8kindt
      New Member
      • Mar 2008
      • 221

      #3
      actually, yes the email headers are already constructed. all i want to change is the recipient fields. so how can i use php in this way?

      i will also look into what you said about smtp. thanks for the reply

      nate

      Comment

      • unauthorized
        New Member
        • May 2009
        • 81

        #4
        Code:
        $header_beg = strpos($myEmail, "To:") + 3;
        $header_end = strpos($myEmail, "\n", $header_beg);
        
        substr_replace($myEmail, $myNewAddress, $header_beg, $header_end);
        Simple, isn't it? Google next time.

        Comment

        • n8kindt
          New Member
          • Mar 2008
          • 221

          #5
          that is not at all what my question relates to. here's an example. let's say i have a raw email that was read using this php script:
          Code:
          #!/usr/bin/php -q
          <?php
          //-q for no output header
          // read from stdin 
          $fd = fopen("php://stdin", "r"); 
          $email = ""; 
          while (!feof($fd)) { 
          $email .= fread($fd, 1024); 
          }
          
          ?>
          the $email variable would now be a string equal to something like the following:
          Code:
                                                                                                                                                                                                                                                                                                           
          MIME-Version: 1.0
          Received: by 10.142.70.10 with HTTP; Mon, 4 May 2009 14:18:20 -0700 (PDT)
          Date: Mon, 4 May 2009 14:18:20 -0700
          Delivered-To: erased@gmail.com
          Message-ID: <e21c1a870905041418t58e3194atbc1997ef1fd50fd3@mail.gmail.com>
          Subject: testing message
          From: Nate <erased@gmail.com>
          To: recipient@whatever.com
          Cc: erased <erased@gmail.com>
          Content-Type: multipart/alternative; boundary=001636e0a7e5a0a77304691cb37f
          
          --001636e0a7e5a0a77304691cb37f
          Content-Type: text/plain; charset=ISO-8859-1
          Content-Transfer-Encoding: 7bit
          
          testing
          
          --001636e0a7e5a0a77304691cb37f
          Content-Type: text/html; charset=ISO-8859-1
          Content-Transfer-Encoding: 7bit
          
          testing
          
          --001636e0a7e5a0a77304691cb37f--

          what i need to do is figure out a way to dump the $email variable into an email process (i would not assume this to be a direct PHP function but i could be wrong) and send it out like as if it were any other regular incoming email being processed. i don't want to touch this raw email other than to edit the recipients and send it on its merry way. as i said earlier, i have no problem parsing the email - i've written scripts to do this many times but i just need a mailman to send out raw email text.

          Comment

          • unauthorized
            New Member
            • May 2009
            • 81

            #6
            Looks like I misunderstood your last comment. Like I said, you will need an SMTP server to achieve what you want.

            All you need is to open a socket and complete a simple negotiation then.
            See the example at http://en.wikipedia.org/wiki/Simple_...communications
            If you want more information on the protocol, you will have to dig out RFC5321.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Check out some of the good php mail clients (google). I recommend SwiftMailer.

              Comment

              • n8kindt
                New Member
                • Mar 2008
                • 221

                #8
                thanks, markus. i will be looking into that next. thought i would post back with the progress i have made so far. i just found a good class on phpclasses.org that, as far as i can determine, directly connects to smtp from a socket and does send out raw email text. here's the entire project link: http://www.phpclasses.org/browse/package/2958.html but the meat and potatoes of what i need is the socket class which i am attaching to this post. i'll let you know how it goes
                Attached Files

                Comment

                • hoopy
                  New Member
                  • Feb 2009
                  • 88

                  #9
                  n8kindt,

                  I know you have solved your question but you seem to making it more difficult for yourself. As Markus said, check out Swift Mailer, it can do exactly what you require which will save you a lot of time.

                  Its very simple to include in your applications as well.

                  Best of luck.

                  Comment

                  • n8kindt
                    New Member
                    • Mar 2008
                    • 221

                    #10
                    glad you said something. i gave it a closer look and i think you guys are right. no sense in reinventing the wheel. i'll let you know how it goes but i don't anticipate any problems b/c it looks fairly straightforward

                    Comment

                    • n8kindt
                      New Member
                      • Mar 2008
                      • 221

                      #11
                      well that swiftmailer didn't get me anywhere at all. i traced it down to where it sends out the mail and this is what it does:
                      Code:
                      class Swift_Transport_SimpleMailInvoker implements Swift_Transport_MailInvoker
                      {
                        
                        /**
                         * Send mail via the mail() function.
                         * 
                         * This method takes the same arguments as PHP mail().
                         * 
                         * @param string $to
                         * @param string $subject
                         * @param string $body
                         * @param string $headers
                         * @param string $extraParams
                         * 
                         * @return boolean
                         */
                        public function mail($to, $subject, $body, $headers = null, $extraParams = null)
                        {
                          return mail($to, $subject, $body, $headers, $extraParams);
                        }
                        
                      }
                      as you can see, it ends up sending mail out using the php mail function. and since i don't see any function within swiftmail that parses mail into an object this does me no good whatsoever. ... unless i am missing something here? for now, i'm going to go back to the class i attached to the post yesterday

                      Comment

                      • hoopy
                        New Member
                        • Feb 2009
                        • 88

                        #12
                        Hi, thats just one of the ways it sends, it can POP before SMTP i.e authenticate which is what a lot of people use to send email. That does not use the PHP Mail function.

                        Im not 100% sure what you need to do here, as I understand it you want people to send a message to a "username" I assume something like somename@yourdo main.com, the system then parses the email coming in and looks up the real email address and forwards it on? If so then you need to pipe the email from sendmail or some other SMTP service into the PHP script which parses it and resends it using Swiftmailer or something similar.

                        However it just seems to be copies you are sending out to a new address so why even use PHP and not just configure all this as sendmail aliases and forward that way?

                        Cheers.

                        Comment

                        • n8kindt
                          New Member
                          • Mar 2008
                          • 221

                          #13
                          Originally posted by hoopy
                          Hi, thats just one of the ways it sends, it can POP before SMTP i.e authenticate which is what a lot of people use to send email. That does not use the PHP Mail function.

                          Im not 100% sure what you need to do here, as I understand it you want people to send a message to a "username" I assume something like somename@yourdo main.com, the system then parses the email coming in and looks up the real email address and forwards it on? If so then you need to pipe the email from sendmail or some other SMTP service into the PHP script which parses it and resends it using Swiftmailer or something similar.

                          However it just seems to be copies you are sending out to a new address so why even use PHP and not just configure all this as sendmail aliases and forward that way?

                          Cheers.
                          i really would love to do the whole sendmail aliases thing. but i don't think i have access to that sort of stuff on a shared server. if you know of a way for me to do that please let me know - i'm using hostgator with cpanel (if that makes any difference).

                          the purpose of all of this is so that i have a dynamic forwarding system. i need to be able to turn their forwarder on or off using a database that is manipulated by a script that assesses monthly sales.

                          the biggest problem with the swiftmailer thing is it does not parse the email and pack it into a nice little swiftmailer object. i would have to program a script to do that and it sounds like a far from efficient process. i'm making some headway on the php socket thing but the progress is slow- and i'm not sure there is a perfect way to do this with php :-S

                          Comment

                          • n8kindt
                            New Member
                            • Mar 2008
                            • 221

                            #14
                            ok, i've got a nice little setup for taking a raw email and changing the appropriate email addresses in the headers and then sending it out thru smtp via a socket connection. one last thing. consider the following example email:
                            (pay attention to the recipients in the header)
                            Code:
                                                                                                                                                                                                                                                                                           
                            MIME-Version: 1.0
                            Date: Fri, 22 May 2009 12:52:23 -0700
                            Subject: test
                            From: <sender@gmail.com>
                            [B][U]To: recip1@mydomain.com, recip2@mydomain.com[/U][/B]
                            [B][U]Cc: recip3@mydomain.com[/U], someone@otherdomain.com[/B]
                            [B][U]Bcc: recip4@mydomain.com[/U][/B]
                            Content-Type: multipart/alternative; boundary=0015175cdbba62c4f4046a85998a
                            
                            --0015175cdbba62c4f4046a85998a
                            Content-Type: text/plain; charset=ISO-8859-1
                            Content-Transfer-Encoding: 7bit
                            
                            testsss
                            
                            --0015175cdbba62c4f4046a85998a
                            Content-Type: text/html; charset=ISO-8859-1
                            Content-Transfer-Encoding: 7bit
                            
                            testsss
                            
                            --0015175cdbba62c4f4046a85998a--
                            now, i'm not exactly clear on how emails get processed at the server level but i am sure that the server iterates through each recipient separately. in other words, this email will be passed to this piped default email script 4x if it cannot find the recipients on this server. the problem is i don't know how to tell if the server is currently on recip1@mydomain .com, recip2, recip3, or recip4. is there any way of telling?

                            otherwise i guess i will have to set up a process for this to connect to a database and identify when it is seeing duplicate emails b/c of the multiple recipients.

                            Comment

                            • n8kindt
                              New Member
                              • Mar 2008
                              • 221

                              #15
                              alright just as an update, i finally got this whole thing to work. turned out to be quite the challenge but a fun project nonetheless. if anyone in the future would like the code to this project just message me on here and i will send it to you.

                              Comment

                              Working...