Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Send Email from Localhost using PHP with SMTP and PHPMailer

#1
Send Email from Localhost using PHP with SMTP and PHPMailer

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/08/send-email-from-localhost-using-php-with-smtp-and-phpmailer.jpg" width="550" height="215" title="" alt="" /></div><div><div class="modified-on" readability="7.1111111111111"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on August 17th, 2022.</div>
<p>Localhost is the web developer’s favorite home. Local development environment is always convenient to develop and debug scripts.</p>
<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>
<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>
<p>This example shows code to use PHPMailer to send email using SMTP from localhost.</p>
<p><img 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/20...00x117.jpg 300w, https://phppot.com/wp-content/uploads/20...68x300.jpg 768w, https://phppot.com/wp-content/uploads/20...g-smtp.jpg 800w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>PHP Mail sending script with SMTP</h2>
<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>
<p>Before running this code, configure the SMTP settings to set the following details.</p>
<ol>
<li>Authentication directive and security protocol.</li>
<li>Configure SMTP credentials to authenticate and authorize mail sending script.</li>
<li>Add From, To and Reply-To addresses using the PHPMailer object.</li>
<li>Build email body and add subject before sending the email.</li>
</ol>
<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>
<p>Set the SMTP Debug = 4 in development mode to print the status details of the mail-sending script.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception; require_once __DIR__ . '/vendor/phpmailer/src/Exception.php';
require_once __DIR__ . '/vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/vendor/phpmailer/src/SMTP.php'; $mail = new PHPMailer(true); $mail-&gt;SMTPDebug = 0;
$mail-&gt;isSMTP();
$mail-&gt;Host = 'SET-SMTP-HOST';
$mail-&gt;SMTPAuth = true;
$mail-&gt;SMTPSecure = "ssl";
$mail-&gt;Port = 465; $mail-&gt;mailer = "smtp"; $mail-&gt;Username = 'SET-SMTP-USERNAME';
$mail-&gt;Password = 'SET-SMTP-PASSWORD'; // Sender and recipient address
$mail-&gt;SetFrom('SET-SENDER-EMAIL', 'SET-SENDER_NAME');
$mail-&gt;addAddress('ADD-RECIPIENT-EMAIL', 'ADD-RECIPIENT-NAME');
$mail-&gt;addReplyTo('ADD-REPLY-TO-EMAIL', 'ADD-REPLY-TO-NAME'); // Setting the subject and body
$mail-&gt;IsHTML(true);
$mail-&gt;Subject = "Send email from localhost using PHP";
$mail-&gt;Body = 'Hello World!'; if ($mail-&gt;send()) { echo "Email is sent successfully.";
} else { echo "Error in sending an email. Mailer Error: {$mail-&gt;ErrorInfo}";
}
?&gt;
</code></pre>
<h2>Google and Microsoft disabled insecure authentication</h2>
<p>Earlier, programmers conveniently used GMail’s 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>
<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>
<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>
<h2>Alternate: Enabling PHP’s built-in mail()</h2>
<ol>
<li>If you do not have access to an email SMTP server.</li>
<li>If you are not able to use xOAuth2 and Google / Microsoft’s mailing service.</li>
</ol>
<p>In the above situations, you may try to setup your own server in localhost. Yes! that is possible and it will work.</p>
<p>PHP has a built-in mail function to send emails without using any third-party libraries.</p>
<p>The mail() function is capable of simple mail sending requirements in PHP.</p>
<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>
<p><img 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/20...00x169.jpg 300w, https://phppot.com/wp-content/uploads/20...etting.jpg 600w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p><a class="download" href="https://phppot.com/downloads/php/send-email-localhost-php.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/send-email-localhost-php/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/08/...phpmailer/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016