Asked  7 Months ago    Answers:  5   Viewed   35 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?

 Answers

54

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.

Wednesday, March 31, 2021
 
Trott
answered 7 Months ago
18

It's because of your SMTP server. Not the server that runs PHP. The SMTP server is blocked by the destination mail server. For making sure of it, if you change your settings (in PHP code) with Gmail server, it won't go to the spam folder any more.

It worked pretty well for a few months, but now the emails started to appear in a Spam folder for some of my customers?

That's because the destination mail server has added your SMTP server to their blacklist.

Saturday, May 29, 2021
 
Bere
answered 5 Months ago
25

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: me@mydomain.com" but if I send something that is addressed "From: you@fakedomain.com" 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: www.data@servername" 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 "www.data" 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 www.data@servername, 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 www.data 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 "www.data" 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 sendmail.cf file in use on the system and look for a line beginning with "Ft", e.g.

Ft/etc/mail/trusted-users

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.

Saturday, May 29, 2021
 
diegoiglesias
answered 5 Months ago
28

Your error says:

SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

So let's look at https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting :

"SMTP Error: Could not connect to SMTP host."

This may also appear as SMTP connect() failed or Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).

Some techniques to diagnose the source of this error are discussed below.

GoDaddy

Popular US hosting provider GoDaddy imposes very strict (to the point of becoming almost useless) constraints on sending email. They block outbound SMTP to ports 25, 465 and 587 to all servers except their own. This problem is the subject of many frustrating questions on Stack Overflow. If you find your script works on your local machine, but not when you upload it to GoDaddy, this will be what's happening to you. The solution is extremely poorly documented by GoDaddy: you must send through their servers, and also disable all security features, username and password (great, huh?!), giving you this config for PHPMailer:

$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;

GoDaddy also refuses to send with a From address belonging to any aol, gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This is because all those domains deploy SPF and DKIM anti-forgery measures, and faking your from address is forgery.

You may find it easier to switch to a more enlightened hosting provider.

Saturday, May 29, 2021
 
EastSw
answered 5 Months ago
29

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);
Wednesday, June 9, 2021
 
commonpike
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :