{"id":124860,"date":"2025-11-27T13:14:15","date_gmt":"2025-11-27T13:14:15","guid":{"rendered":"\/ph\/tutorials\/how-to-send-emails-using-php-mail-function\/"},"modified":"2026-11-13T07:20:13","modified_gmt":"2026-11-13T07:20:13","slug":"how-to-send-emails-using-php-mail-function","status":"publish","type":"post","link":"\/ph\/tutorials\/how-to-send-emails-using-php-mail-function\/","title":{"rendered":"How to send emails with the PHP mail() function"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p class=\"wp-block-paragraph\">The <strong>PHP mail() function<\/strong> sends an email directly from a PHP script by passing the message to the server&#8217;s mail system, with no external library to install. You call mail() with a recipient, a subject, and a message, then add headers for the sender, reply address, copies, or HTML content.<\/p><p class=\"wp-block-paragraph\">This guide covers the mail() syntax and headers, a test script that confirms your server can send email, and examples for HTML emails and contact form messages. It also explains how to fix common problems, such as messages landing in spam, missing SPF, DKIM, and DMARC records, and disabled mail functions.<\/p><p class=\"wp-block-paragraph\">Because mail() has no SMTP authentication, it works best for testing and low-volume notifications. The final section compares mail() with PHPMailer and explains when to switch.<\/p>\n\n\n\n<\/p><h2 id=\"h-what-is-the-php-mail-function\" class=\"wp-block-heading h-t-title-2\">What is the PHP mail() function?<\/h2><p class=\"wp-block-paragraph\">The PHP mail() function is a built-in function that sends an email directly from a PHP script. Because mail() is part of the core <a data-click-id=\"hgr-post-content-link-php-scripting-language\" href=\"\/ph\/tutorials\/what-is-php\/\">PHP scripting language<\/a>, there&#8217;s no library to install, which makes it a quick option for contact form notifications and test messages.<\/p><p class=\"wp-block-paragraph\">The mail() function doesn&#8217;t deliver email by itself. On Linux servers, it passes each message to the local mail transfer agent (MTA), such as Sendmail, Postfix, or Exim, through the program set in the sendmail_path directive of php.ini. On Windows, mail() connects to the SMTP server set in the SMTP and smtp_port directives.<\/p><p class=\"wp-block-paragraph\">The function requires three parameters: the recipient&#8217;s address, the subject line, and the message body. It returns true when the server accepts the message for delivery, which <strong>doesn&#8217;t confirm that the email reached the inbox<\/strong>.<\/p><p class=\"wp-block-paragraph\">The mail() function has no built-in SMTP authentication, attachment helper, or detailed error reporting, so deliverability depends on how the server is configured. That&#8217;s why mail() suits learning, testing, and low-volume notifications better than production email.<\/p><h2 id=\"h-how-to-send-emails-using-the-php-mail-function\" class=\"wp-block-heading h-t-title-2\">How to send emails using the PHP mail() function<\/h2><p class=\"wp-block-paragraph\">Here&rsquo;s the basic syntax of the mail() function:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mail(to, subject, message, headers, parameters);<\/pre><ul class=\"wp-block-list\">\n<li><strong>to<\/strong> &ndash; the recipient&rsquo;s email address.<\/li>\n\n\n\n<li><strong>subject<\/strong> &ndash; the email subject line.<\/li>\n\n\n\n<li><strong>message<\/strong> &ndash; the body of the email.<\/li>\n\n\n\n<li><strong>headers<\/strong> &ndash; additional information like From, Reply-To, or Content-Type.<\/li>\n\n\n\n<li><strong>parameters<\/strong> &ndash; optional extra options passed to the system&rsquo;s sendmail program.<\/li>\n<\/ul><h3 id=\"h-understanding-php-mail-components\" class=\"wp-block-heading h-t-title-3\">Setting email headers, CC, and BCC<\/h3><p class=\"wp-block-paragraph\">Headers tell mail clients who sent the message, where replies go, and who else receives a copy. Pass them as the fourth parameter, separated by rn:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$to = \"example@domain.com\";\n$subject = \"Test email from PHP\";\n$message = \"This is a plain text email example.\";\n$headers = \"From: hello@domain.com\" . \"rn\" .\n           \"Reply-To: hello@domain.com\" . \"rn\" .\n           \"Cc: manager@domain.com\" . \"rn\" .\n           \"Bcc: archive@domain.com\" . \"rn\" .\n           \"X-Mailer: PHP\/\" . phpversion();\n\nmail($to, $subject, $message, $headers);\n?&gt;<\/pre><ul class=\"wp-block-list\">\n<li><strong>From<\/strong> &ndash; sets the sender address. mail() needs a From header, or a sendmail_from default in php.ini.<\/li>\n\n\n\n<li><strong>Reply-To <\/strong>&ndash; defines where replies should be directed.<\/li>\n\n\n\n<li><strong>Cc<\/strong> &ndash; sends a visible copy to another recipient.<\/li>\n\n\n\n<li><strong>Bcc<\/strong> &ndash; sends a hidden copy that other recipients can&#8217;t see.<\/li>\n\n\n\n<li><strong>X-Mailer<\/strong> &ndash; identifies that the message was sent by PHP. This header is optional.<\/li>\n<\/ul><p class=\"wp-block-paragraph\">To send the same message to several main recipients, separate their addresses in $to with commas, for example &#8220;anna@domain.com, ben@domain.com&#8221;.<\/p><h3 id=\"h-creating-a-test-file-for-php-mail\" class=\"wp-block-heading h-t-title-3\">Creating a test file for PHP mail<\/h3><p class=\"wp-block-paragraph\">A test file confirms whether mail() can hand messages to your server&#8217;s mail system. Create a testmail.php file in your website&#8217;s root directory:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$to = \"youraddress@example.com\";\n$subject = \"PHP Mail Test\";\n$message = \"This is a test message sent from PHP.\";\n$headers = \"From: webmaster@yourdomain.com\";\n\nif (mail($to, $subject, $message, $headers)) {\n    echo \"Email accepted for delivery.\";\n} else {\n    echo \"Email was not accepted. Check your server's mail configuration.\";\n}\n?&gt;<\/pre><p class=\"wp-block-paragraph\">Replace both addresses with real ones, using an address on your own domain in the From header. Upload the file to your <a data-click-id=\"hgr-post-content-link-web-server\" href=\"\/ph\/tutorials\/what-is-a-web-server\/\" target=\"_blank\" rel=\"noopener\">web server<\/a>, open https:\/\/yourdomain.com\/testmail.php in your browser, and check both the inbox and the spam folder of the recipient address.<\/p><p class=\"wp-block-paragraph\">&#8220;Email accepted for delivery&#8221; only means the server took the message, not that it arrived. If nothing arrives within a few minutes, work through the troubleshooting steps for spam filtering and domain authentication. Delete testmail.php after testing so nobody else can use it to send messages.<\/p><h3 id=\"h-sending-html-emails-with-php\" class=\"wp-block-heading h-t-title-3\">Sending HTML emails with PHP<\/h3><p class=\"wp-block-paragraph\">By default, mail() sends plain-text emails. To send HTML emails, add a <strong>Content-Type<\/strong> header:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$to = \"example@domain.com\";\n$subject = \"HTML Email Example\";\n\n$message = \"\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;HTML Email&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Hello!&lt;\/h1&gt;\n&lt;p&gt;This is an &lt;strong&gt;HTML&lt;\/strong&gt; email sent with PHP.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n\";\n\n$headers = \"MIME-Version: 1.0\" . \"rn\";\n$headers .= \"Content-type:text\/html;charset=UTF-8\" . \"rn\";\n$headers .= \"From: hello@domain.com\" . \"rn\";\n\nmail($to, $subject, $message, $headers);\n?&gt;<\/pre><p class=\"wp-block-paragraph\">This allows you to format text, add headings, and style your message with HTML.<\/p><h3 id=\"h-how-can-i-validate-email-addresses-before-using-the-php-mail-function-or-phpmailer-to-send-emails\" class=\"wp-block-heading h-t-title-3\">Sending contact form messages with mail()<\/h3><p class=\"wp-block-paragraph\">A contact form sends visitor input to a PHP script, which validates it and passes it to mail(). Validation stops malformed addresses and blocks header injection, where an attacker adds line breaks to a field to insert extra recipients and turn your form into a spam relay.<\/p><p class=\"wp-block-paragraph\">Add this form to any page on your site:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;form action=\"contact.php\" method=\"post\"&gt;\n  &lt;input type=\"text\" name=\"name\" placeholder=\"Your name\" required&gt;\n  &lt;input type=\"email\" name=\"email\" placeholder=\"Your email\" required&gt;\n  &lt;textarea name=\"message\" placeholder=\"Your message\" required&gt;&lt;\/textarea&gt;\n  &lt;button type=\"submit\"&gt;Send&lt;\/button&gt;\n&lt;\/form&gt;<\/pre><p class=\"wp-block-paragraph\">Then create contact.php in the same directory:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nif ($_SERVER[\"REQUEST_METHOD\"] !== \"POST\") {\n    http_response_code(405);\n    exit(\"Method not allowed.\");\n}\n\n$name    = trim($_POST[\"name\"] ?? \"\");\n$email   = trim($_POST[\"email\"] ?? \"\");\n$message = trim($_POST[\"message\"] ?? \"\");\n\n\/\/ Reject empty fields, invalid addresses, and line breaks used for header injection\nif ($name === \"\" || $message === \"\" || !filter_var($email, FILTER_VALIDATE_EMAIL) || preg_match(\"\/[rn]\/\", $name)) {\n    exit(\"Please enter your name, a valid email address, and a message.\");\n}\n\n$to      = \"you@yourdomain.com\";\n$subject = \"New contact form message from \" . $name;\n$body    = \"Name: \" . $name . \"rn\" .\n           \"Email: \" . $email . \"rnrn\" .\n           wordwrap($message, 70, \"rn\");\n$headers = [\n    \"From\"         =&gt; \"no-reply@yourdomain.com\",\n    \"Reply-To\"     =&gt; $email,\n    \"Content-Type\" =&gt; \"text\/plain; charset=UTF-8\",\n];\n\nif (mail($to, $subject, $body, $headers)) {\n    echo \"Thanks! Your message has been sent.\";\n} else {\n    echo \"Sorry, your message couldn't be sent. Please try again later.\";\n}\n?&gt;<\/pre><p class=\"wp-block-paragraph\">The From header stays on your own domain, while Reply-To holds the visitor&#8217;s address, so you can answer with one click without failing sender checks. The filter_var() function with FILTER_VALIDATE_EMAIL rejects badly formatted addresses, and the header array format requires PHP 7.2 or later. For a public form, also add a CAPTCHA or a hidden honeypot field to reduce automated spam submissions.<\/p><h2 id=\"h-how-to-troubleshoot-common-php-mail-function-errors\" class=\"wp-block-heading h-t-title-2\">How to troubleshoot common PHP mail() function errors<\/h2><p class=\"wp-block-paragraph\">The mail() function can fail for several reasons. Here are the most common issues:<\/p><ul class=\"wp-block-list\">\n<li>Mail goes to the spam folder<\/li>\n\n\n\n<li>Call to undefined function mail() or failed to connect to mailserver<\/li>\n\n\n\n<li>Gmail couldn&rsquo;t verify that domain.tld sent this message<\/li>\n\n\n\n<li>Sender address rejected: not owned by the user<\/li>\n\n\n\n<li>Mail not delivered at all (silently failing)<\/li>\n\n\n\n<li>Incorrect headers or formatting<\/li>\n<\/ul><h3 id=\"h-mail-goes-to-the-spam-folder\" class=\"wp-block-heading h-t-title-3\">Mail goes to the spam folder<\/h3><p class=\"wp-block-paragraph\">Emails often end up in spam if headers are incomplete or if the content looks suspicious. To reduce the risk:<\/p><ul class=\"wp-block-list\">\n<li>Use a proper <strong>From<\/strong> address matching your domain.<\/li>\n\n\n\n<li>Avoid trigger words that spam filters flag.<\/li>\n\n\n\n<li>Set up DNS records like SPF and DKIM for domain verification.<\/li>\n<\/ul><p class=\"wp-block-paragraph\"><strong>Example of adding From and Reply-To headers:<\/strong><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$headers = \"From: no-reply@yourdomain.comrn\";\n$headers .= \"Reply-To: support@yourdomain.comrn\";\n$headers .= \"X-Mailer: PHP\/\" . phpversion();<\/pre><h3 id=\"h-could-not-connect-to-smtp-host\" class=\"wp-block-heading h-t-title-3\">Call to undefined function mail() or failed to connect to mailserver<\/h3><h3 class=\"wp-block-heading h-t-title-3\">Call to undefined function mail() or failed to connect to mailserver<\/h3><p class=\"wp-block-paragraph\">These errors mean PHP can&#8217;t hand the message to a mail server. Since PHP 8.0, a function listed in the disable_functions directive behaves as if it doesn&#8217;t exist, so a disabled mail() triggers a fatal &#8220;Call to undefined function mail()&#8221; error. On Windows and local development setups, a warning such as &#8220;Failed to connect to mailserver at &#8220;localhost&#8221; port 25&#8243; means no SMTP server is listening at the address set in php.ini.<\/p><p class=\"wp-block-paragraph\">To fix the problem:<\/p><ul class=\"wp-block-list\">\n<li>Run var_dump(function_exists(&#8220;mail&#8221;)); in a PHP file. A false result means mail() is disabled.<\/li>\n\n\n\n<li>On a server you manage, remove only mail from the disable_functions list in <a data-click-id=\"hgr-post-content-link-php-ini\" href=\"https:\/\/cseo-audit-app.aura.hostinger.systems\/tutorials\/what-is-php-ini\/\" target=\"_blank\" rel=\"noopener\">php.ini<\/a>, then restart PHP. On shared hosting, ask your provider, because the main php.ini is managed for you. Hostinger keeps mail() enabled on its hosting servers.<\/li>\n\n\n\n<li>On Windows or a local setup, point the SMTP and smtp_port directives to a reachable SMTP server, or set sendmail_path to a sendmail-compatible program.<\/li>\n\n\n\n<li>Check whether a firewall or your provider blocks outbound connections on port 25.<\/li>\n<\/ul><p class=\"wp-block-paragraph\">Example of removing only mail from the disabled functions list in php.ini:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Before: mail() is disabled\ndisable_functions = exec,shell_exec,system,mail\n\n; After: only mail is removed from the list\ndisable_functions = exec,shell_exec,system\n<\/pre><p class=\"wp-block-paragraph\">Never clear the whole disable_functions directive. An empty value turns back on every function the host disabled for security reasons, such as exec and shell_exec.<\/p><h3 id=\"h-gmail-couldn-t-verify-that-domain-tld-sent-this-message\" class=\"wp-block-heading h-t-title-3\">Gmail couldn&rsquo;t verify that domain.tld sent this message<\/h3><p class=\"wp-block-paragraph\">Gmail shows this warning when a message fails domain authentication. It&#8217;s common with mail() because the message leaves through the hosting server instead of your domain&#8217;s email service, so it may not carry your domain&#8217;s DKIM signature. To fix it, publish these TXT records in the DNS zone of the domain used in your From address:<\/p><ul class=\"wp-block-list\">\n<li>Add an <strong>SPF record<\/strong> to specify authorized mail servers.<\/li>\n\n\n\n<li>Enable <strong>DKIM<\/strong> to verify message integrity.<\/li>\n\n\n\n<li>Set up <strong>DMARC<\/strong> to protect against spoofing.<\/li>\n<\/ul><p class=\"wp-block-paragraph\"><strong>Example of an SPF record<\/strong> (use the include value your email or hosting provider gives you):<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">v=spf1 include:[provider SPF domain] ~all\n<\/pre><p class=\"wp-block-paragraph\"><strong>Example of a DKIM record<\/strong> (your email provider generates the key and gives you the public key value):<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Name: [selector]._domainkey.[domain.tld]\nContent: v=DKIM1; k=rsa; p=[public key]\n<\/pre><p class=\"wp-block-paragraph\"><strong>Example of a DMARC record:<\/strong><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Name: _dmarc.[domain.tld]\nContent: v=DMARC1; p=none; rua=mailto:[yourname]@[domain.tld]<\/pre><p class=\"wp-block-paragraph\">Our guide to <a data-click-id=\"hgr-post-content-link-spf-dkim-and-dmarc-records\" href=\"\/ph\/tutorials\/spf-dkim-dmarc-guide\/\">SPF, DKIM, and DMARC records<\/a> explains how receiving servers check each record. If Gmail still shows the warning after the records are in place, send through authenticated SMTP instead, so messages leave from your domain&#8217;s mail server.<\/p><h3 id=\"h-sender-address-rejected-not-owned-by-the-user\" class=\"wp-block-heading h-t-title-3\">Sender address rejected: not owned by the user<\/h3><p class=\"wp-block-paragraph\">This happens when the sender email does not belong to your domain. To resolve it:<\/p><ul class=\"wp-block-list\">\n<li>Always use a valid sender address, like <strong>no-reply@yourdomain.com<\/strong>.<\/li>\n\n\n\n<li>Avoid using third-party addresses such as Gmail or Yahoo in the From field.<\/li>\n<\/ul><p class=\"wp-block-paragraph\"><strong>Example of a correct sender address:<\/strong><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$headers = \"From: no-reply@yourdomain.comrn\";<\/pre><h3 id=\"h-mail-not-delivered-at-all-silently-failing\" class=\"wp-block-heading h-t-title-3\">Mail not delivered at all (silently failing)<\/h3><p class=\"wp-block-paragraph\">Sometimes the mail() function does not throw an error but messages never arrive. Common reasons include:<\/p><ul class=\"wp-block-list\">\n<li>The hosting provider blocks PHP mail for security reasons.<\/li>\n\n\n\n<li>The server is misconfigured.<\/li>\n\n\n\n<li>Logs show messages being dropped.<\/li>\n<\/ul><p class=\"wp-block-paragraph\"><strong>Example of logging mail() results in PHP<\/strong><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (mail($to, $subject, $message, $headers)) {\n    error_log(\"Mail sent successfully to $to\");\n} else {\n    error_log(\"Mail failed to send to $to\");\n}<\/pre><h3 id=\"h-incorrect-headers-or-formatting\" class=\"wp-block-heading h-t-title-3\">Incorrect headers or formatting<\/h3><p class=\"wp-block-paragraph\">Improper headers cause delivery failures or corrupted emails. Follow these practices:<\/p><ul class=\"wp-block-list\">\n<li>Always use rn for line breaks in headers.<\/li>\n\n\n\n<li>Include <strong>MIME-Version<\/strong> and <strong>Content-Type<\/strong> when sending HTML emails.<\/li>\n\n\n\n<li>Keep formatting consistent to avoid parsing errors.<\/li>\n<\/ul><p class=\"wp-block-paragraph\"><strong>Example of correct HTML headers:<\/strong><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$headers = \"MIME-Version: 1.0rn\";\n$headers .= \"Content-type: text\/html; charset=UTF-8rn\";<\/pre><h2 id=\"h-when-should-you-switch-from-mail-to-phpmailer\" class=\"wp-block-heading h-t-title-2\">When should you switch from mail() to PHPMailer?<\/h2><p class=\"wp-block-paragraph\">Switch from mail() to PHPMailer or another SMTP-based library when you need reliable inbox delivery, attachments, SMTP authentication, or regular sending volume. The mail() function remains a good fit for learning, testing, and occasional notifications on a server with a working mail system.<\/p><figure tabindex=\"0\" class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>PHP mail()<\/th><th>PHPMailer with SMTP<\/th><\/tr><\/thead><tbody><tr><td>Setup<\/td><td>Built into PHP<\/td><td>Installed with Composer or manually<\/td><\/tr><tr><td>Authentication<\/td><td>None; relies on the server&#8217;s mail system<\/td><td>SMTP username and password<\/td><\/tr><tr><td>HTML email<\/td><td>Manual MIME-Version and Content-Type headers<\/td><td>Built-in isHTML() method<\/td><\/tr><tr><td>Attachments<\/td><td>Manual MIME encoding<\/td><td>Built-in addAttachment() method<\/td><\/tr><tr><td>Error reporting<\/td><td>Returns only true or false<\/td><td>Detailed error messages and SMTP debug output<\/td><\/tr><tr><td>Deliverability<\/td><td>More likely to be filtered as spam<\/td><td>Usually better, because messages come from an authenticated mailbox<\/td><\/tr><tr><td>Best for<\/td><td>Tests and low-volume notifications<\/td><td>Contact forms, transactional email, and production sites<\/td><\/tr><\/tbody><\/table><\/figure><p class=\"wp-block-paragraph\"><strong>Verdict:<\/strong> use mail() to check that your server can send email or to power a low-traffic form, and move to PHPMailer with SMTP as soon as delivery matters. On Hostinger web hosting, emails sent with mail() and other unauthenticated methods are limited to 10 per minute and 100 per day, and Hostinger recommends SMTP for regular sending.<\/p><p class=\"wp-block-paragraph\">To set up authenticated sending, follow our tutorial on <a data-click-id=\"hgr-post-content-link-how-to-send-emails-using-phpmailer\" href=\"\/ph\/tutorials\/how-to-send-emails-using-phpmailer\/\">how to send emails using PHPMailer<\/a>, which covers installation, SMTP settings, and attachments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PHP mail() function sends an email directly from a PHP script by passing the message to the server&#8217;s mail system, with no external library to install. You call mail() with a recipient, a subject, and a message, then add headers for the sender, reply address, copies, or HTML content. This guide covers the mail() [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ph\/tutorials\/how-to-send-emails-using-php-mail-function\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":357,"featured_media":127862,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to send emails with PHP mail(): examples and fixes","rank_math_description":"Learn to send emails with the PHP mail() function using code examples for headers, HTML, and contact forms, plus fixes for spam and delivery errors.","rank_math_focus_keyword":"php mail function, php mail() function","_content_updated_date":"","footnotes":""},"categories":[22658],"tags":[],"class_list":["post-124860","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-email"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-enviar-emails-usando-php\/","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/php-mail\/","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/enviar-emails-usando-php-mail\/","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-send-emails-using-php-mail-function\/","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/enviar-emails-usando-php-mail\/","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/enviar-emails-usando-php-mail\/","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/enviar-emails-usando-php-mail\/","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-enviar-emails-usando-php\/","default":0},{"locale":"de-DE","link":"https:\/\/www.hostinger.com\/de\/tutorials\/php-mail-funktion","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts\/124860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/users\/357"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/comments?post=124860"}],"version-history":[{"count":2,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts\/124860\/revisions"}],"predecessor-version":[{"id":138213,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts\/124860\/revisions\/138213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/media\/127862"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/media?parent=124860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/categories?post=124860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/tags?post=124860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}