问题
I can send an e-mail using php mail function. Other languages also have similar functions.
mail(to,subject,message,headers,parameters)
I was under an impression that to send an e-mail you need SMTP credentials. How does this function send emails? Which SMTP server does it use? How does it work?
回答1:
you have to configure your server to send emails ..
You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.
for example you can configure C:\xampp\php\php.ini
and c:\xampp\sendmail\sendmail.ini
for gmail to send mail.
in C:\xampp\php\php.ini
find extension=php_openssl.dll
and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
in php.ini file find [mail function]
and change
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now Open C:\xampp\sendmail\sendmail.ini
. Replace all the existing code in sendmail.ini with following code
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com
Now you have done!! create php file with mail function and send mail from localhost.
PS: don't forgot to replace my-gmail-id and my-gmail-password in above code.
Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
in the php.ini file
REF : LINK
回答2:
In php.ini, there is a field called SMTP. It is where you set the SMTP server. I think it defaults to localhost. Other related fields include smtp_port and sendmail_from. You use these for a Win32 system.
回答3:
The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn't include a local mail server.
Using the mail() function can be a lot of work. Instead use an external library which greatly simplifies this work for you. One such example is PHPMailer
来源:https://stackoverflow.com/questions/22970492/sending-e-mail-from-mail-function-php