php 从本地主机发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18288007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php send mail from localhost
提问by Sohil Desai
I am new at php. I was trying to send mail from php using this code.
我是 php 新手。我试图使用此代码从 php 发送邮件。
<?php
$to = '[email protected]';
$subject = 'The subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I have change settings in php.ini
我在 php.ini 中更改了设置
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]
& in sendmail.ini
& 在 sendmail.ini
# A freemail service example
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from [email protected]
auth on
user [email protected]
password xxxxxxxxx
# Set a default account
account default : Gmail
Now code runs successfully but I am not getting any mail.
现在代码运行成功,但我没有收到任何邮件。
回答by mram888
You must change the php.ini file:
您必须更改 php.ini 文件:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain
It won't work if localhost is set, for that reason change to your mail server.
如果设置了 localhost,它将不起作用,因此请更改您的邮件服务器。
回答by Mittal Patel
You won't have SMTP server installed by default so you can't send emails from localhost directly. Either you can set up SMTP server on local or use third party SMTP servers. Have a look at http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtpwhich gives you insight about how to send mail from localhost using third party SMTP server.
默认情况下您不会安装 SMTP 服务器,因此您无法直接从本地主机发送电子邮件。您可以在本地设置 SMTP 服务器或使用第三方 SMTP 服务器。看看http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp,它让您深入了解如何使用第三方 SMTP 服务器从本地主机发送邮件。
回答by K Cloud
The function will not work on your localhost, as the locahost doesn't works as a SMTP server, upload your content to a valid server with SMTP installed, and then go for the mail call.
该功能在您的本地主机上不起作用,因为 locahost 不能用作 SMTP 服务器,请将您的内容上传到安装了 SMTP 的有效服务器,然后进行邮件呼叫。
回答by Tomasz Banasiak
Your server does not have local mailserver.
您的服务器没有本地邮件服务器。
There are few solutions:
有几个解决方案:
- Install local mail server if you have sufficient rights
- Change your PHP settings to use other mail server (other open mailserver or auth-based ones like Gmail, Yahoo etc)
- Use one of available mail libraries which supports IMAP / POP3 to handle mail sending. SwiftMailer or Pear Mail are one of most commonly used.
- 如果您有足够的权限,请安装本地邮件服务器
- 更改您的 PHP 设置以使用其他邮件服务器(其他开放邮件服务器或基于身份验证的服务器,如 Gmail、Yahoo 等)
- 使用支持 IMAP/POP3 的可用邮件库之一来处理邮件发送。SwiftMailer 或 Pear Mail 是最常用的一种。
回答by Manish Chauhan
Try to set below things in your php.ini,
尝试在 php.ini 中设置以下内容,
- "SMTP" to "mail.YourDomain.com"
- "smtp_port" to "25"
- “SMTP”到“mail.YourDomain.com”
- “smtp_port”到“25”
OR you can set this option using php script also,
或者您也可以使用 php 脚本设置此选项,
// Please specify your Mail Server oR other mail server which you are going to use.(e.g gmail, yahoo)
// 请指定您要使用的邮件服务器或其他邮件服务器。(例如 gmail、yahoo)
ini_set("SMTP","mail.YourDomain.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
// 请指定一个 SMTP 号码 25 和 8889 是有效的 SMTP 端口。
ini_set("smtp_port","25");
回答by Mani Kandan
Here's the link that gives me the answer:
这是给我答案的链接:
Install the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip
安装“fake sendmail for windows”。如果您不使用 XAMPP,您可以在此处下载:http: //glob.com.au/sendmail/sendmail.zip
Modify the php.ini file to use it (commented out the other lines):
修改 php.ini 文件以使用它(注释掉其他行):
mail function
邮件功能
For Win32 only.
仅适用于 Win32。
SMTP = smtp.gmail.com
smtp_port = 25
For Win32 only.
仅适用于 Win32。
sendmail_from = <e-mail username>@gmail.com
For Unix only. You may supply arguments as well
仅适用于 Unix。你也可以提供参数
(default: "sendmail -t -i").
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
(ignore the "Unix only" bit, since we actually are using sendmail)
(忽略“仅限 Unix”位,因为我们实际上使用的是 sendmail)
You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:
然后,您必须在安装 sendmail 的目录中配置“sendmail.ini”文件:
sendmail
发邮件
smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com
回答by wieljer
I had this issue the past weeks, on my centos boxes, sharing this for others also having issues with mail() in php not sending... This resolved the issue for all of my mail() php scripts.
过去几周我在我的 centos 机器上遇到了这个问题,将这个问题分享给其他人也有问题的 php 中的 mail() 不发送...这解决了我所有的 mail() php 脚本的问题。
// Enable the sendmail in selinux
setsebool -P httpd_can_sendmail 1
// Add the following to /etc/postfix/main.cf
relayhost = smtp.server.com
// Then from command line
service postfix restart