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

Sendmail.php

Post by hairytea »

Everah | Please use appropriate code tags when posting code in the forums. Tags you can use are [code], [{LANG}] (where {LANG} is the lowercase name of the language you are using) or [syntax="{LANG}"] (again, where {LANG} is the lowercase name of the language you are using). Thank you.

Hello all,

I am new to this forum and hope i have posted appropraite material for this section of the forum...here goes....

I have been training to become a web developer for 2 years now and can comfortably write in Perl and javascript as well as the obvious html xhtml css etc. I am now learning php and have come stuck at the first hurdle...

I am trying to create a simple web form using a sendmail.php file that emails the contant back to me (like a feedback form), according to the book i am learning from.

The Problem.
The script works the echo function correctly and displays the desired content and users input to the browser as it should, but the form itself is not being sent via as email as per my coding.

My PHP script is as follows........

Code: Select all

<!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>Sending mail from the form</title>
</head>
 
<body>
<?php
 
echo "<p>Thank You, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your email address is: <b>".$_POST["email"]."</b>.</p>";
echo "<p>Your message was:<br/>";
echo $_POST["message"]."</p>";
 
//start building the mail string
$msg = "Name:   ".$_POST["name"]."\r\n";
$msg .= "E-Mail:    ".$_POST["email"]."\r\n";
$msg .= "Message:   ".$_POST["message"]."\r\n";
 
//set up the mail
$recipient = "root@xxxxxx.co.uk";
$subject = "Website Contact Form";
$mailheaders = "From: My Web Site <http://www.xxxxxx.co.uk> \r\n";
$mailheaders .= "Reply-To: ".$_POST["email"];
 
//Send the mail.
mail($recipient, $subject, $msg, $mailheaders);
 
?>
</body>
</html>
I have set the perimeters in my php.ini file as follows(my isp is sky and the smtp address of smtp.sky.com is correct and functions using outlook to send mail with no problems)...

Code: Select all

[mail function]
; For Win32 only.
SMTP = smtp.sky.com
smtp_port = 25
 
; For Win32 only.
;sendmail_from = website@xxxxxx.co.uk
I am using a simple html form to call the php script as follows....

Code: Select all

