Given an email as raw text, how can I send it using PHP?

最后都变了- 提交于 2019-12-17 13:26:39

问题


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?


回答1:


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.




回答2:


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.




回答3:


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"


来源:https://stackoverflow.com/questions/6632399/given-an-email-as-raw-text-how-can-i-send-it-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!