Given an email as raw text, how can I send it using 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: [email protected]'.chr(10)),
    array('250', 'RCPT TO: [email protected]'.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.

Leave a Comment