Home > Article > Backend Development > How to detect whether the php mail function is enabled
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.
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.
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.
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.
<?php if( function_exists('mail') ){ 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!