472,353 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

OUTPUT raw email with php

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 6098
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
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

May 12 '09 #3
Expand|Select|Wrap|Line Numbers
  1. $header_beg = strpos($myEmail, "To:") + 3;
  2. $header_end = strpos($myEmail, "\n", $header_beg);
  4. substr_replace($myEmail, $myNewAddress, $header_beg, $header_end);
Simple, isn't it? Google next time.
May 12 '09 #4
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. }
  11. ?>
the $email variable would now be a string equal to something like the following:
Expand|Select|Wrap|Line Numbers
  2. MIME-Version: 1.0
  3. Received: by 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
  13. --001636e0a7e5a0a77304691cb37f
  14. Content-Type: text/plain; charset=ISO-8859-1
  15. Content-Transfer-Encoding: 7bit
  17. testing
  19. --001636e0a7e5a0a77304691cb37f
  20. Content-Type: text/html; charset=ISO-8859-1
  21. Content-Transfer-Encoding: 7bit
  23. testing
  25. --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.
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
6,050 Expert 4TB
Check out some of the good php mail clients (google). I recommend SwiftMailer.
May 12 '09 #7
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, 432 views)
May 12 '09 #8

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
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
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. {
  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.   }
  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
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?

May 13 '09 #12
221 100+
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
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
  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
  11. --0015175cdbba62c4f4046a85998a
  12. Content-Type: text/plain; charset=ISO-8859-1
  13. Content-Transfer-Encoding: 7bit
  15. testsss
  17. --0015175cdbba62c4f4046a85998a
  18. Content-Type: text/html; charset=ISO-8859-1
  19. Content-Transfer-Encoding: 7bit
  21. testsss
  23. --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.
May 22 '09 #14
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
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
221 100+

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!

Attached Files
File Type: zip email_forwarder.zip (6.8 KB, 383 views)
Aug 14 '09 #17
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
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

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

Similar topics

by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails...
by: Tushar | last post by:
hello all, i would like to know why this code compiles and gives the output Code 1: ---------- int main(){ printf("String" + 2); }
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
by: Yusuf INCEKARA | last post by:
by: Tarren | last post by:
Hi: I have an aspx page that takes a dataset, makes the datagrid, and displays it. The new thing I have to do is take this same output and...
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their...
by: bloc | last post by:
I am programming an interactive CV using xml, xslt and java script. The page consists of a header which contains links to various 'sections' on ...
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background:...
by: fdu.xiaojf | last post by:
Hi, I'm writing a program which imports an external module writing in C and calls a function provided by the module to do my job. But the method...
by: laredotornado | last post by:
Hi, I'm using PHP 5. Assuming there are no errors, how do I get the output of an exec call? I'm trying $cmd = 'strings < ' ....
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.