Given an email as raw text, how can I send it using PHP?
I've got a PHP script that my mail server is piping emails to via STDIN. Is there a straightforward/non-convoluted way to take a raw email string and send/forward/relay it to a specific email address?
I hesitate to use PHP's mail()
or Pear::Mail
because, as far as I can tell, I can't just pass along the raw email. I'd have 开发者_开发技巧to parse the headers, thereby running the risk of stripping or altering the original email's contents.
What would be the recommended way to do this with minimal "molesting" of the original email contents?
Note: If there isn't a built-in approach, are there any existing libraries that might help me do this?
I had the same problem but found a solution that seams to work. Open a socket in PHP and "telnetting" the raw emaildata. Something like this:
$lSmtpTalk = array(
array('220', 'HELO my.hostname.com'.chr(10)),
array('250', 'MAIL FROM: me@hostname.com'.chr(10)),
array('250', 'RCPT TO: you@anotherhost.com'.chr(10)),
array('250', 'DATA'.chr(10)),
array('354', $lTheRawEmailStringWithHeadersAndBody.chr(10).'.'.chr(10)),
array('250', 'QUIT'.chr(10)),
array('221', ''));
$lConnection = fsockopen('mail.anotherhost.dk', 25, $errno, $errstr, 1);
if (!$lConnection) abort('Cant relay, no connnection');
for ($i=0;$i<count($lSmtpTalk);$i++) {
$lRes = fgets($lConnection, 256);
if (substr($lRes, 0, 3) !== $lSmtpTalk[$i][0])
abort('Got '.$lRes.' - expected: '.$lSmtpTalk[$i][0]);
if ($lSmtpTalk[$i][1] !== '')
fputs($lConnection, $lSmtpTalk[$i][1]);
}
fclose($lConnection);
You might need to lookup the mx-host if you dont know it. Google has an answer to that i'm sure.
There's this article about sending a plain text email using PHP. You can use Zend/Mail.php package from Zend Framework.
require_once 'Zend/Mail.php';
require_once 'Zend/Validate/EmailAddress.php';
$mail=new Zend_Mail();
$validator=new Zend_Validate_EmailAddress();
///////...
$mail->setBodyText(strip_tags($_POST['message']));
$mail->setBodyHtml($_POST['message']);
the setBodyText serves as an alternative mime types header for text only email, while setBodyHtml for hmtl version.
Hope that helps. Let us know if that works.
I was facing the same issue, the best solution I could come up with (linux environment) was to pipe the raw message into maildrop, and give it a mailfilter file that just specified the intended recipient.
With that in place I found Exchange server would identify the message as a duplicate, as one with the same message-id was already in its store, so I piped through reformail as well to generate a new message-id, ending up with:
/usr/bin/reformail -R Message-ID: Original-Message-ID: -A'Message-ID:' | /usr/bin/maildrop maildrop-file
...fed the raw email into that from PHP with proc_open()
"maildrop-file" contains nothing other than
to "!recipient@domain.com"
I wrote a simple tool that leverages PHPMailer (so you don't need to worry about the negotiation/TLS/EHLO etc) at https://github.com/xrobau/smtphack which is based on my answer of Send Mail from raw body for testing purposes
精彩评论