OUTPUT raw email with php  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | |
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
| | Member | | Join Date: May 2009
Posts: 81
| | | re: OUTPUT raw email with php
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.
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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
| | Member | | Join Date: May 2009
Posts: 81
| | | re: OUTPUT raw email with php - $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.
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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: -
#!/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: -
-
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.
| | Member | | Join Date: May 2009
Posts: 81
| | | re: OUTPUT raw email with php
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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,948
| | | re: OUTPUT raw email with php
Check out some of the good php mail clients (google). I recommend SwiftMailer.
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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
| | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: OUTPUT raw email with php
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.
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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: - 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
| | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: OUTPUT raw email with php
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.
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php Quote:
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@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. 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
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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) -
-
MIME-Version: 1.0
-
Date: Fri, 22 May 2009 12:52:23 -0700
-
Subject: test
-
From: <sender@gmail.com>
- To: recip1@mydomain.com, recip2@mydomain.com
- Cc: recip3@mydomain.com, someone@otherdomain.com
- Bcc: recip4@mydomain.com
-
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.
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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.
| | Newbie | | Join Date: Aug 2009
Posts: 3
| | | re: OUTPUT raw email with php
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!!!
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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
| | Newbie | | Join Date: Aug 2009
Posts: 3
| | | re: OUTPUT raw email with php
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
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California
Posts: 221
| | | re: OUTPUT raw email with php
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!
| | Newbie | | Join Date: Aug 2009
Posts: 3
| | | re: OUTPUT raw email with php
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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|