Hey,

My code epic fails... I get delivery failed when sending an email to the email addess that pipe's to this script:

#!/usr/bin/php -q
<?php
ini_set('memory_limit', '256M'); //The concern here is having enough mem for emails with attachments.
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);

// handle email

$lines = explode("\n", $email);

// empty vars

$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i]."\n";
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}

if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}



$theEmail .= $from . "\n";
$theEmail .= $subject . "\n";
$theEmail .= $headers . "\n";
$theEmail .= $message . "\n";
$theEmail .= "-------------------\n\n\n";

$file = 'email.txt';
// Get the current contents
$ccontents = file_get_contents($file);
// Append a new person to the file
$ccontents .= $theEmail;
// Write the contents back to the file
file_put_contents($file, $ccontents);
?>

Can anyone help?
Dan

Where is your send code ?

What send code?

Dan

This code just puts data in a file. If you get delivery failed, it is in some other part.

Yes, thats all I want it to do at the min, is get the data from the email and put it in a file? Do I have to forward it on to a real mail box then?

Dan

I think so, I've never tried it this way.

Ok... I tried adding:

mail("daniel@DOMAIN.com", $subject, $message);

At the end still failed

It might just be the code but you should check that your web host allows this. I know that mine doesn't.

I am my web host ;) and I allow it :P I fixed the problem anyway thanks, I needed to CHMOD the file to 755 :)

Dan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.