Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to Send Email from Localhost in PHP

Home  »  How to • PHP • Tips   »   How to Send Email from Localhost in PHP
Posted on September 18, 2022September 18, 2022
559

PHP sends email using the Inbuilt PHP MAIL() function. This tutorial shows you how you can send emails in PHP using the inbuilt PHP mail() function.

If you work with PHP, you need to send emails to users like when the user registered with us, send a reset password link in the mail, if any offers send to users by email, send invoice or pdf, welcome emails, etc.

How to Send Email from Localhost in PHP

Using the PHP inbuilt mail() function, You can send text or HTML in emails from localhost in PHP.

The PHP mail() Function

Sending emails is a very common process for any web application. Such as welcome email, reset password link, invoice or PDF if there is an offer, etc.

Here you will learn how to send mail from your web application to one or more users using the PHP built-in mail () function. either in plain-text form or in HTML format.

The basic syntax of the PHP mail function is:

 mail(to, subject, message, headers, parameters)

Now we demonstrate to you the mail function parameters:

ParameterRequired or OptionalDescription
toYesThe recipient’s email address.
subjectYesThe subject of the email to be sent. This parameter i.e. the subject line cannot contain any newline character (\n).
messageYesDefines the message to be sent. Each line should be separated with a line feed-LF (\n). Lines should not exceed 70 characters.
headersNoThis is typically used to add extra headers such as “From”, “Cc”, and “Bcc”. The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n).
parametersNoUsed to pass additional parameters.

Send Plain Text In Emails – PHP

The easy way to send an email with PHP is to send a text in the email. In the below-given example, we first declare the variables — the recipient’s email address, subject line, and message body. After that, we pass these defined variables to the mail() function to send the email.

Example of sending a plain text in emails

The below example sends the plain text in emails using the PHP Mail() function.

<?php
$to = 'youremail@email.com';
$subject = 'Testing Purpose';
$message = 'Hi there, this is the test mail using php mail() function?'; 
$from = 'info@teachdeveloper.com';
  
// Sending email
if(mail($to, $subject, $message)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending emails.

Send HTML in Emails Using PHP Mail Function

When you send a simple text message in the mail using the PHP mail function, that is treated as simple text. We are sending a well-designed mail to users, so that time we will send HTML in mail

If you want to send an HTML in emails using the PHP mail function. So don’t worry, the email sending process will be the same. However, this time we add additional headers as well as an HTML formatted message.

Example of sending HTML in the email using PHP mail()

<?php
$to = 'yourmail@email.com'; // receiver email
$subject = 'Tesing Purpose'; // subject
$from = 'info@teachdeveloper.com'; // send email
  
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi There!</h1>';
$message .= '<p style="color:#080;font-size:20px;">This is a testing mail with html?</p>';
$message .= '</body></html>';
  
// Sending email to receipt
if(mail($to, $subject, $message, $headers)) {
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

Conclusion

Send PHP Mail Tutorial. Here you have learned how to send an email in PHP using the mail() function. Also, you learned how to send plain text and formatted HTML in emails using the inbuilt PHP mail() function.

How to, PHP, Tips

Post navigation

Previous Post: How to PHP Get Max Value From Array
Next Post: PHP 8: Constructor property promotion

Related Posts

  • How to split a string into an array with Twig
  • How do sum values from an array of key-value pairs in JavaScript?
  • How to Display Breadcrumb without Plugin in WordPress
  • How to Convert PHP CSV to JSON
  • How to make the scrollbar visible in mobile browsers with CSS?
  • Enable or Disable SSH Root Login Access in Linux

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

Git basics: The simple commands to begin with
How to Make Toggles With React Hooks
How to Send Email from Localhost in PHP
How to make the div move up and down when scrolling the page with CSS?
ES6 classes

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved