PHP mail() Function
PHP mail() Function
Category: PHP
The PHP mail()
function is a built-in PHP feature used to send emails directly from PHP scripts. It is typically enabled by default for your hosting account, and no further actions are required to activate it.
Check Account Settings
Ensure that the outgoing email functionality is not disabled. Check the Account Usage table in your control panel:
- SMTP Off: If outgoing email functionality is disabled, the PHP
mail()
function will not work.
Important Considerations
To send emails through your server, either the "To:" or "From:" address must be from your hosted domain. This is critical for contact forms or applications sending emails on your behalf.
Common Errors
If the recipient email isn't from a locally hosted mailbox, you'll see this error: "The email could not be sent. Possible reason: your host may have disabled the mail() function."
Additionally, popular CMS applications like WordPress, Joomla, and OpenCart have integrated mailing functions, and you only need to ensure the recipient email is hosted locally.
Test the PHP mail() Function
Here's a simple PHP script to test if the mail()
function is working on your server:
<?php $from = "From: Name <[email protected]>"; $to = "[email protected]"; $subject = "PHP mail test"; $body = "PHP mail works!"; if(mail($to, $subject, $body, $from)) echo "Message successfully sent!"; else echo "PHP mail FAILED"; ?>
Mail Service Setup
If your domain is not using the hosting provider's mail service (e.g., using third-party MX records like Google or Outlook), you should use SMTP instead of the mail()
function in your scripts.
If you haven't set up a mailbox yet, navigate to Email -> Mailboxes in the control panel to create one.
Tags: Email errors, hosted domain email, Joomla email, mail server, PHP email, PHP mail function, PHP mail() function example, PHP script email, PHPMailer, send email PHP, SMTP off, test PHP mail., WordPress email