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

本小妞迷上赌 提交于 2019-11-27 15:20:01

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