469,288 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

OUTPUT raw email with php

n8kindt
221 100+
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
May 12 '09 #1
19 5829
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.
May 12 '09 #2
n8kindt
221 100+
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
May 12 '09 #3
Expand|Select|Wrap|Line Numbers
  1. $header_beg = strpos($myEmail, "To:") + 3;
  2. $header_end = strpos($myEmail, "\n", $header_beg);
  3.  
  4. substr_replace($myEmail, $myNewAddress, $header_beg, $header_end);
Simple, isn't it? Google next time.
May 12 '09 #4
n8kindt
221 100+
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:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/php -q
  2. <?php
  3. //-q for no output header
  4. // read from stdin 
  5. $fd = fopen("php://stdin", "r"); 
  6. $email = ""; 
  7. while (!feof($fd)) { 
  8. $email .= fread($fd, 1024); 
  9. }
  10.  
  11. ?>
the $email variable would now be a string equal to something like the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2. MIME-Version: 1.0
  3. Received: by 10.142.70.10 with HTTP; Mon, 4 May 2009 14:18:20 -0700 (PDT)
  4. Date: Mon, 4 May 2009 14:18:20 -0700
  5. Delivered-To: erased@gmail.com
  6. Message-ID: <e21c1a870905041418t58e3194atbc1997ef1fd50fd3@mail.gmail.com>
  7. Subject: testing message
  8. From: Nate <erased@gmail.com>
  9. To: recipient@whatever.com
  10. Cc: erased <erased@gmail.com>
  11. Content-Type: multipart/alternative; boundary=001636e0a7e5a0a77304691cb37f
  12.  
  13. --001636e0a7e5a0a77304691cb37f
  14. Content-Type: text/plain; charset=ISO-8859-1
  15. Content-Transfer-Encoding: 7bit
  16.  
  17. testing
  18.  
  19. --001636e0a7e5a0a77304691cb37f
  20. Content-Type: text/html; charset=ISO-8859-1
  21. Content-Transfer-Encoding: 7bit
  22.  
  23. testing
  24.  
  25. --001636e0a7e5a0a77304691cb37f--
  26.  

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.
May 12 '09 #5
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.
May 12 '09 #6
Markus
6,050 Expert 4TB
Check out some of the good php mail clients (google). I recommend SwiftMailer.
May 12 '09 #7
n8kindt
221 100+
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
File Type: txt sock.txt (3.3 KB, 409 views)
May 12 '09 #8
hoopy
88
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.
May 12 '09 #9
n8kindt
221 100+
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
May 12 '09 #10
n8kindt
221 100+
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:
Expand|Select|Wrap|Line Numbers
  1. class Swift_Transport_SimpleMailInvoker implements Swift_Transport_MailInvoker
  2. {
  3.  
  4.   /**
  5.    * Send mail via the mail() function.
  6.    * 
  7.    * This method takes the same arguments as PHP mail().
  8.    * 
  9.    * @param string $to
  10.    * @param string $subject
  11.    * @param string $body
  12.    * @param string $headers
  13.    * @param string $extraParams
  14.    * 
  15.    * @return boolean
  16.    */
  17.   public function mail($to, $subject, $body, $headers = null, $extraParams = null)
  18.   {
  19.     return mail($to, $subject, $body, $headers, $extraParams);
  20.   }
  21.  
  22. }
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
May 13 '09 #11
hoopy
88
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@yourdomain.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.
May 13 '09 #12
n8kindt
221 100+
@hoopy
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
May 14 '09 #13
n8kindt
221 100+
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)
Expand|Select|Wrap|Line Numbers
  1.  
  2. MIME-Version: 1.0
  3. Date: Fri, 22 May 2009 12:52:23 -0700
  4. Subject: test
  5. From: <sender@gmail.com>
  6. To: recip1@mydomain.com, recip2@mydomain.com
  7. Cc: recip3@mydomain.com, someone@otherdomain.com
  8. Bcc: recip4@mydomain.com
  9. Content-Type: multipart/alternative; boundary=0015175cdbba62c4f4046a85998a
  10.  
  11. --0015175cdbba62c4f4046a85998a
  12. Content-Type: text/plain; charset=ISO-8859-1
  13. Content-Transfer-Encoding: 7bit
  14.  
  15. testsss
  16.  
  17. --0015175cdbba62c4f4046a85998a
  18. Content-Type: text/html; charset=ISO-8859-1
  19. Content-Transfer-Encoding: 7bit
  20.  
  21. testsss
  22.  
  23. --0015175cdbba62c4f4046a85998a--
  24.  
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.
May 22 '09 #14
n8kindt
221 100+
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.
Jun 8 '09 #15
n8kindt,
I was very impressed with your spirit. I need an insight too, may be you can help it's along the same line.

I have a database of people with their various emails @gmail, @yahoo, @hotmail....etc, they submitted their email userid & password I want the script to regularly check up their specified email accounts, pick any new mail, parse it into its various components and forward in a specified way to our community portal for global viewing....just being thinking of how to go about it, your experience gave me courage......
Will appreciate your help!!!
Aug 14 '09 #16
n8kindt
221 100+
eyajuda,

thanks for the compliment. i'm going to attach the script that works for me and my server--it might take some tweaking for you to get it to work on your environment. after two months i haven't had any complaints about emails not being delivered but i think there is potential for that to happen. with php, however, i'm not sure there is a way to make it perfect. the reason for this is the email header doesn't include the bcc line and i had to resort to voodoo trickery to pull the email address out of the email lol. i can't say that the way i did it is 100% effective.

anyways, this script should get you headed in the right direction. let me know if you have problems setting it up or you see any way of improving it!

regards,
nate
Attached Files
File Type: zip email_forwarder.zip (6.8 KB, 368 views)
Aug 14 '09 #17
n8kindt,
Thanks for your prompt assistance, will go through the entire stuff and try to see if I can step up your voodoo.....laughs..
Well I have a step before outputting the mail....I must first off all use the guys credentials to check if he has a new mail, if yes, fetch it parse it and then output it. So I got this (http://phpclasses.fonant.com/browse/package/2.html) for the first part and then + yours... I guess I must be ok, any way if stucked will ask from friends around.....Thanks
Aug 14 '09 #18
n8kindt
221 100+
haha aren't we fortunate to have this site and phpclasses.org? rather than starting from scratch we both have something to build upon. you're also lucky you have some friends around that can help you too. the more heads you can put together the better. sounds like you may still be a ways from testing out my code... but don't forget to let me know how it works for you!
Aug 15 '09 #19
Yes ..what a blessing these sites are..any way there is nothing new under heaven we are just sitting on our various shoulders..will let you know...Thanx
Aug 15 '09 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Tushar | last post: by
54 posts views Thread by bnp | last post: by
8 posts views Thread by Yusuf INCEKARA | last post: by
6 posts views Thread by Tarren | last post: by
14 posts views Thread by fdu.xiaojf | last post: by
1 post views Thread by laredotornado | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.