The following script sends email using mail
function . But i am unable to send an email.Upon clicking submit
this gets displayed :
Warning: mail() [function.mail]: SMTP server response: 553 We do not relay non-local mail, sorry. in E:\xampp\htdocs\feedback.php on line 19
mail sent successfully
SCRIPT
<?php
if( isset( $_REQUEST['email'] ) ) {
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
mail("me@gmail.com" , $subject , $message , "From:".$email );
echo "mail sent successfully";
} else {
echo "<form method = 'post' action = 'feedback开发者_如何学Go.php'>
Email of sender : <input name = 'email' type = 'text' /> <br/>
Subject : <input name = 'subject' type = 'text'/> <br/>
Enter your feedback here : <textarea name = 'message' rows = 15 cols = 40 > </textarea> <br/>
<input type = 'submit'/>
</form>";
}
?>
I am using Apache as php server
Also tell why we have to write $subject
, $message
i.e with the $
sign in the mail argument , since we have declared $email
, $message
etc. , just above. Why can't we just write message , email , .. without the dollar sign?
Resolvendo:
In Xampp menu,
Go to mercury admin-->Configuration menu-->MercuryS SMTP Server-->Connection control. In that window, remove the check on the checkbox. See below:
Then in php.ini file:
[mail function] SMTP=127.0.0.1 <------------------change localhost to ip local
smtp_port=25
You're using XAMPP which by default comes with Mercury, which is not configured to send mail to a different server by default. It is basically there for debugging. Instructions do exist to configure it to do so, but Windows + Apache is generally best only as a debugging environment in my experience.
PHP variables all have the $ before them. It's called a
sigil
. It is what distinguishes them from, say, constants, class definitions, and functions. If you want to assign a value and then send it into a function, you need to use variables. You can usedefine
to set a constant if it is important enough, but trust me, those situations are rare and you should generally avoid them.
You can also do this, however:
mail("me@gmail.com" ,
$_REQUEST['subject'] ,
$_REQUEST['message'] ,
"From:".$_REQUEST['email'] );
Warning: mail() [function.mail]: SMTP server response: 553 We do not relay non-local mail, sorry. in E:\xampp\htdocs\feedback.php on line 19 mail sent successfully
That means your server is not properly configured. It should be able to send mails through some smarthost which allows relaying from your system. On a real server this is likely to be the MTA running on localhost.
Also tell why we have to write $subject , $message i.e with the $ sign in the mail argument , since we have declared $email , $message etc. , just above. Why can't we just write message , email , .. without the dollar sign?
That's because variables are prefixed with $
in PHP.
The following script sends email using mail function . But i am unable to send an email.Upon clicking submit this gets displayed :
The SMTP server prevents sending mail through it when not from local machine / domain.
This is to prevent spammers using the SMTP server to send mail. (open relay).
_Also tell why we have to write $subject , $message i.e with the $ sign in the mail argument , since we have declared $email , $message etc. , just above. Why can't we just write message , email , .. without the dollar sign _ ?
You don't have to use variables.
mail('me@gmail.com' , 'subject', 'test', 'From: someone@example.com');
is perfectly fine .
You could have also done:
mail("me@gmail.com" , $_REQUEST['subject'] , $_REQUEST['message'] , "From:".$_REQUEST['email'] );
This issue is due to your server configuraton. Now a days server stops open relaying the messages and method we are using in core PHP for sending mail is an open relay method.Stopping open relay reduces spamming. Thats the reson you are getting this error message,try mailing library like swift mail for sending mails from your SMTP
If you are using Windows + Xampp installation: This is because mercury service is already running and it is unable to send email outside the localhost. Go to xammp control panel and stop the mercury service. Hope it helps.
精彩评论