{"id":1188,"date":"2021-10-19T08:12:03","date_gmt":"2021-10-19T08:12:03","guid":{"rendered":"https:\/\/lite16.com\/blog\/?p=1188"},"modified":"2021-10-19T08:12:03","modified_gmt":"2021-10-19T08:12:03","slug":"how-to-make-use-of-phps-mail-function-to-send-emails","status":"publish","type":"post","link":"https:\/\/lite16.com\/blog\/2021\/10\/19\/how-to-make-use-of-phps-mail-function-to-send-emails\/","title":{"rendered":"How to Make Use of PHP&#8217;s mail() Function to Send Emails"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.tutsmake.com\/wp-content\/uploads\/2019\/10\/Send-Email-In-PHP-using-Mail-PHP-Mail.jpeg\" alt=\"See the source image\" \/><\/p>\n<p>To send emails directly from a PHP script, the first method is to use the built-in mail() function.<\/p>\n<p>To make use of the PHP send mail feature, users who host their PHP application or website on a local server must configure the Sendmail program by editing the php.ini file in their PHP installation directory.<\/p>\n<p>If you&#8217;re using a hosting server, Sendmail is almost always configured for you. However, you must ensure that your hosting provider permits manual management of the Sendmail service.<\/p>\n<p>Users of Hostinger can enable or disable this feature by logging into the hPanel and navigating to Emails -&gt; Mail Service Control.<\/p>\n<p>The Sendmail service is enabled by default. Nonetheless, double-check to ensure accuracy.<\/p>\n<h4>Creating a PHP Mail Test File<\/h4>\n<p>After ensuring that Sendmail is operational, we&#8217;ll create a PHP mail script file in the public html directory.<\/p>\n<p>How to do it:<\/p>\n<p>1. To access the Hostinger File Manager, navigate to Files -&gt; File Manager in hPanel.<\/p>\n<p>2. Double-click the public html folder and then on the top bar, click the New File icon. Give this new file the name testmail.php and then click Create.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/testmail-file-1024x453.png\" alt=\"New file: testmail.php\" \/><\/p>\n<p>3. To edit testmail.php, double-click on it. You may use the basic PHP code below, but you must adjust the parameters appropriately.<\/p>\n<p>&lt;?php<\/p>\n<p>ini_set( &#8216;display_errors&#8217;, 1 );<\/p>\n<p>error_reporting( E_ALL );<\/p>\n<p>$from = &#8220;test@hostinger-tutorials.com&#8221;;<\/p>\n<p>$to = &#8220;test@hostinger.com&#8221;;<\/p>\n<p>$subject = &#8220;Checking PHP mail&#8221;;<\/p>\n<p>$message = &#8220;PHP mail works just fine&#8221;;<\/p>\n<p>$headers = &#8220;From:&#8221; . $from;<\/p>\n<p>if(mail($to,$subject,$message, $headers)) {<\/p>\n<p>echo &#8220;The email message was sent.&#8221;;<\/p>\n<p>} else {<\/p>\n<p>echo &#8220;The email message was not sent.&#8221;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>4. Once finished editing, click Save &amp; Close.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/edit-file-1024x458.png\" alt=\"Editing the testmail.php file\" \/><\/p>\n<p>5. Send the email by browsing to YourDomain\/testmail.php. Remember to replace YourDomain with the domain name that was used to create testmail.php.<\/p>\n<p>6. The recipient will receive the following message:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/test-email-1024x461.png\" alt=\"PHP mail test\" \/><\/p>\n<h4>How to Send Emails Using PHPMailer<\/h4>\n<p>If you&#8217;re sending a large number of emails, it&#8217;s recommended that you use an external PHP mailing package. The native PHP mail() function is not designed to handle large volumes of email because it opens and closes a Simple Mail Transfer Protocol (SMTP) socket connection for each email.<\/p>\n<p>Numerous PHP mail packages are available, including Pear Mail and Swift Mailer. We&#8217;ll use PHPMailer in this article.<\/p>\n<p>PHPMailer is a widely used email sending library that supports sending emails via the mail() function or an SMTP server. It provides access to a set of email-sending functions, which simplifies the process of configuring PHP mail.<\/p>\n<h4>Installing PHPMailer<\/h4>\n<p>Installing PHPMailer is a breeze, especially if you already have Composer installed \u2013 this tool is included with many shared hosting plans.<\/p>\n<p>To manually install PHPMailer, connect to your hosting account via the SSH terminal as follows:<\/p>\n<p>1. Download and install the PuTTY SSH client from the PuTTY website.<\/p>\n<p>2. From your hPanel dashboard, navigate to Advanced -&gt; SSH Access and note the SSH IP address, port number, username, and password listed under SSH Access information.<\/p>\n<p>3. Launch PuTTY and fill in the Host Name (or IP address) and Port fields with your SSH credentials. After that, click Open.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/putty-configuration.png\" alt=\"PuTTY Configuration, highlighting the Open button\" \/><\/p>\n<p>4. When a command window appears, enter your SSH username and password. Bear in mind that PuTTY will not display the password, so do not be alarmed if it does not appear.<\/p>\n<p>5. Enter the cd public html command.<\/p>\n<p>6. Finally, enter the composer require phpmailer\/phpmailer command.<\/p>\n<p>7. Wait a moment for the installation process to complete.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/command-window.png\" alt=\"PuTTY terminal window\" \/><\/p>\n<h4>Utilizing PHPMailer in conjunction with Hostinger&#8217;s SMTP<\/h4>\n<p>You can use PHPMailer to send PHP emails immediately after installation.<\/p>\n<p>In this section, we&#8217;ll demonstrate how to send email using PHPMailer via the Hostinger SMTP server:<\/p>\n<p>1. Access the hPanel and navigate to Emails -&gt; Email Accounts -&gt; Create New Email Account.<\/p>\n<p>2. Enter your new email address and password. After that, click Create. Bear in mind that you will be using this information to send email via PHPMailer.<\/p>\n<p>3. Return to the same page and navigate to Configuration Settings -&gt; Manual Configuration, noting the SMTP Host and Port.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/08\/configuration-settings-1024x421.png\" alt=\"Email configuration settings, highlighting the outgoing server (SMTP)\" \/><\/p>\n<p>4. Navigate to Files -&gt; File Manager in the hPanel dashboard. To create a new file, right-click the public html folder and select Add New. Create a file called testphpmailer.php and give it a name.<\/p>\n<p>5. Double-click the testphpmailer.php file and then copy and paste the code below, making any necessary modifications. Replace test@hostinger-tutorials.com with your Hostinger email address and EMAIL ACCOUNT PASSWORD with your Hostinger password.<\/p>\n<p>&lt;?php<\/p>\n<p>use PHPMailer\\PHPMailer\\PHPMailer;<\/p>\n<p>require &#8216;vendor\/autoload.php&#8217;;<\/p>\n<p>$mail = new PHPMailer;<\/p>\n<p>$mail-&gt;isSMTP();<\/p>\n<p>$mail-&gt;SMTPDebug = 2;<\/p>\n<p>$mail-&gt;Host = &#8216;smtp.hostinger.com&#8217;;<\/p>\n<p>$mail-&gt;Port = 587;<\/p>\n<p>$mail-&gt;SMTPAuth = true;<\/p>\n<p>$mail-&gt;Username = &#8216;test@hostinger-tutorials.com&#8217;;<\/p>\n<p>$mail-&gt;Password = &#8216;EMAIL_ACCOUNT_PASSWORD&#8217;;<\/p>\n<p>$mail-&gt;setFrom(&#8216;test@hostinger-tutorials.com&#8217;, &#8216;Your Name&#8217;);<\/p>\n<p>$mail-&gt;addReplyTo(&#8216;test@hostinger-tutorials.com&#8217;, &#8216;Your Name&#8217;);<\/p>\n<p>$mail-&gt;addAddress(&#8216;example@email.com&#8217;, &#8216;Receiver Name&#8217;);<\/p>\n<p>$mail-&gt;Subject = &#8216;Testing PHPMailer&#8217;;<\/p>\n<p>$mail-&gt;msgHTML(file_get_contents(&#8216;message.html&#8217;), __DIR__);<\/p>\n<p>$mail-&gt;Body = &#8216;This is a plain text message body&#8217;;<\/p>\n<p>\/\/$mail-&gt;addAttachment(&#8216;test.txt&#8217;);<\/p>\n<p>if (!$mail-&gt;send()) {<\/p>\n<p>echo &#8216;Mailer Error: &#8216; . $mail-&gt;ErrorInfo;<\/p>\n<p>} else {<\/p>\n<p>echo &#8216;The email message was sent.&#8217;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>6. Once you&#8217;ve finished editing the code, click Save &amp; Close. To run the script, navigate to YourDomain.com\/testphpmailer.php in your browser&#8217;s address bar.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To send emails directly from a PHP script, the first method is to use the built-in mail() function. To make use of the PHP send mail feature, users who host their PHP application or website on a local server must configure the Sendmail program by editing the php.ini file in their PHP installation directory. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1188","post","type-post","status-publish","format-standard","hentry","category-technical-how-to"],"_links":{"self":[{"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/posts\/1188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/comments?post=1188"}],"version-history":[{"count":1,"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1189,"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/posts\/1188\/revisions\/1189"}],"wp:attachment":[{"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/media?parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/categories?post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lite16.com\/blog\/wp-json\/wp\/v2\/tags?post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}