Sendmail.php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

TESTING LOCALLY.......

using my script as you mentioned.......

$recipient = "andrew@thanweb.co.uk";
$subject = "Website Contact Form";
$mailheaders = "From: <website@thanweb.co.uk> \r\n";
$mailheaders .= "Reply-To: ".$_POST["email"];
//Send the mail.
mail($recipient, $subject, $msg, $mailheaders) or die("Email not sent");
?>

i get this error in my log....

[Mon Feb 04 16:22:56 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <<website@thanweb.co.uk> >: missing or malformed local part in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\sendmailnew.php on line 24, referer: http://localhost/feedback.html
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

I have also uploaded the previous version to my server and the die function was not invoked as though it had sent the email? But nothing received?
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

I HAVE FIGURED IT OUT WOOO HOO.

I have been to forum after forum and repeatidly seen people being told off for negating the < and > around the email address, such as website@thanweb.co.uk instead of <website@thanweb.co.uk>.

Just out of curiosity i removed the < and > and saved my script having website@thanweb.co.uk as my email and is now working perfectly.

WHY oh why do people recommend using the < and > characters if it causes the script to malfunction??

Many thanks for the help on this anyhow.

Andrew
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

Try it with out the from header on the non-local server and see what happens! - that will totally narrow it down to that as the issue.
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:Try it with out the from header on the non-local server and see what happens! - that will totally narrow it down to that as the issue.

You know what...

It is working on the local server but not from the non-local server?!?!?!?

weird.

And neither does it when you remove the from header.

I don't want it working locally i want it working on a live website of course so now am pee'd off again.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

If the same code works on your local server and not on a non local server - there is an issue with their mail handling. Some naff hosts turn it off. Have a look at the log on the non local server to see what the error is. Post what you have codewise now too, as there have been lots of changes....
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:If the same code works on your local server and not on a non local server - there is an issue with their mail handling. Some naff hosts turn it off. Have a look at the log on the non local server to see what the error is. Post what you have codewise now too, as there have been lots of changes....
I spoke to them and they ran a test cgi mailer of some sort and told me the error was that i had my email addresses in the script set to addresses other than root@thanweb.co.uk. So i changed them what they suggested and hey presto...still does not work.

Getting really frustrated now...it's been 9hours and 35 minutes today so far and still no joy.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

Okay. The webhost doesn't trust php developers to make their code safe. (Yours isn't, but we were going to deal with that later!).

The trouble can be when scripts use unchecked user input in the headers. A spammer can add extra stuff other than the expected email address in the form and your code would allow them to send spam to potentially millions. Their solution is to not trust developers to do the necessary checks and only allow root@thanweb.co.uk in the headers thereby stopping email header injection.

EDIT: They are not happy with the reply to header as well as the from header

So your script is not working on their server because

Code: Select all

$mailheaders = "From: <website@thanweb.co.uk> \r\n";
$mailheaders .= "Reply-To: ".$_POST["email"];
Strip those two lines out and it will magically work.

Then go to a better web host that trusts its php developers, but learn how to check for valid email addresses in the input before using it in the headers of a mail() call
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:Okay. The webhost doesn't trust php developers to make their code safe. (Yours isn't, but we were going to deal with that later!).

The trouble can be when scripts use unchecked user input in the headers. A spammer can add extra stuff other than the expected email address in the form and your code would allow them to send spam to potentially millions. Their solution is to not trust developers to do the necessary checks and only allow root@thanweb.co.uk in the headers thereby stopping email header injection.

EDIT: They are not happy with the reply to header as well as the from header

So your script is not working on their server because

Code: Select all

$mailheaders = "From: <website@thanweb.co.uk> \r\n";
$mailheaders .= "Reply-To: ".$_POST["email"];
Strip those two lines out and it will magically work.

Then go to a better web host that trusts its php developers, but learn how to check for valid email addresses in the input before using it in the headers of a mail() call

Nice try thank you.....but it is still not working though.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

To save your sanity :banghead: , this works...
It posts to itself so call it anything.php!

Code: Select all

<?php
if(isset($_POST['submit'])){ //form submitted so send email
$recipient = "andrew@thanweb.co.uk";
$subject = "Website Contact Form";
$msg = 'Name: '.$_POST['name'].'<br/>';
$msg .= 'E-Mail: '.$_POST['email'].'<br/>';
$msg .= 'Message: '.$_POST['message'].'<br/>';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
mail($recipient,$subject,$msg,$headers) or die("Mail could not be sent");
 
echo "Thank you for filling out the form.<br/> Here's what you entered...<br/>".$msg;
exit();
}
//form not submitted so output form...
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>E-mail Form</title>
</head>
 
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><strong>Name: </strong><br>
<input type="text" name="name" size="25" /></p>
<p><strong>E-Mail Address: </strong><br/>
<input type="text" size="25" name="email" /> </p>
<p><strong>Message:</strong><br/>
<textarea name="message" cols="30" rows="5"></textarea></p>
<p><input type="submit" name= "submit" value="send" /></p>
</form>
</body>
</html>
Certainly on my webhost!!!

