给定一封电子邮件作为原始文本,我如何使用 PHP 发送它? [英] Given an email as raw text, how can I send it using PHP?

查看:22
本文介绍了给定一封电子邮件作为原始文本,我如何使用 PHP 发送它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 脚本,我的邮件服务器通过 STDIN 将电子邮件发送到该脚本.是否有一种直接/不复杂的方法来获取原始电子邮件字符串并将其发送/转发/中继到特定的电子邮件地址?

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?

我对使用 PHP 的 mail()Pear::Mail 犹豫不决,因为据我所知,我不能只传递原始电子邮件.我必须解析标题,从而冒着剥离或更改原始电子邮件内容的风险.

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?

推荐答案

我遇到了同样的问题,但找到了一个可行的解决方案.在 PHP 中打开一个套接字并远程登录"原始电子邮件数据.像这样:

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); 

如果您不知道,您可能需要查找 mx-host.我敢肯定,Google 对此有答案.

You might need to lookup the mx-host if you dont know it. Google has an answer to that i'm sure.

这篇关于给定一封电子邮件作为原始文本,我如何使用 PHP 发送它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