{"id":127340,"date":"2022-08-17T15:31:09","date_gmt":"2022-08-17T15:31:09","guid":{"rendered":"https:\/\/phppot.com\/?p=18970"},"modified":"2022-08-17T15:31:09","modified_gmt":"2022-08-17T15:31:09","slug":"send-email-from-localhost-using-php-with-smtp-and-phpmailer","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/17\/send-email-from-localhost-using-php-with-smtp-and-phpmailer\/","title":{"rendered":"Send Email from Localhost using PHP with SMTP and PHPMailer"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1111111111111\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on August 17th, 2022.<\/div>\n<p>Localhost is the web developer\u2019s favorite home. Local development environment is always convenient to develop and debug scripts.<\/p>\n<p>Mailing via a script with in-built PHP <a href=\"https:\/\/phppot.com\/php\/functions-in-php\/\">function<\/a>. This may not work almost always in a localhost. You need to have a sendmail program and appropriate configurations.<\/p>\n<p>If the <a href=\"https:\/\/phppot.com\/php\/php-mail\/\">PHP mail()<\/a> is not working on your localhost, there is an alternate solution to sending email. You can use a SMTP server and send email from localhost and it is the popular choice for sending emails for PHP programmers.<\/p>\n<p>This example shows code to use PHPMailer to send email using SMTP from localhost.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-18983\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/mail-sending-from-localhost-using-smtp-550x215.jpg\" alt=\"mail sending from localhost using smtp\" width=\"550\" height=\"215\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/mail-sending-from-localhost-using-smtp-550x215.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/mail-sending-from-localhost-using-smtp-300x117.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/mail-sending-from-localhost-using-smtp-768x300.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/mail-sending-from-localhost-using-smtp.jpg 800w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>PHP Mail sending script with SMTP<\/h2>\n<p>This program uses the PHPMailer library to connect SMTP to send emails. You can test this in your localhost server. You need access to a SMTP server.<\/p>\n<p>Before running this code, configure the SMTP settings to set the following details.<\/p>\n<ol>\n<li>Authentication directive and security protocol.<\/li>\n<li>Configure SMTP credentials to authenticate and authorize mail sending script.<\/li>\n<li>Add From, To and Reply-To addresses using the PHPMailer object.<\/li>\n<li>Build email body and add subject before sending the email.<\/li>\n<\/ol>\n<p>The above steps are mandatory with the PHPMailer mail sending code. Added to that, this mailing library supports many features. Some of them are,<\/p>\n<p>Set the SMTP Debug = 4 in development mode to print the status details of the mail-sending script.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\nuse PHPMailer\\PHPMailer\\PHPMailer;\nuse PHPMailer\\PHPMailer\\SMTP;\nuse PHPMailer\\PHPMailer\\Exception; require_once __DIR__ . '\/vendor\/phpmailer\/src\/Exception.php';\nrequire_once __DIR__ . '\/vendor\/phpmailer\/src\/PHPMailer.php';\nrequire_once __DIR__ . '\/vendor\/phpmailer\/src\/SMTP.php'; $mail = new PHPMailer(true); $mail-&gt;SMTPDebug = 0;\n$mail-&gt;isSMTP();\n$mail-&gt;Host = 'SET-SMTP-HOST';\n$mail-&gt;SMTPAuth = true;\n$mail-&gt;SMTPSecure = \"ssl\";\n$mail-&gt;Port = 465; $mail-&gt;mailer = \"smtp\"; $mail-&gt;Username = 'SET-SMTP-USERNAME';\n$mail-&gt;Password = 'SET-SMTP-PASSWORD'; \/\/ Sender and recipient address\n$mail-&gt;SetFrom('SET-SENDER-EMAIL', 'SET-SENDER_NAME');\n$mail-&gt;addAddress('ADD-RECIPIENT-EMAIL', 'ADD-RECIPIENT-NAME');\n$mail-&gt;addReplyTo('ADD-REPLY-TO-EMAIL', 'ADD-REPLY-TO-NAME'); \/\/ Setting the subject and body\n$mail-&gt;IsHTML(true);\n$mail-&gt;Subject = \"Send email from localhost using PHP\";\n$mail-&gt;Body = 'Hello World!'; if ($mail-&gt;send()) { echo \"Email is sent successfully.\";\n} else { echo \"Error in sending an email. Mailer Error: {$mail-&gt;ErrorInfo}\";\n}\n?&gt;\n<\/code><\/pre>\n<h2>Google and Microsoft disabled insecure authentication<\/h2>\n<p>Earlier, programmers conveniently used GMail\u2019s SMTP server for sending emails via PHP. Now, less secure APPs configuration in Google is disabled. Google and Microsoft force authentication via OAuth 2 to send email.<\/p>\n<p>There is ill-informed text going around in this topic. People say that we cannot send email via Google SMTP any more. That is not the case. They have changed the authentication method.<\/p>\n<p><strong>IMPORTANT<\/strong>: I have written an article to help you <a href=\"https:\/\/phppot.com\/php\/sending-email-using-phpmailer-with-gmail-xoauth2\/\">send email using xOAuth2 via PHP<\/a>. You can use this and continue to send email using Google or other email service providers who force to use OAuth.<\/p>\n<h2>Alternate: Enabling PHP\u2019s built-in mail()<\/h2>\n<ol>\n<li>If you do not have access to an email SMTP server.<\/li>\n<li>If you are not able to use xOAuth2 and Google \/ Microsoft\u2019s mailing service.<\/li>\n<\/ol>\n<p>In the above situations, you may try to setup your own server in localhost. Yes! that is possible and it will work.<\/p>\n<p>PHP has a built-in mail function to send emails without using any third-party libraries.<\/p>\n<p>The mail() function is capable of simple mail sending requirements in PHP.<\/p>\n<p>In localhost, it will not work without setting the <a href=\"https:\/\/phppot.com\/php\/php-ini-file\/\">php.ini configuration<\/a>. Find the following section in your php.ini file and set the sendmail path and related configuration.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-18981\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/php.ini-mail-setting-550x310.jpg\" alt=\"php.ini mail setting\" width=\"550\" height=\"310\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/php.ini-mail-setting-550x310.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/php.ini-mail-setting-300x169.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/php.ini-mail-setting.jpg 600w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<p><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/php\/send-email-localhost-php.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/php\/send-email-localhost-php\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on August 17th, 2022. Localhost is the web developer\u2019s favorite home. Local development environment is always convenient to develop and debug scripts. Mailing via a script with in-built PHP function. This may not work almost always in a localhost. You need to have a sendmail program and appropriate configurations. If the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":127341,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-127340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127340"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127340\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/127341"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}