开发者

How to check if PHP mail() is enabled?

I开发者_JAVA技巧 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.

Is there a way to check if the server allows sending of e-mails through the php mail() function?

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.


To check if the mail function is enabled on your server or PHP install, use

<?php
if ( function_exists( 'mail' ) )
{
    echo 'mail() is available';
}
else
{
    echo 'mail() has been disabled';
}
?>

to check if it is sending mail as intended;

<?php 

$email = "youremail@gmail.com";
$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 mail's not going, check if a mail transport agent (MTA) such as sendmail or postfix is installed on your server.

If on Linux :

Try;

$ dpkg -S `which sendmail`

to install sendmail on Ubuntu;

sudo apt-get install sendmail

https://www.digitalocean.com/community/questions/setting-up-email-with-sendmail


In Linux, by default mail() function uses sendmail from operating system.

In Windows, by default mail() doesn't do anything, you have to set it up editing php.ini file.

You can check what options from php.ini your hosting is using, writing a showinfo.php file, and inside it, write:

   <?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:

SMTP = smtp.isp.net (the name or ip of your server)
sendmail_from = me@isp.net

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.


This seems like an old post but you may use the function_exists() function, check if the function exist before using it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