{"id":124840,"date":"2025-11-27T13:14:17","date_gmt":"2025-11-27T13:14:17","guid":{"rendered":"\/my\/tutorials\/how-to-send-emails-using-php-mail-function\/"},"modified":"2026-03-10T10:04:25","modified_gmt":"2026-03-10T10:04:25","slug":"how-to-send-emails-using-php-mail-function","status":"publish","type":"post","link":"\/my\/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>The <strong>PHP mail() function<\/strong> is a built-in way to send emails directly from a PHP script.<\/p><p>Developers often use it for simple tasks like sending contact form messages or testing email functionality.<\/p><p>While it is easy to use, the mail() function has limitations that make it better suited for learning and small projects than for production environments.<\/p><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-the-php-mail-function\">What is the PHP mail() function?<\/h2><p>The PHP mail() function lets your website send emails without needing an external library. It works by calling the <a href=\"\/my\/tutorials\/what-is-a-web-server\">web server&rsquo;s<\/a> mail transfer agent (MTA) to deliver messages.;<\/p><p>The function takes in at least three parameters: the recipient&rsquo;s email, the subject line, and the message body. You can also include optional headers like <strong>From<\/strong>, <strong>Reply-To<\/strong>, and content type.<\/p><p>This makes mail() one of the quickest ways to test sending emails in <a href=\"\/my\/tutorials\/what-is-php\/\">PHP<\/a>. However, it doesn&rsquo;t offer modern features like authentication, attachments, or reliable spam protection.<\/p><h3 class=\"wp-block-heading\" id=\"h-phpmailer-vs-mail-function-pros-and-cons\">PHPMailer vs mail() function: pros and cons<\/h3><p>While mail() is simple, it lacks flexibility. <strong>PHPMailer<\/strong>, on the other hand, is a popular library that provides advanced email features like SMTP support, authentication, attachments, and HTML formatting.<\/p><p><strong>Pros of mail():<\/strong><\/p><ul class=\"wp-block-list\">\n<li>Built into PHP<\/li>\n\n\n\n<li>Easy to use<\/li>\n\n\n\n<li>No installation required<\/li>\n<\/ul><p><strong>Cons of mail():<\/strong><\/p><ul class=\"wp-block-list\">\n<li>Limited functionality<\/li>\n\n\n\n<li>Higher chance of being flagged as spam<\/li>\n\n\n\n<li>Harder to debug errors<\/li>\n<\/ul><p>If you need advanced features, consider using PHPMailer. We&rsquo;ve covered it in detail in our guide on <a href=\"\/my\/tutorials\/how-to-send-emails-using-phpmailer\">how to send emails using PHPMailer<\/a>.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-send-emails-using-the-php-mail-function\">How to send emails using the PHP mail() function<\/h2><p>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 class=\"wp-block-heading\" id=\"h-understanding-php-mail-components\">Understanding PHP mail components<\/h3><p>To send an email successfully, you need to set up correct headers:<\/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           \"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.<\/li>\n\n\n\n<li><strong>Reply-To<\/strong> &ndash; defines where replies should be directed.<\/li>\n\n\n\n<li><strong>X-Mailer<\/strong> &ndash; identifies that the message was sent by PHP.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-creating-a-test-file-for-php-mail\">Creating a test file for PHP mail<\/h3><p>To test mail(), create a <strong>testmail.php<\/strong> file in your server root:<\/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 sent successfully.\";\n} else {\n    echo \"Email delivery failed.\";\n}\n?&gt;<\/pre><p>Upload this file and access it in your browser. If configured correctly, you should receive an email.<\/p><h3 class=\"wp-block-heading\" id=\"h-sending-html-emails-with-php\">Sending HTML emails with PHP<\/h3><p>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>This allows you to format text, add headings, and style your message with HTML.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-can-i-validate-email-addresses-before-using-the-php-mail-function-or-phpmailer-to-send-emails\">How can I validate email addresses before using the PHP mail() function or PHPMailer to send emails?<\/h3><p>Validating email addresses helps prevent errors and reduce spam issues. PHP provides <strong>filter_var()<\/strong> for this:<\/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$email = \"user@example.com\";\n\nif (filter_var($email, FILTER_VALIDATE_EMAIL)) {\n    echo \"Valid email address.\";\n} else {\n    echo \"Invalid email address.\";\n}\n?&gt;<\/pre><p>This ensures you only send emails to properly formatted addresses.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-troubleshoot-common-php-mail-function-errors\">How to troubleshoot common PHP mail() function errors<\/h2><p>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>Could not connect to SMTP host<\/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 class=\"wp-block-heading\" id=\"h-mail-goes-to-the-spam-folder\">Mail goes to the spam folder<\/h3><p>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><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 class=\"wp-block-heading\" id=\"h-could-not-connect-to-smtp-host\">Could not connect to SMTP host<\/h3><p>This error indicates a server configuration problem. Possible causes include:<\/p><ul class=\"wp-block-list\">\n<li>The hosting provider blocks outbound mail.<\/li>\n\n\n\n<li>PHP mail is disabled in <a href=\"\/my\/tutorials\/what-is-php-ini\"><strong>php.ini<\/strong><\/a>.<\/li>\n\n\n\n<li>Firewall rules are preventing the connection.<\/li>\n<\/ul><p><strong>Example of checking mail function in php.ini<\/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=\"\">; Make sure this is not disabled\ndisable_functions =<\/pre><h3 class=\"wp-block-heading\" id=\"h-gmail-couldn-t-verify-that-domain-tld-sent-this-message\">Gmail couldn&rsquo;t verify that domain.tld sent this message<\/h3><p>Gmail and other providers require domain authentication to trust your messages. To fix this, ensure you have the following TXT entries in your DNS records:<\/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><strong>Example of an SPF 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=\"\">v=spf1 include:_spf.[domain.tld] ~all<\/pre><p><strong>Example of a DKIM 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: [selector]._domainkey.[domain.tld]\nContent: v=DKIM1; p=[series of numbers]<\/pre><p><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><h3 class=\"wp-block-heading\" id=\"h-sender-address-rejected-not-owned-by-the-user\">Sender address rejected: not owned by the user<\/h3><p>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><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 class=\"wp-block-heading\" id=\"h-mail-not-delivered-at-all-silently-failing\">Mail not delivered at all (silently failing)<\/h3><p>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><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 class=\"wp-block-heading\" id=\"h-incorrect-headers-or-formatting\">Incorrect headers or formatting<\/h3><p>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><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 class=\"wp-block-heading\" id=\"h-when-to-move-beyond-the-mail-function\">When to move beyond the mail() function<\/h2><p>The PHP mail() function is useful for learning, debugging, and sending simple test messages. However, it is not the best choice for production use. If you need reliable delivery, attachments, or SMTP authentication, move to a more powerful library like <strong>PHPMailer<\/strong> or connect to an external email service.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PHP mail() function is a built-in way to send emails directly from a PHP script. Developers often use it for simple tasks like sending contact form messages or testing email functionality. While it is easy to use, the mail() function has limitations that make it better suited for learning and small projects than for [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/my\/tutorials\/how-to-send-emails-using-php-mail-function\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":357,"featured_media":128041,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to send emails with the PHP mail() function","rank_math_description":"Learn how to send emails from your web server with PHP using the built-in PHP mail() function in this guide.","rank_math_focus_keyword":"php mail function, php mail() function","footnotes":""},"categories":[22631],"tags":[],"class_list":["post-124840","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":"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-UK","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}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/124840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/users\/357"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/comments?post=124840"}],"version-history":[{"count":1,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/124840\/revisions"}],"predecessor-version":[{"id":128040,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/124840\/revisions\/128040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media\/128041"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media?parent=124840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/categories?post=124840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/tags?post=124840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}