如何检查是否启用了 PHP mail()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3889439/
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
How to check if PHP mail() is enabled?
提问by JasonS
I need to install a PHP site on a Microsoft server. For some reason the host isn't allowing the site to send e-mails. The host has sent me a code sample... which didn't work.
我需要在 Microsoft 服务器上安装一个 PHP 站点。出于某种原因,主机不允许该站点发送电子邮件。主机给我发送了一个代码示例......它不起作用。
Is there a way to check if the server allows sending of e-mails through the php mail() function?
有没有办法检查服务器是否允许通过 php mail() 函数发送电子邮件?
At this stage it is all about finger pointing and I need some proof here to show the client that the host is at fault.
在这个阶段,一切都与指责有关,我需要一些证据来向客户端表明主机有问题。
回答by Asuquo12
To check if the mail function is enabled on your server or PHP install, use
要检查您的服务器或 PHP 安装上是否启用了邮件功能,请使用
<?php
if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
}
?>
to check if it sending mail
检查它是否发送邮件
<?php
$email = "[email protected]";
$subject = "Email Test";
$message = "this is a mail testing email function on server";
$sendMail = mail($email, $subject, $message);
if($sendMail)
{
echo "Email Sent Successfully";
}
else
{
echo "Mail Failed";
}
?>
If the mail() function exist but mails not going, check if a mail transport agent (MTA)such as sendmail or postfix is installed on your server
If you on Linux :
如果你在 Linux 上:
Try;
尝试;
$ dpkg -S `which sendmail`
to install sendmail on Ubuntu;
在 Ubuntu 上安装 sendmail;
sudo apt-get install sendmail
https://www.digitalocean.com/community/questions/setting-up-email-with-sendmail
https://www.digitalocean.com/community/questions/setting-up-email-with-sendmail
回答by vicenteherrera
In Linux, by default mail() function uses sendmail from operating system.
在 Linux 中,默认情况下 mail() 函数使用来自操作系统的 sendmail。
In Windows, by default mail() doesn't do anything, you have to set it up editing php.ini file.
在 Windows 中,默认情况下 mail() 不执行任何操作,您必须通过编辑 php.ini 文件来设置它。
You can check what options from php.ini your hosting is using, writing a showinfo.php file, and inside it, write:
您可以检查您的主机正在使用的 php.ini 中的哪些选项,编写一个 showinfo.php 文件,并在其中写入:
<?php
phpinfo();
?>
Then if you call that webpage, it will show you all enabled options.
然后,如果您调用该网页,它将显示所有启用的选项。
To be able to send mail on Windows, these two values should be set up similar like these:
为了能够在 Windows 上发送邮件,这两个值应该像这样设置:
SMTP = smtp.isp.net (the name or ip of your server)
sendmail_from = [email protected]
XAMPP platform comes with a mailtodisk replacement, and you can set it to use "fakemail" in place of sendmail, also by means of an SMTP connection. You can take the sendmail folder that comes with XAMPP, and set it up in the php.ini that IIS uses.
XAMPP 平台带有一个 mailtodisk 替代品,您可以将它设置为使用“fakemail”代替 sendmail,也可以通过 SMTP 连接。可以拿XAMPP自带的sendmail文件夹,在IIS使用的php.ini中进行设置。
回答by Ryan S
This seems like an old post but you may use the function_exists() function, check if the function exist before using it.
这似乎是一篇旧帖子,但您可以使用 function_exists() 函数,在使用之前检查该函数是否存在。