Viewed   219 times

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'.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('', 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]); 

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

Friday, October 14, 2022
// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = "Hi,nnHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'passwordxxx'

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
Monday, December 26, 2022

You should replace the SMTPSecure value from SSL to TLS. It will resolve your issue connect to the Server and mail was sent. I have tested your code it is working fine.

$mail->SMTPSecure = "tls"; // sets the prefix to the servier //$mail->SMTPSecure = "ssl";

Second Replacement is your replayto email $mail->AddReplyTo($youremail, "Reply to"); //optional You have given $emailpassword it is wrong.

Saturday, August 20, 2022

OK firstly you're allowing a random stranger (who submits the POST request) to set the FROM address of the email message by accessing $_POST['from'] and using that as the FROM address. That's not going to work if (as others have suggested) you're setting a FROM address that is not a valid sender address according to your mail server -- and according to whatever upstream mail server you're using. So, for example, in my own network I can send an email "From: [email protected]" but if I send something that is addressed "From: [email protected]" then it will probably bounce or be thrown out. So you probably need to hard code that From address to whatever your IT guys say, not use the $_POST variable.

The fact that your email is actually going out as "From: [email protected]" probably means a couple of things:

  • Your PHP is configured so that the mail() function calls the standard sendmail command installed on most Unix/Linux systems.
  • Your Apache server, that is running the PHP processes, is running as the "" user on that system.
  • The "www-data" user on that system is not authorized to set an alternative FROM address, and so sendmail is forcing the FROM address to be [email protected], regardless of what you tell it.

Your system admin needs to tell you what address to hard code as the FROM address on that system. In addition, he or she needs to configure the mail system to allow to set its own FROM address. Assuming that you are using actual real sendmail (and not something else configured to work like sendmail, such as postfix), then that would be done by adding a line "" to the file /etc/mail/trusted-users or whatever trust file is set up on that system. The clue to that is to find the file in use on the system and look for a line beginning with "Ft", e.g.


More information can be found in the sendmail docs, e.g. on a Red Hat / CentOS system with the sendmail-cf package installed there will be a /usr/share/sendmail-cf/README file on that system with useful information in it about trusted-users.

Tuesday, October 4, 2022

If the cookie is generated from script, then you can send the cookie manually along with the cookie from the file(using cookie-file option). For example:

# sending manually set cookie
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie"));

# sending cookies from file
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

In this case curl will send your defined cookie along with the cookies from the file.

If the cookie is generated through javascrript, then you have to trace it out how its generated and then you can send it using the above method(through http-header).

The utma utmc, utmz are seen when cookies are sent from Mozilla. You shouldn't bet worry about these things anymore.

Finally, the way you are doing is alright. Just make sure you are using absolute path for the file names(i.e. /var/dir/cookie.txt) instead of relative one.

Always enable the verbose mode when working with curl. It will help you a lot on tracing the requests. Also it will save lot of your times.

curl_setopt($ch, CURLOPT_VERBOSE, true);
Sunday, December 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :