可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.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?
回答1:
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 use define
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'] );
回答2:
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.
回答3:
Resolvendo: In Xampp menu, in Mercury - admin, In Mercury, menu Configuration-...SMTP Server, In the window remove the check in checkbox checked. See bellow:
Then in php.ini file:
[mail function] SMTP=127.0.0.1 <------------------change localhost to ip local
smtp_port=25
回答4:
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'] );
回答5:
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
回答6:
Answer is simple: your SMTP server does not support relayed messages. You might need to configure or login to it for relaying to work. Although I have done something similar before I cannot give any sample for now. Try searching "SMTP login in PHP"