Home  >  Article  >  Backend Development  >  How to detect whether the php mail function is enabled

How to detect whether the php mail function is enabled

青灯夜游
青灯夜游Original
2021-09-18 19:21:012085browse

Method: 1. Use the phpinfo() function to search for the "sendmail_path" item in the output results. If the value is "/usr/sbin/sendmail -t -i", it is supported; 2. Use mail() Send an email to test; 3. Use function_exists() to judge.

How to detect whether the php mail function is enabled

The operating environment of this tutorial: CentOS 6 system, PHP version 7.1, Dell G3 computer.

Based on a Linux system environment, you can try the following methods to confirm whether your Linux host supports the PHP mail() function.

1. phpinfo() detection

Save the following code as a phpinfo.php file and upload it to the root directory of the website:

<?php
 phpinfo(); 
?>

Access this file , search for "sendmail_path" on the page, the value is "/usr/sbin/sendmail -t -i", indicating that the mail() function is supported.

2. Mail sending test

Configure the following code, save it as a mail.php file and upload it to the root directory of the website:

<?php
$content = "hello world!";
$mail = "name@example.com"; // 将此邮箱地址改成你的收信地址
mail($mail, "Subject", $content); // 发送邮件
echo "Mail sent successfully!";
?>

Access this file directly. If the Linux host supports the mail() function, it will automatically send the email to your mailbox.

3. Judgment of function_exists

<?php
if( function_exists(&#39;mail&#39;) ){
	echo "支持mail()函数!";
} else {
	echo "不支持mail()函数!";
}
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to detect whether the php mail function is enabled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn