473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 6248
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, 449 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, 386 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

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

Similar topics

16
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 in the testwwws users mailbox. As you can see I...
5
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); }
54
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;
8
by: Yusuf INCEKARA | last post by:
I have a stored procedure : CREATE PROCEDURE STP_GETSTORELIST @RETCUR CURSOR VARYING OUTPUT AS set @RETCUR = CURSOR FORWARD_ONLY STATIC FOR SELECT ID,STORE_NAME FROM T_INF_STORE ORDER BY...
6
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 send it as an email. Is there an easy way to do...
32
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 preprocessors produce identical output given as input...
3
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 the xml cv, a left and right menu, and a central...
8
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: I'm creating a survey system for our company, for...
14
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 produces a lot of output to the stdout, and this...
1
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 < ' . escapeshellarg($file) . ' | sed - n \'s|.*/Count...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.