{"id":685,"date":"2023-05-15T06:12:00","date_gmt":"2023-05-15T06:12:00","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/uncategorized\/websitehow-to-send-emails-using-php\/"},"modified":"2025-04-08T00:07:33","modified_gmt":"2025-04-08T00:07:33","slug":"send-emails-using-php-mail","status":"publish","type":"post","link":"\/uk\/tutorials\/send-emails-using-php-mail","title":{"rendered":"How to send emails from your web server with the PHP mail() function and PHPMailer"},"content":{"rendered":"<p>Most businesses create a professional email account to boost their credibility and build customer trust. To accomplish this, you need to buy a domain and set up your business email on an email client or a server.<\/p><p>If you build your website or web application with the PHP programming language, you can send emails directly from your web server. The PHP mail functionality enables you to create custom mail forms and send basic text-based emails to multiple recipients.<\/p><p>There are two ways to send emails in PHP: using the built-in <strong>PHP mail() function<\/strong> or a mail-sending library such as <strong>PHPMailer<\/strong>.<\/p><p>In this article, we&rsquo;ll cover the differences between PHPMailer and the <strong>mail()<\/strong> function and show you how to send emails with both.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/Web-Development-Glossary-for-Beginners.pdf\" target=\"_blank\" rel=\"noopener\">Download glossary for web beginners<\/a><\/p><p>\n\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What is PHP mail?<\/h2>\n                    <p>PHP mail is a function that sends emails using PHP scripts. The built-in PHP function can target multiple recipients per email sending. However, it isn&rsquo;t suitable for bulk emailing without using an external PHP mailing package like PHPMailer.<br>\n<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-phpmailer-vs-mail-function-pros-and-cons\"><strong>PHPMailer vs mail() function: pros and cons<\/strong><\/h2><p>PHP mail lets PHP-based website administrators send mail from their <a href=\"\/uk\/tutorials\/what-is-a-web-server\">web server<\/a> using PHP scripts. It&rsquo;s a popular alternative to getting a third-party email hosting service, as the PHP function is integrated with the web server.<\/p><p>Sending emails in <a href=\"\/uk\/tutorials\/what-is-php\/\">PHP<\/a> is possible via the native PHP <strong>mail()<\/strong> function or an external PHP mailing package like PHPMailer. Here&rsquo;s a brief coverage of each method, including their advantages and disadvantages:<\/p><p><strong>PHP mail() function<\/strong><\/p><p><strong>mail(<\/strong>) is a PHP function that uses PHP scripts to send simple emails. The mail function includes three mandatory parameters &#8210; <strong>$to<\/strong>, <strong>$subject<\/strong>, and<strong> $message<\/strong>. Optional parameters to utilize include <strong>$headers<\/strong> and <strong>$parameters<\/strong>. We&rsquo;ll cover them in more detail later.<\/p><p>This built-in PHP function returns a <strong>boolean<\/strong> value upon execution &#8210; <strong>TRUE<\/strong> if the server successfully receives the email for sending or <strong>FALSE<\/strong> on failure.<\/p><p><strong>Advantages<\/strong>:<\/p><ul class=\"wp-block-list\">\n<li>Pre-installed and ready to use, all you need is to have PHP.<\/li>\n\n\n\n<li>Backward-compatible, a <a href=\"\/uk\/tutorials\/how-to-change-your-php-version\">PHP version change<\/a> won&rsquo;t break the script.<\/li>\n\n\n\n<li>Easy to learn.<\/li>\n<\/ul><p><strong>Disadvantages<\/strong>:<\/p><ul class=\"wp-block-list\">\n<li>Sends emails using an outdated method without built-in support for modern authentication like SPF and DKIM.<\/li>\n\n\n\n<li>Often lands in spam or junk folders due to missing authentication and improper configuration.<\/li>\n\n\n\n<li>Requires complex setup for SMTP, increasing the chance of triggering spam filters.<\/li>\n\n\n\n<li>Cannot handle large volumes of emails effectively.<\/li>\n\n\n\n<li>Offers lower sending limits and less reliability compared to PHPMailer.<\/li>\n\n\n\n<li>Returns <strong>TRUE<\/strong> even if the email fails to reach the recipient.<\/li>\n<\/ul><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> For Hostinger users, keep in mind that the current limits for emails sent via PHP mail() are <strong>100\/day<\/strong> and <strong>10\/minute<\/strong>. These limits will reset every 24 hours and will be consumed immediately if there are queued emails.<\/p><\/div>\n\n\n\n<\/p><p><strong>PHPMailer<\/strong><\/p><p>PHPMailer is a popular mail-sending library that supports sending mail via the <strong>mail()<\/strong> function or a <a href=\"\/uk\/tutorials\/smtp-port\">Simple Mail Transfer Protocol (SMTP)<\/a> server. It gives access to a set of functions for sending mail, simplifying the manual PHP mail configuration process.<\/p><p><strong>Advantages<\/strong>:<\/p><ul class=\"wp-block-list\">\n<li>Sends emails over a secure connection with SPF and DKIM authentication, improving inbox delivery.<\/li>\n\n\n\n<li>Simplifies email configuration and supports advanced features like HTML bodies and attachments.<\/li>\n\n\n\n<li>Supports SMTP with integrated <a href=\"\/uk\/tutorials\/what-is-ssl\">SSL<\/a> and <a href=\"\/uk\/tutorials\/what-is-tls\">TLS<\/a> encryption.<\/li>\n\n\n\n<li>Handles large volumes of email efficiently in a short period.<\/li>\n<\/ul><p><strong>Disadvantages<\/strong>:<\/p><ul class=\"wp-block-list\">\n<li>Requires manual installation through SSH.<\/li>\n\n\n\n<li>Incurs additional costs on Hostinger&rsquo;s <strong>Single<\/strong> web hosting plan due to the lack of SSH access.<\/li>\n\n\n\n<li>Has a steeper learning curve compared to the built-in <strong>mail()<\/strong> function.<\/li>\n<\/ul><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> If you use PHPMailer with <a href=\"https:\/\/support.hostinger.com\/en\/articles\/5326155-parameters-and-limits-of-titan-email\" target=\"_blank\" rel=\"noopener\">Titan Mail on Hostinger, the sending limit is<\/a> <strong>1000 emails\/day<\/strong> and <strong>300 emails\/hour<\/strong>. For <a href=\"https:\/\/support.hostinger.com\/en\/articles\/4625828-parameters-and-limits-of-hostinger-email\" target=\"_blank\" rel=\"noopener\"> Hostinger&rsquo;s email service, the limit is higher<\/a> &ndash; up to <strong>3000 emails\/day<\/strong> with no hourly limit. Check the parameters and limits for each service, as they vary depending on your hosting plan.<\/p><\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-phpmailer-to-send-emails\"><strong>How to use PHPMailer to send emails<\/strong><\/h2><p>In this section, we&rsquo;ll cover the steps for sending mail using PHPMailer. We recommend using an authenticated SMTP connection with PHPMailer to increase the deliverability of your emails against spam filters.<\/p><h3 class=\"wp-block-heading\" id=\"h-installing-phpmailer\"><strong>Installing PHPMailer<\/strong><\/h3><p>Installing PHPMailer is quite simple, especially when using <a href=\"\/uk\/tutorials\/how-to-install-composer\">Composer<\/a>.<\/p><p>Note that <a href=\"\/uk\/web-hosting\">Hostinger&rsquo;s Premium and Business web hosting plans<\/a> and <a href=\"\/uk\/cloud-hosting\">cloud hosting options<\/a> come pre-installed with two versions of Composer.<\/p><p>Use the <strong>composer<\/strong> command to activate Composer version <strong>1.10<\/strong>. If you need the new <strong>2.0<\/strong> version or use PHP version <strong>8.0<\/strong> or later, execute the <strong>composer2<\/strong> command.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-full\"><a href=\"\/uk\/web-hosting\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\" alt=\"Hostinger web hosting banner\" class=\"wp-image-98604\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/11\/Web-hosting_in-text-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/11\/Web-hosting_in-text-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/11\/Web-hosting_in-text-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/11\/Web-hosting_in-text-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>\n\n\n<div><p class=\"important\"> You must install PHPMailer using Composer through SSH for it to work correctly. Adding a <strong>phpmailer.php<\/strong> file with SMTP code, as we&rsquo;ll cover later in this guide, won&rsquo;t work without proper installation.<\/p><\/div>\n\n\n\n<\/p><p>Follow these steps to install PHPMailer manually:<\/p><ol class=\"wp-block-list\">\n<li>From your <a href=\"\/uk\/tutorials\/hpanel-tutorial\">hPanel dashboard<\/a>, go to <strong>Advanced &rarr; SSH Access<\/strong> and take note of the <strong>SSH IP<\/strong>, <strong>port<\/strong>, <strong>username<\/strong>, and <strong>password<\/strong> under the <strong>SSH details<\/strong>.<\/li>\n\n\n\n<li>Open PuTTY and enter your SSH information in the <strong>Host Name (or IP address)<\/strong> and <strong>Port fields<\/strong>. Then, click <strong>Open<\/strong>.<\/li>\n\n\n\n<li>Once a command window appears, type in your SSH username and password and hit <strong>Enter<\/strong>. Remember that PuTTY will not display the password, so don&rsquo;t be surprised if it doesn&rsquo;t appear on the screen.<\/li>\n\n\n\n<li>Execute the following command to navigate to the <strong>public_html <\/strong>directory:<\/li>\n<\/ol><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=\"\">cd public_html<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Run the following command to install PHPMailer:<\/li>\n<\/ol><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=\"\">composer2 require phpmailer\/phpmailer<\/pre><ol start=\"6\" class=\"wp-block-list\">\n<li>Wait a moment until the installation process is finished. Here&rsquo;s what it should look like:<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e2224a97141\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"733\" height=\"335\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/Finished-Composer-installation-process.png\" alt=\"Finished Composer installation process\" class=\"wp-image-85515\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/05\/Finished-Composer-installation-process.png 733w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/05\/Finished-Composer-installation-process-300x137.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/05\/Finished-Composer-installation-process-150x69.png 150w\" sizes=\"(max-width: 733px) 100vw, 733px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Suggested reading<\/h4>\n                    <p>Learn more about <a href=\"\/uk\/tutorials\/ssh\/basic-ssh-commands\">SSH commands<\/a> to help manage your server.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-understanding-phpmailer-components\"><strong>Understanding PHPMailer components<\/strong><\/h3><p>To understand how PHPMailer works, let&rsquo;s review each script component above.<\/p><ul class=\"wp-block-list\">\n<li><strong>use PHPMailer\\PHPMailer\\PHPMailer;<\/strong> &ndash; imports the PHPMailer class to the <a href=\"https:\/\/www.php.net\/manual\/en\/language.namespaces.php\" target=\"_blank\" rel=\"noopener\">global namespace<\/a>.<\/li>\n\n\n\n<li><strong>require &lsquo;..\/vendor\/autoload.php&rsquo;;<\/strong> &ndash; includes various libraries that PHPMailer needs.<\/li>\n\n\n\n<li><strong>$mail = new PHPMailer;<\/strong> &ndash; creates a new PHPMailer object.<\/li>\n\n\n\n<li><strong>$mail-&gt;isSMTP(); <\/strong>&ndash; tells the PHPMailer class to use the custom SMTP configuration defined in the script instead of the local mail server.<\/li>\n\n\n\n<li><strong>$mail-&gt;SMTPDebug = 2;<\/strong> &ndash; detects if something goes wrong with the SMTP connection.<\/li>\n\n\n\n<li><strong>$mail-&gt;Host = &lsquo;smtp.hostinger.com&rsquo;;<\/strong> &ndash; this is where the SMTP server address should be specified.<\/li>\n\n\n\n<li><strong>$mail-&gt;Port = 587;<\/strong> &ndash; set the SMTP port here. We&rsquo;ll pick <a href=\"\/uk\/tutorials\/smtp-port-587\">SMTP port 587<\/a> as the default mail submission port for all types of SMTP data transmission.<\/li>\n\n\n\n<li><strong>$mail-&gt;SMTPAuth = true;<\/strong> &ndash; activates SMTP authentication.<\/li>\n\n\n\n<li><strong>$mail-&gt;Username = &lsquo;mymail@myawesomedomain.tld&rsquo;;<\/strong> &ndash; specify your email address here.<\/li>\n\n\n\n<li><strong>$mail-&gt;Password = &lsquo;My$tr0ngPa55w0rd!;<\/strong> &ndash; enter your email password here.<\/li>\n\n\n\n<li><strong>$mail-&gt;setFrom(&lsquo;mymail@myawesomedomain.tld&rsquo;, &lsquo;Your Name&rsquo;); <\/strong>&ndash; this is where you insert the sender&rsquo;s email address.<\/li>\n\n\n\n<li><strong>$mail-&gt;addReplyTo(&lsquo;mymail@myawesomedomain.tld&rsquo;, &lsquo;Your Name&rsquo;);<\/strong> &ndash; informs the recipient which address they should reply to.<\/li>\n\n\n\n<li><strong>$mail-&gt;addAddress(&lsquo;recipient@domain.tld&rsquo;, &lsquo;Receiver Name&rsquo;);<\/strong> &ndash; insert the recipient&rsquo;s address here.<\/li>\n\n\n\n<li><strong>$mail-&gt;Subject = &lsquo;Checking if PHPMailer works&rsquo;;<\/strong> &ndash; add the email subject here.<\/li>\n\n\n\n<li><strong>$mail-&gt;msgHTML(file_get_contents(&lsquo;message.html&rsquo;), __DIR__);<\/strong> &ndash; reads an HTML message body from an external file. The <strong>file_get_contents()<\/strong> function will load the content from <strong>message.html<\/strong>, a local file located in the <strong>public_html <\/strong>directory, and include it in the message.<\/li>\n\n\n\n<li><strong>$mail-&gt;Body = &lsquo;This is just a plain text message body&rsquo;;<\/strong> &ndash; contains the mail message body.<\/li>\n\n\n\n<li><strong>\/\/$mail-&gt;addAttachment(&lsquo;attachment.txt&rsquo;);<\/strong> &ndash; if you want to include attachments, add the file names and remove the double slash from this statement.<\/li>\n\n\n\n<li><strong>if (!$mail-&gt;send()) {<\/strong> &ndash; defines what happens when the script is executed.<\/li>\n\n\n\n<li><strong>echo &lsquo;Mailer Error:&rsquo;. $mail-&gt;ErrorInfo;<\/strong> &ndash; you will see an error message and details of the error if the script fails to send.<\/li>\n\n\n\n<li><strong>} else { <\/strong>&ndash;<strong> <\/strong>extends the <strong>if<\/strong> statement and describes what happens if the previous condition is unmet.<\/li>\n\n\n\n<li><strong>echo &lsquo;The email message was sent!&rsquo;;<\/strong> &ndash; means the email sending process is successful.<\/li>\n<\/ul><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro tip<\/h4>\n                    <p>The <strong>SMTPDebug = 2;<\/strong> line is only applicable when you test a script and want to see how it operates. Remember to change it to <strong>SMTPDebug = 0;<\/strong> when you are done with the test to prevent receivers from catching the SMTP protocol delivery report.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-using-phpmailer-with-hostinger-smtp\"><strong>Using PHPMailer with Hostinger SMTP<\/strong><\/h3><p>After installing PHPMailer, you can start sending emails in PHP. This section will show you how to send emails through the Hostinger SMTP server using PHPMailer.<\/p><p>To do so, follow the steps below:<\/p><ol class=\"wp-block-list\">\n<li><a href=\"\/uk\/tutorials\/how-to-create-an-email\">Create an email account<\/a> by accessing <strong>hPanel<\/strong>, then go to <strong>Emails &rarr; Email Accounts &rarr; Create email account<\/strong>.<\/li>\n\n\n\n<li>Fill in the new email address and set a password. Then, click <strong>Create<\/strong>. Remember this information as you will use it to send mail via PHPMailer.<\/li>\n\n\n\n<li>From the same page, go to <strong>Configuration Settings &rarr; Manual Configuration<\/strong> and take note of the <strong>SMTP Port<\/strong> and <strong>Hostname<\/strong>.<\/li>\n\n\n\n<li>Access the <strong>hPanel<\/strong> dashboard and navigate to <strong>Files &rarr; File Manager<\/strong>. Click on the <strong>public_html<\/strong> folder and select <strong>Add New<\/strong> to create a new file. Name the file <strong>phpmailer.php<\/strong> and click <strong>Create<\/strong>.<\/li>\n\n\n\n<li>Double-click the <strong>phpmailer.php<\/strong> file, and copy and paste the code below after making all the necessary changes. Make sure to replace the <strong>mymail@myawesomedomain.tld<\/strong> and <strong>recipient@domain.tld<\/strong> with your existing domain and <strong>My$tr0ngPa55w0rd!<\/strong> with your email account password.<\/li>\n<\/ol><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\nrequire 'vendor\/autoload.php';\nuse PHPMailer\\PHPMailer\\PHPMailer;\n$mail = new PHPMailer;\n$mail-&gt;isSMTP();\n$mail-&gt;SMTPDebug = 2;\n$mail-&gt;Host = 'smtp.hostinger.com';\n$mail-&gt;Port = 587;\n$mail-&gt;SMTPAuth = true;\n$mail-&gt;Username = 'mymail@myawesomedomain.tld';\n$mail-&gt;Password = 'My$tr0ngPa55w0rd!';\n$mail-&gt;setFrom('mymail@myawesomedomain.tld', 'Your Name');\n$mail-&gt;addReplyTo('mymail@myawesomedomain.tld', 'Your Name');\n$mail-&gt;addAddress('recipient@domain.tld', 'Receiver Name');\n$mail-&gt;Subject = 'Checking if PHPMailer works';\n$mail-&gt;msgHTML(file_get_contents('message.html'), __DIR__);\n$mail-&gt;Body = 'This is just a plain text message body';\n\/\/$mail-&gt;addAttachment('attachment.txt');\nif (!$mail-&gt;send()) {\necho 'Mailer Error:'. $mail-&gt;ErrorInfo;\n} else {\necho 'The email message was sent.';\n}\n?&gt;<\/pre><ol start=\"6\" class=\"wp-block-list\">\n<li>After editing the code, click <strong>Save &amp; Close<\/strong>. To execute the script, enter <strong>yourdomain.tld\/phpmailer.php<\/strong> in your web browser.<\/li>\n<\/ol><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Configuring Titan SMTP<\/h4>\n                    <p>If you're using Titan Mail, connect via port <strong>465<\/strong> (SSL) or <strong>587<\/strong> (STARTTLS). Visit <strong>Emails &rarr; Configure Desktop App<\/strong> to see your <a href=\"https:\/\/support.hostinger.com\/en\/articles\/5966022-how-to-get-email-account-configuration-details-for-titan-email\" target=\"_blank\" rel=\"noopener\">email account configuration details<\/a>. You can also check <a href=\"https:\/\/support.titan.email\/hc\/en-us\/articles\/900000215446-Configure-Titan-on-other-apps-using-IMAP-POP\" target=\"_blank\" rel=\"noopener\"> Titan's official tutorial<\/a> to get the correct SMTP, IMAP, or POP settings.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-creating-a-phpmailer-contact-form\"><strong>Creating a PHPMailer contact form<\/strong><\/h3><p>In addition to sending simple PHP mail, PHPMailer lets users create various contact forms, such as feedback surveys.<\/p><p>Like previous PHP scripts, create a new PHP file in the <strong>public_html<\/strong> folder and name it <strong>formscript.php<\/strong>. Copy and paste the script below into the new file and modify the values accordingly:<\/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\nuse PHPMailer\\PHPMailer\\PHPMailer;\n$msg = '';\nif (array_key_exists('email', $_POST)) {\nrequire 'vendor\/autoload.php';\n$mail = new PHPMailer;\n$mail-&gt;isSMTP();\n$mail-&gt;Host = 'smtp.hostinger.com';\n$mail-&gt;Port = 587;\n$mail-&gt;SMTPDebug = 0;\n$mail-&gt;SMTPAuth = true;\n$mail-&gt;Username = 'mymail@myawesomedomain.tld';\n$mail-&gt;Password = 'My$tr0ngPa55w0rd!';\n$mail-&gt;setFrom('mymail@myawesomedomain.tld', 'Mr. Snuffles');\n$mail-&gt;addAddress('recipient@domain.tld', 'Receiver Name');\nif ($mail-&gt;addReplyTo($_POST['email'], $_POST['name'])) {\n$mail-&gt;Subject = 'PHPMailer contact form';\n$mail-&gt;isHTML(false);\n$mail-&gt;Body = &lt;&lt;&lt;EOT\nEmail: {$_POST['email']}\nName: {$_POST['name']}\nMessage: {$_POST['message']}\nEOT;\nif (!$mail-&gt;send()) {\n$msg = 'Sorry, something went wrong. Please try again later.';\n} else {\n$msg = 'Message sent! Thanks for contacting us.';\n}\n} else {\n$msg = 'Share it with us!';\n}\n}\n?&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n&lt;meta charset=\"UTF-8\"&gt;\n&lt;title&gt;Contact form&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Contact us&lt;\/h1&gt;\n&lt;?php if (!empty($msg)) {\necho \"&lt;h2&gt;$msg&lt;\/h2&gt;\";\n} ?&gt;\n&lt;form method=\"POST\"&gt;\n&lt;label for=\"name\"&gt;Name: &lt;input type=\"text\" name=\"name\" id=\"name\"&gt;&lt;\/label&gt;&lt;br&gt;\n&lt;label for=\"email\"&gt;Email address: &lt;input type=\"email\" name=\"email\" id=\"email\"&gt;&lt;\/label&gt;&lt;br&gt;\n&lt;label for=\"message\"&gt;Message: &lt;textarea name=\"message\" id=\"message\" rows=\"8\" cols=\"20\"&gt;&lt;\/textarea&gt;&lt;\/label&gt;&lt;br&gt;\n&lt;input type=\"submit\" value=\"Send\"&gt;\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro tip<\/h4>\n                    <p>Taking user input from <strong>&#8203;&#8203;$_POST<\/strong> and using it unsanitized is not safe due to <strong>cross-site scripting (XSS)<\/strong> attacks. To avoid this, check out the <a href=\"https:\/\/www.geeksforgeeks.org\/what-are-the-best-input-sanitizing-functions-in-php\" target=\"_blank\" rel=\"noopener\">best practices for PHP variable sanitization<\/a>.<\/p>\n                <\/div>\n\n\n\n<\/p><p>Save your changes and run the script from your browser.<\/p><p>Here&rsquo;s what the result will look like:<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e2224a98e10\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"726\" height=\"605\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/Contact-Us-form-example.png\" alt=\"Contact Us form example\" class=\"wp-image-85517\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/05\/Contact-Us-form-example.png 726w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/05\/Contact-Us-form-example-300x250.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/05\/Contact-Us-form-example-150x125.png 150w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>If a visitor submits a message via the form, they will get a confirmation. The form&rsquo;s content will be sent to the email address you entered here:<\/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-&amp;gt;addAddress('recipient@domain.tld', 'Receiver Name');<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro tip<\/h4>\n                    <p>If the PHPMailer contact form doesn't work, change the <strong>$mail-&gt;SMTPDebug = 0;<\/strong> line to <strong>$mail-&gt;SMTPDebug = 2<\/strong> to identify the cause. Don't forget to remove the line or change the <strong>2<\/strong> to <strong>0<\/strong> after.<\/p>\n                <\/div>\n\n\n\n<\/p><p>Visit <a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\/tree\/master\/examples\" target=\"_blank\" rel=\"noopener\">PHPMailer&rsquo;s official repository<\/a> on GitHub to check out other examples of how to use this mail-sending library.<\/p><p>If you&rsquo;re a WordPress user, you can streamline the form-building process using a <a href=\"\/uk\/tutorials\/wordpress-contact-form-plugins\">contact form plugin<\/a> like <strong>Formidable Forms<\/strong>, <strong>Contact Form 7<\/strong>, or <strong>WPForms<\/strong>.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-send-emails-using-the-php-mail-function\"><strong>How to send emails using the PHP mail() function<\/strong><\/h2><p>Another method to send emails directly from PHP is the built-in <strong>mail() <\/strong>function. To use this feature, users hosting their PHP application or site on a local server must configure a <strong>Sendmail<\/strong> program by changing the <a href=\"\/uk\/tutorials\/what-is-php-ini\">php.ini file<\/a> in their PHP installation folder.<\/p><p>If you use a hosting server, <strong>Sendmail<\/strong> usually comes already pre-configured. However, you must ensure that your hosting provider allows you to manually manage the <strong>Sendmail service<\/strong> option.<\/p><p>By default, the <strong>Sendmail<\/strong> service is already enabled. Nevertheless, double-check to be sure.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Titan Mail enables the PHP <strong>mail()<\/strong> function by default. Thus, you can&rsquo;t switch it on or off in hPanel.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-understanding-php-mail-components\"><strong>Understanding PHP mail components<\/strong><\/h3><p>To help you understand the PHP <strong>mail() <\/strong>function, we&rsquo;ll review the components of the PHP script used in the previous section.<\/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=\"\">ini_set( 'display_errors', 1 );\nerror_reporting( E_ALL );<\/pre><p>The first two lines above enable error reporting to inform you if the PHP script has failed to execute.<\/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=\"\">$from = \"mymail@myawesomedomain.tld\";<\/pre><p>This line contains the sender&rsquo;s email address. Most hosting providers forbid adding random email addresses here due to <a href=\"https:\/\/www.malwarebytes.com\/spoofing\/\" target=\"_blank\" rel=\"noopener\">spoofing<\/a> risk. Therefore, it&rsquo;s better to use one with your domain name to execute the script successfully.<\/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=\"\">$to = \"recipient@domain.tld\";<\/pre><p>The recipient&rsquo;s email address goes here. Separate the email addresses with commas if you want to send multiple emails.<\/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=\"\">$subject = \"Checking PHP mail\";<\/pre><p>Enter the email subject line here.<\/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=\"\">$message = \"PHP mail works just fine\";<\/pre><p>Here, input the body of your email message.<\/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:\" . $from;<\/pre><p>This line commonly includes additional headers like <strong>From<\/strong>, <strong>Reply-To<\/strong>, and <strong>Cc<\/strong>. You can separate them with the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/CRLF\" target=\"_blank\" rel=\"noopener\">CRLF<\/a>.<\/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))<\/pre><p>This script executes the <strong>mail()<\/strong> function and checks whether it has run successfully.<\/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=\"\">echo \"The email message was sent.\";<\/pre><p>The message above will appear if the <strong>mail()<\/strong> function is executed successfully.<\/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=\"\">echo \"The email message was not sent.\";<\/pre><p>Alternatively, you will see this message if the <strong>mail()<\/strong> function fails.<\/p><p>Although additional headers are optional, it&rsquo;s essential to include the <strong>From <\/strong>header when sending mail. Otherwise, you&rsquo;ll receive a notification like 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=\"\">Warning: mail(): \"sendmail_from\" not set in php.ini or custom \"From:\" header missing.<\/pre><p>For more information about the <strong>Sendmail <\/strong>function and its parameters, refer to the <a href=\"https:\/\/www.php.net\/manual\/en\/function.mail.php\" target=\"_blank\" rel=\"noopener\">official PHP documentation<\/a>.<\/p><h3 class=\"wp-block-heading\" id=\"h-creating-a-test-file-for-php-mail\"><strong>Creating a test file for PHP mail<\/strong><\/h3><p>After assuring that <strong>Sendmail<\/strong> is active, create a PHP mail file inside the <strong>public_html <\/strong>directory.<\/p><p>Here&rsquo;s how to do it:<\/p><ol class=\"wp-block-list\">\n<li>From <strong>hPanel<\/strong>, navigate to<strong> Files <\/strong>&rarr; <strong>File Manager <\/strong>to access <strong>Hostinger&rsquo;s File Manager<\/strong>.<\/li>\n\n\n\n<li>Double-click the <strong>public_html<\/strong> folder and select the <strong>New File <\/strong>icon at the top. Name this new file <strong>testmail.php<\/strong> and hit <strong>Create<\/strong>.<\/li>\n\n\n\n<li>Double-click on <strong>testmail.php<\/strong> to edit it. You can use the basic PHP code below but change the parameters accordingly:<\/li>\n<\/ol><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\nini_set( 'display_errors', 1 );\nerror_reporting( E_ALL );\n$from = \"mymail@myawesomedomain.tld\";\n$to = \"recipient@domain.tld\";\n$subject = \"Checking PHP mail\";\n$message = \"PHP mail works just fine\";\n$headers = \"From:\" . $from;\nif(mail($to,$subject,$message, $headers)) {\necho \"The email message was sent.\";\n} else {\necho \"The email message was not sent.\";\n}\n?&gt;<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Click <strong>Save &amp; Close <\/strong>once finished.<\/li>\n\n\n\n<li>Send the email by accessing <strong>yourdomain\/testmail.php<\/strong> from your web browser. Remember to change <strong>&ldquo;yourdomain&rdquo;<\/strong> to the domain used when creating <strong>testmail.php<\/strong>.<\/li>\n<\/ol><h3 class=\"wp-block-heading\" id=\"h-sending-html-emails-with-php\"><strong>Sending HTML emails with PHP<\/strong><\/h3><p>The PHP <strong>mail() <\/strong>function can also send HTML emails. This format is highly customizable compared to a plain text message.<\/p><p>The process to send HTML mail is the same, but you must include an HTML message and additional parameter headers this time.<\/p><p>Here is an example of a basic script to send an HTML email:<\/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\nini_set( 'display_errors', 1 );\nerror_reporting( E_ALL );\n$from = \"mymail@myawesomedomain.tld\";\n$to = \"recipient@domain.tld\";\n$subject = \"Checking PHP mail\";\n$message = \"\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;This is a test HTML email&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;p&gt;Hi, it's a test email. Please ignore.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n\";\n\/\/ The content-type header must be set when sending HTML email\n$headers = \"MIME-Version: 1.0\" . \"\\r\\n\";\n$headers .= \"Content-type:text\/html;charset=UTF-8\" . \"\\r\\n\";\n$headers = \"From:\" . $from;\nif(mail($to,$subject,$message, $headers)) {\necho \"Message was sent.\";\n} else {\necho \"Message was not sent.\";\n}\n?&gt;<\/pre><h2 class=\"wp-block-heading\" id=\"h-how-to-troubleshoot-common-php-mail-and-phpmailer-errors\"><strong>How to troubleshoot common PHP mail and PHPMailer errors<\/strong><\/h2><p>In the following section, we&rsquo;ll review some of the most common issues when using the PHP<strong> mail() <\/strong>function or PHPMailer and how to fix them.<\/p><h3 class=\"wp-block-heading\" id=\"h-sender-address-rejected-not-owned-by-the-user\"><strong>Sender address rejected: not owned by the user<\/strong><\/h3><p>This error means the server could not authenticate the sender using the provided details. To fix it, check the email address you&rsquo;ve used to send the message and ensure it corresponds to an existing one.<\/p><p>Also, make sure your <strong>Sender Policy Framework (SPF)<\/strong> is enabled. If you use Hostinger, check your SPF record by going to <strong>hPanel<\/strong> and navigating to <strong>Emails <\/strong>&rarr; <strong>Email Accounts <\/strong>&rarr; <strong>DNS settings<\/strong> &rarr; <strong>Manage Email Delivery<\/strong>.<\/p><p>If the SPF record is enabled, it will be shown as <strong>Active.<\/strong><\/p><h3 class=\"wp-block-heading\" id=\"h-gmail-couldn-t-verify-that-domain-tld-sent-this-message\"><strong>Gmail couldn&rsquo;t verify that domain.tld sent this message<\/strong><\/h3><p>If you see this warning when testing a PHP mail script, the cause might be one of the following:<\/p><ul class=\"wp-block-list\">\n<li><strong>There is no SPF record in the domain&rsquo;s DNS Zone<\/strong> &ndash; if the record is missing or you&rsquo;re using external nameservers, manually add a new SPF TXT record via hPanel or cPanel.<\/li>\n\n\n\n<li><strong>You used invalid SMTP authentication details<\/strong> &ndash; make sure to use an existing email address that belongs to you.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-mail-goes-to-the-spam-folder\"><strong>Mail goes to the spam folder<\/strong><\/h3><p>There are various reasons why PHP mail might trigger spam filters. Some of the most common causes include:<\/p><ul class=\"wp-block-list\">\n<li><strong>Misleading or spam-like subject <\/strong>&ndash; usually happens when using terms such as &ldquo;test&rdquo; or &ldquo;urgent.&rdquo; Be sure to set a clear intent in the <strong>Subject<\/strong> line.<\/li>\n\n\n\n<li><strong>Incorrect sender address <\/strong>&ndash; adding the wrong address can invoke security measures to filter your email to prevent spoofing and scams.<\/li>\n\n\n\n<li><strong>Using spam trigger words <\/strong>&ndash; remove spammy words like &ldquo;a great offer&rdquo; and &ldquo;this is not spam&rdquo; from your message to increase your email&rsquo;s credibility.<\/li>\n\n\n\n<li><strong>Your mailing list doesn&rsquo;t have an unsubscribe link <\/strong>&ndash; ensure to include an unsubscribe button to prevent this issue and build reader trust.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-could-not-connect-to-smtp-host\"><strong>Could not connect to SMTP host<\/strong><\/h3><p>Newer PHP versions usually have stricter SSL behavior. If the PHP <strong>mail()<\/strong> function fails to execute after updating your PHP to the latest version, this might be what causes it.<\/p><p>Using PHPMailer with some hosting providers usually triggers this code error as well.<\/p><p>For example, according to the <a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\/wiki\/Troubleshooting#php-56-certificate-verification-failure\" target=\"_blank\" rel=\"noopener\">PHPMailer wiki<\/a>, GoDaddy blocks outbound SMTP connections via ports <strong>25<\/strong>, <strong>465<\/strong>, and <strong>587<\/strong> to third-party servers like Gmail and Hotmail, except their own. Furthermore, it doesn&rsquo;t support integration with third-party SMTP servers.<\/p><p>Use the following script to resolve this error:<\/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-&gt;SMTPOptions = array( \n'ssl' =&gt; array( \n'verify_peer' =&gt; false, \n'verify_peer_name' =&gt; false, \n'allow_self_signed' =&gt; true \n) \n);<\/pre><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Avoid implementing these changes globally in <strong>php.ini<\/strong>, as doing so allows insecure connections, a security issue that PHP has addressed since version <strong>5.6<\/strong>. <\/p><\/div>\n\n\n\n<\/p><p>As Hostinger SMTP doesn&rsquo;t have this issue, consider migrating to our <a href=\"\/uk\/business-email\">business email service<\/a> for a better mailing experience.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/business-email\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/Email-hosting-cta-banner-1024x300.png\" alt=\"\" class=\"wp-image-77916\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/Email-hosting-cta-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/Email-hosting-cta-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/Email-hosting-cta-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/Email-hosting-cta-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\"><strong>Conclusion<\/strong><\/h2><p>You can send emails with PHP using the<strong> mail() <\/strong>function or a mail-sending library like PHPMailer. The former is suitable for sending small volumes of simple text-based messages, whereas the latter is better for sending bulk emails or creating contact forms.<\/p><p>You can leverage the built-in PHP mail function by creating a new PHP file in the <strong>public_html <\/strong>directory and executing the script using your web browser.<\/p><p>On the other hand, sending emails with PHPMailer requires installing the code library via Composer, setting up an email account for it, and configuring your hosting&rsquo;s SMTP settings.<\/p><p>In this tutorial, we covered installing PHPMailer, creating a test script, and setting up a simple contact form. We also covered sending emails with the PHP <strong>mail()<\/strong> function and troubleshooting common errors during the email-sending process.<\/p><p>We hope you found this tutorial useful. If you have any further questions, leave them in the comments section below.<\/p><h2 class=\"wp-block-heading\" id=\"h-php-mail-faq\"><strong>PHP mail FAQ<\/strong><\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1741912585407\"><h3 class=\"schema-faq-question\"><strong>Can I send emails from PHPMailer to Gmail or other email services?<\/strong><\/h3> <p class=\"schema-faq-answer\">It depends on your hosting provider. With GoDaddy, you can&rsquo;t send mail to inboxes that implement SPF and DKIM email authentication protocols like Gmail, Yahoo, and Hotmail. However, this issue doesn&rsquo;t persist with Hostinger. Contact your web host to clarify its SMTP settings and whether it supports PHPMailer with third-party SMTP servers.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1741912596240\"><h3 class=\"schema-faq-question\"><strong>How can I validate email addresses before using the PHP mail() function or PHPMailer to send emails?<\/strong><\/h3> <p class=\"schema-faq-answer\">You can use the <strong>filter_var()<\/strong> function and pass the email address to the <strong>FILTER_VALIDATE_EMAIL<\/strong> filter. This filter will verify the email&rsquo;s validity, ensuring it doesn&rsquo;t contain unsupported characters or white spaces.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Most businesses create a professional email account to boost their credibility and build customer trust. To accomplish this, you need to buy a domain and set up your business email on an email client or a server. If you build your website or web application with the PHP programming language, you can send emails directly [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/send-emails-using-php-mail\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":115,"featured_media":69236,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to send emails with the PHP mail() function and PHPMailer","rank_math_description":"Learn how to send emails from your web server with PHP using the inbuilt PHP mail() function and PHPMailer in this guide.","rank_math_focus_keyword":"php mail, php mail() function","footnotes":""},"categories":[22632,22624,22626],"tags":[],"class_list":["post-685","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-email","category-hosting","category-technical-aspects"],"hreflangs":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=685"}],"version-history":[{"count":116,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/685\/revisions"}],"predecessor-version":[{"id":126780,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/685\/revisions\/126780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media\/69236"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}