<!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="sendmailnew.php" 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" value="send" /></p>
</form>
</body>
</html>
The error log located at C:\Program Files\Apache Group\Apache2\logs\ prints the following error statement...
[Mon Feb 04 14:20:07 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <My Web Site <http://www.thanweb.co.uk> >: "@" or "." expected after "My" in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\sendmailnew.php on line 24, referer: http://localhost/feedback.html
I have apache installed and am testing locally using http://localhost/
I am running PHP vers 5.1.4

If anyone has ANY idea what may be causing this NOT to send the mail i would be very very grateful as my patience is wearing extremely thin now.

Thank you

Andrew

Everah | Please use appropriate code tags when posting code in the forums. Tags you can use are [code], [{LANG}] (where lang is the lowercase name of the language you are using) or [syntax="{LANG}"] (again, where language is the lowercase name of the language you are using). Thank you.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Re: Sendmail.php

Post by arukomp »

Well, I don't think it's allowed to send e-mails from localhost outside your lan. Maybe that's why you're getting this error? Just a guess, because I wasn't allowed to send e-mails from my computer using a php script.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

I ran your error through html_entity_decode() to see what it was in English!
[Mon Feb 04 14:20:07 2008] [error] [client 127.0.0.1] PHP Warning: mail() [function.mail]: SMTP server response: 501 >: "@" or "." expected after "My" in C:\Program Files\Apache Group\Apache2\htdocs\sendmailnew.php on line 24, referer: http://localhost/feedback.html
It is a header problem.

Code: Select all

$mailheaders = "From: My Web Site <http://www.xxxxxx.co.uk> \r\n";

Emails are sent from email addresses even in scripts, not website addresses. Loose the line for now - you have sendmail_from set anyway.

See if that makes it work.

When you have got it working, do a google on "email header injection" before you go live with what you have done!
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

Thank you for your quick response...

I have tried your solution....and i get the same results?

Any other possibilities?
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

N.B.

My script is line for line and character for character EXACTLY the same as my book instructs me to do so. I have checked, re-checked, re-checked etc etc etc and there is not one character different.

Also, why would it teach to include the headers if it does not work?

Sorry if i sound like im being funny, im not i promise i am just being confused and frustrated.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

Post the error you get now please
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

arukomp wrote:Well, I don't think it's allowed to send e-mails from localhost outside your lan. Maybe that's why you're getting this error? Just a guess, because I wasn't allowed to send e-mails from my computer using a php script.

It cannot be alocalhost problem as i have just determined.

I have uploaded the page to http://www.thanweb.co.uk/feedback.html and also uploaded the sendmailnew.php file to the htdocs directory and tried to run live on my my web server only to get exactly the same results. NO email being sent but the echo function working and correctly printing to screen the users input etc.

please feel free to check if necessary..... http://www.thanweb.co.uk/feedback.html

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

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:Post the error you get now please

[Mon Feb 04 15:26:08 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <My Web Site <root@thanweb.co.uk> >: "@" or "." expected after "My" 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 »

Sorry should have mentioned (i think) that th error (as per the log) is mentioning line 24 which is...

mail($recipient, $subject, $msg, $mailheaders);


How relevant that information is i am not sure??
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

That info is very relevant it is expecting an @ after My which is in your from header!
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

Everah | Please use appropriate code tags when posting code in the forums. Tags you can use are [code], [{LANG}] (where lang is the lowercase name of the language you are using) or [syntax="{LANG}"] (again, where language is the lowercase name of the language you are using). Thank you.
andym01480 wrote:That info is very relevant it is expecting an @ after My which is in your from header!
ok ok ok

i have changed my php script as follows.....

Code: Select all

<?php
echo "<p>Thank You, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your email address is: <b>".$_POST["email"]."</b>.</p>";
echo "<p>Your message was:<br/>";
echo $_POST["message"]."</p>";
//start building the mail string
$msg = "Name:   ".$_POST["name"]."\r\n";
$msg .= "E-Mail:    ".$_POST["email"]."\r\n";
$msg .= "Message:   ".$_POST["message"]."\r\n";
//set up the mail
$recipient = "root@thanweb.co.uk";
$subject = "Website Contact Form";
$mailheaders = "From: My Web Site <root@thanweb.co.uk> \r\n";
$mailheaders .= "Reply-To: ".$_POST["email"];
//Send the mail.
mail($recipient, $subject, $msg, $mailheaders);
?>
And still get this error...this is the latest error since trying to send the form last (locally)....
[Mon Feb 04 15:39:56 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <My Web Site <root@thanweb.co.uk> >: "@" or "." expected after "My" in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\sendmailnew.php on line 24, referer: http://localhost/feedback.html
There is an @ sign so it can't be that??

Yours (extremely frustrated and giving my pc flying lessions)

Andrew

Everah | Please use appropriate code tags when posting code in the forums. Tags you can use are [code], [{LANG}] (where lang is the lowercase name of the language you are using) or [syntax="{LANG}"] (again, where language is the lowercase name of the language you are using). Thank you.
Last edited by RobertGonzalez on Tue Feb 05, 2008 10:35 am, edited 2 times in total.
Reason: Please use proper bbCode tags when posting code in the forums
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

The error you are getting when run through html_entity_decode is
[Mon Feb 04 15:39:56 2008] [error] [client 127.0.0.1] PHP Warning: mail() [function.mail]: SMTP server response: 501 >: "@" or "." expected after "My" in C:\Program Files\Apache Group\Apache2\htdocs\sendmailnew.php on line 24, referer: http://localhost/feedback.html
What that means is that your server wants an @ or . after My - in other words it wants just an email address even though what you have written is RFC822 blah blah.

Humour it and please change

$mailheaders = "From: My Web Site <root@thanweb.co.uk> \r\n";

to

Code: Select all

 
$mailheaders = "From: root@thanweb.co.uk \r\n";
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:The error you are getting when run through html_entity_decode is
[Mon Feb 04 15:39:56 2008] [error] [client 127.0.0.1] PHP Warning: mail() [function.mail]: SMTP server response: 501 >: "@" or "." expected after "My" in C:\Program Files\Apache Group\Apache2\htdocs\sendmailnew.php on line 24, referer: http://localhost/feedback.html
What that means is that your server wants an @ or . after My - in other words it wants just an email address even though what you have written is RFC822 blah blah.

Humour it and please change

$mailheaders = "From: My Web Site <root@thanweb.co.uk> \r\n";

to

Code: Select all

 
$mailheaders = "From: root@thanweb.co.uk \r\n";
i am now one step closer thank you...

i have done exactly as you stated but only to be confronted with a new error log...

[Mon Feb 04 15:49:09 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <<root@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

Any ideas on this one?

Or alternatively - how are you running through html_entity_decode to simplify the results?

As this may help me? possibly? maybe? maybe not? aaaaaaaaaaaaarggggggggghhhhhhhhh
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Sendmail.php

Post by andym01480 »

All I was doing is

Code: Select all

<?php
$string="
[Mon Feb 04 15:39:56 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <My Web Site <root@thanweb.co.uk> >: "@" or "." expected after "My" in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\sendmailnew.php on line 24, referer: http://localhost/feedback.html";
echo html_entity_decode($string);
 
?>
because your server uses html_entities to convert things like & to & which is a bit unreadable.
Try a different word to "root" like "website" and upload it rather than local in case there are localhost server problems.
root might be reserved.
Also

mail($recipient, $subject, $msg, $mailheaders);

to

Code: Select all

mail($recipient, $subject, $msg, $mailheaders) or die("Email not sent");
so you know if it worked immediately!
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Sendmail.php

Post by hairytea »

andym01480 wrote:All I was doing is

Code: Select all

<?php
$string="
[Mon Feb 04 15:39:56 2008] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 <My Web Site <root@thanweb.co.uk> >: "@" or "." expected after "My" in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\sendmailnew.php on line 24, referer: http://localhost/feedback.html";
echo html_entity_decode($string);
 
?>
because your server uses html_entities to convert things like & to & which is a bit unreadable.
Try a different word to "root" like "website" and upload it rather than local in case there are localhost server problems.
root might be reserved.
Also

mail($recipient, $subject, $msg, $mailheaders);

to

Code: Select all

mail($recipient, $subject, $msg, $mailheaders) or die("Email not sent");
so you know if it worked immediately!
My isp has told me that my php scripts MUST be set with the mail box as root@thanweb.co.uk though?
Post Reply