{"id":345,"date":"2025-06-17T07:51:06","date_gmt":"2025-06-17T02:21:06","guid":{"rendered":"https:\/\/griffso.com\/blogs\/?p=345"},"modified":"2025-06-17T07:51:18","modified_gmt":"2025-06-17T02:21:18","slug":"fix-php-mail-function-not-working-in-xampp-windows-macos-linux-ubuntu","status":"publish","type":"post","link":"https:\/\/griffso.com\/blogs\/fix-php-mail-function-not-working-in-xampp-windows-macos-linux-ubuntu\/","title":{"rendered":"Fix PHP mail() Function Not Working in XAMPP (Windows, macOS, Linux\/Ubuntu)"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Fix PHP <code>mail()<\/code> Function Not Working in XAMPP (Windows, macOS, Linux\/Ubuntu)<\/strong><\/h2>\n\n\n\n<p>Is the PHP mail function not working in XAMPP? Learn how to send emails using <code>mail()<\/code> by configuring <code>sendmail<\/code> or external SMTP on Windows, macOS, and Linux\/Ubuntu.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde9 <strong>Introduction<\/strong><\/h3>\n\n\n\n<p>By default, the <code>mail()<\/code> function in PHP doesn\u2019t work out of the box in XAMPP because it&#8217;s not connected to any mail transfer agent (MTA). This issue is especially noticeable on <strong>Windows<\/strong>, where no built-in mail server exists.<\/p>\n\n\n\n<p>In this blog, we\u2019ll configure mail functionality using <strong>sendmail<\/strong> and <strong>SMTP<\/strong> across <strong>Windows<\/strong>, <strong>macOS<\/strong>, and <strong>Linux\/Ubuntu<\/strong> environments.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2757 Why <code>mail()<\/code> Fails<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sendmail<\/code> not configured<\/li>\n\n\n\n<li>SMTP settings missing or incorrect<\/li>\n\n\n\n<li>Firewall blocking mail ports (587\/465\/25)<\/li>\n\n\n\n<li>Antivirus\/email clients blocking outgoing mail<\/li>\n\n\n\n<li>No MTA on local environment<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udda5\ufe0f <strong>Fix on Windows (Using SMTP via sendmail)<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">\u2705 Step-by-Step:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Locate <code>php.ini<\/code>:<\/strong>\n<ul class=\"wp-block-list\">\n<li>XAMPP Control Panel > Apache > Config > <code>php.ini<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Enable mail logging (optional but helpful):<\/strong> <code>log_errors = On error_log = \"C:\\xampp\\php\\logs\\php_error_log\"<\/code><\/li>\n\n\n\n<li><strong>Edit <code>php.ini<\/code>:<\/strong><br>Find and edit: <code>[mail function] SMTP=smtp.gmail.com smtp_port=587 sendmail_from = your_email@gmail.com sendmail_path = \"\\\"C:\\xampp\\sendmail\\sendmail.exe\\\" -t\"<\/code><\/li>\n\n\n\n<li><strong>Edit <code>sendmail.ini<\/code>:<\/strong><br>Open: <code>C:\\xampp\\sendmail\\sendmail.ini<\/code> Replace content with: <code>smtp_server=smtp.gmail.com smtp_port=587 smtp_ssl=auto auth_username=your_email@gmail.com auth_password=your_app_password<\/code> \u26a0\ufe0f <strong>Note:<\/strong> Use an <a href=\"https:\/\/myaccount.google.com\/apppasswords\" target=\"_blank\" rel=\"noopener\">App Password<\/a> if 2FA is enabled.<\/li>\n\n\n\n<li><strong>Restart Apache.<\/strong><\/li>\n\n\n\n<li><strong>Test PHP Script:<\/strong> <code>&lt;?php if(mail(\"to@example.com\", \"Test Subject\", \"Test Body\", \"From: your_email@gmail.com\")) { echo \"Mail sent successfully!\"; } else { echo \"Mail sending failed!\"; } ?><\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf4f <strong>Fix on macOS<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install Sendmail (optional):<\/strong><br>On macOS, use <strong>postfix<\/strong> or tools like <code>msmtp<\/code>.<\/li>\n\n\n\n<li><strong>Use PHP + SMTP:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Install <code>msmtp<\/code>: <code>brew install msmtp<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Configure PHP to use SMTP:<\/strong><br>In your <code>php.ini<\/code> (<code>\/Applications\/XAMPP\/xamppfiles\/etc\/php.ini<\/code>): <code>sendmail_path = \"\/usr\/local\/bin\/msmtp -t\"<\/code><\/li>\n\n\n\n<li><strong>Configure <code>~\/.msmtprc<\/code>:<\/strong> <code>defaults auth on tls on tls_trust_file \/etc\/ssl\/certs\/ca-certificates.crt account gmail host smtp.gmail.com port 587 from your_email@gmail.com user your_email@gmail.com password your_app_password account default : gmail<\/code><\/li>\n\n\n\n<li><strong>Set permission:<\/strong> <code>chmod 600 ~\/.msmtprc<\/code><\/li>\n\n\n\n<li><strong>Restart Apache and test mail.<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udc27 <strong>Fix on Linux\/Ubuntu<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install <code>msmtp<\/code>:<\/strong> <code>sudo apt update &amp;&amp; sudo apt install msmtp<\/code><\/li>\n\n\n\n<li><strong>Configure PHP:<\/strong><br>In <code>\/opt\/lampp\/etc\/php.ini<\/code>, add: <code>sendmail_path = \"\/usr\/bin\/msmtp -t\"<\/code><\/li>\n\n\n\n<li><strong>Configure msmtp (user-level):<\/strong> <code># ~\/.msmtprc defaults auth on tls on tls_trust_file \/etc\/ssl\/certs\/ca-certificates.crt account gmail host smtp.gmail.com port 587 from your_email@gmail.com user your_email@gmail.com password your_app_password account default : gmail<\/code><\/li>\n\n\n\n<li><strong>Permission Fix:<\/strong> <code>chmod 600 ~\/.msmtprc<\/code><\/li>\n\n\n\n<li><strong>Restart Apache:<\/strong> <code>sudo \/opt\/lampp\/lampp restartapache<\/code><\/li>\n\n\n\n<li><strong>Test PHP mail code<\/strong> as mentioned above.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 Alternative: Use PHPMailer for Reliability<\/h3>\n\n\n\n<p>Using libraries like <strong>PHPMailer<\/strong> is recommended for production as it handles SMTP authentication, attachments, and error reporting more effectively.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc Conclusion<\/h3>\n\n\n\n<p>PHP <code>mail()<\/code> not working in XAMPP is a configuration issue, not a code issue. Whether you&#8217;re on Windows, macOS, or Linux, following these steps will allow your local server to send emails using Gmail SMTP or other providers.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcac Facing Errors?<\/h3>\n\n\n\n<p>Drop your configuration and error message in the comments, and I\u2019ll help you troubleshoot it personally.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n<div class=\"pld-like-dislike-wrap pld-template-1\">\n    <div class=\"pld-like-wrap  pld-common-wrap\">\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"345\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\n                        <i class=\"fas fa-thumbs-up\"><\/i>\n                <\/a>\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"345\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\n                        <i class=\"fas fa-thumbs-down\"><\/i>\n                <\/a>\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Fix PHP mail() Function Not Working in XAMPP (Windows, macOS, Linux\/Ubuntu) Is the PHP mail function not working in XAMPP? Learn how to send emails using mail() by configuring sendmail or external SMTP on Windows, macOS, and Linux\/Ubuntu. \ud83e\udde9 Introduction By default, the mail() function in PHP doesn\u2019t work out of the box in XAMPP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-coding-help-programming-tips-code-examples-coding-resources-debug-guide-software-development-learn-to-code-coding-best-practices-coding-challenges-coding-tutorials"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":1,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/345\/revisions\/346"}],"wp:attachment":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}