Otherwise try swiftmailer - search for it on the forum.
I haven't cleaned the input - you'd want htmlentities() on all the data so no nasties will come to you in your email. You are not vulnerable to header injection with that code though.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Sendmail.php

Post by Chris Corbyn »

You just need to add a Return-Path header I think since that's what it will prefer to use in the MAIL FROM of SMTP. Put < angle brackets > around it too. Meh, I hate mail() and it gazillion inconsistencies.

Code: Select all

// To send HTML mail, the Content-type header must be set
$headers = "Return-Path: <your@address.com>\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
If you do that then you can likely have the From: header that way you had it in the first place ;)
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:To save your sanity :banghead: , this works...
It posts to itself so call it anything.php!

Code: Select all

<?php
if(isset($_POST['submit'])){ //form submitted so send email
$recipient = "andrew@thanweb.co.uk";
$subject = "Website Contact Form";
$msg = 'Name: '.$_POST['name'].'<br/>';
$msg .= 'E-Mail: '.$_POST['email'].'<br/>';
$msg .= 'Message: '.$_POST['message'].'<br/>';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
mail($recipient,$subject,$msg,$headers) or die("Mail could not be sent");
 
echo "Thank you for filling out the form.<br/> Here's what you entered...<br/>".$msg;
exit();
}
//form not submitted so output form...
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>E-mail Form</title>
</head>
 
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><strong>Name: </strong><br>
<input type="text" name="name" size="25" /></p>
<p><strong>E-Mail Address: </strong><br/>
<input type="text" size="25" name="email" /> </p>
<p><strong>Message:</strong><br/>
<textarea name="message" cols="30" rows="5"></textarea></p>
<p><input type="submit" name= "submit" value="send" /></p>
</form>
</body>
</html>
Certainly on my webhost!!!

Otherwise try swiftmailer - search for it on the forum.
I haven't cleaned the input - you'd want htmlentities() on all the data so no nasties will come to you in your email. You are not vulnerable to header injection with that code though.

Hello,

I need a standalone php file residing on the server as i am going to replace the website with my new version that is completely Flash built and cannot embed html inside it.

Like this site i am currently building (http://www.thanweb.co.uk/gap.html) it has a contact form (funnily enough on the contact us page).

So thank you very much but this is not an option for me as the current version of thanweb is being replaced soon.

Thankyou again your efforts to help me out have been amazing and i cannot thank you enough...i am in talks with my web hosts and trying to come to some esolution there at present.

If and when i get this script working and figure out the problems with my host i will post here and let you all know what is was.

Andrew
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

PROBLEM FOUND - NOT RESOLVED YET THOUGH :banghead:

After contacting my webhosts AGAIN, they told me to log into my domain control panel and reset the server for my account.

So i go into control panel section of the website and in great big red writing i get the message...error: there is a problem with the linux server configuration on your acocunt, please contact technical support to resolve this issue.

I have at present left it with my host to fix this problem and if all goes well.......i should be able to get my php functioning by the end of business today....so they say.

Well, a GREAT BIG THANKS to all who tried to help....very kind.

If at any point i can help anyone out i will do my best to return the favour to this forum.

Andrew
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

I GIVE UP....

ok so i got my webhosts to fix the problem with the server and all my scripts now function.....so i was happy for a while.

THEN.....i write new script and test locally to get no email come through, check the error log in apache/log files etc and there is a new errorregarding my smtp connection not functioning.

hmmmmmmmm

I have a connection with sky broadband using smtp.sky.com as outgoing and incoming as mail.thanweb.co.uk which up until now has worked.

Phone sky up.....hello your smtp server is not functioning....that's right sir we have closed it down and created a new one running over ssl which is smtp.tools.sky.com. They gave me the new socket numbers i need to allow connection etc and still no joy.

Phone sky again.....

Still not working....ok sir what's your sky email address....that's not relevant i am using my own domain name xxxx@thanweb.co.uk....oh no sir we no longer support external email addresses going through our smtp servers, if your domain name does not include sky.com then it will be rejected from the server.

So now i cannot even use my local apache to make connection thanks to them f+++++s.

Woo Hoo what a couple of days i've had.

:banghead:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Sendmail.php

Post by RobertGonzalez »

Ok, this is seriously turning out to be a lot more difficult than it should be for you.

Here is an easy test:

Code: Select all

<?php
$message = 'This is a test message';
$subject = 'Testing out mail';
$recipient = 'andrew@thanweb.co.uk';
$headers = "From: Test Sender <root@thanweb.co.uk>\r\n";
 
if (! mail($recipient, $subject, $message, $headers)) {
  echo "Could not send the message as it was entered: \n$message";
} else { 
  echo "Message accepted for delivery to $recipient.";
}
?>
Save that file and upload it. Call it. See what comes of it.

On your local machine, if you do not have a mail server you might run into problems sending mail. On the hosted server, if the mail server is knackered it would be up to the host to handle that. But generally speaking hosts allow mail to be sent, just in metered amounts.
Post Reply