How to Send Email from Localhost in PHP?

In this tutorial, we will show you how to send emails in PHP from localhost with ubuntu, and windows xampp using mail() function.

How to Send Email from Localhost in PHP Ubuntu, Windows Xampp?

Here are steps:

The PHP mail() Function

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

Now, 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(tosubjectmessageheadersparameters)

Now, this is mail function parameters:

ParameterRequired or OptionalDescription
toYesThe recipient’s email address.
subjectYesSubject 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”, “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 in PHP

The easy way to send an email with PHP is to send a text in the email. In the example, we first declare the variables — 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 plain text in emails

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

<?php
$to = '[email protected]';
$subject = 'Testing Purpose';
$message = 'Hi there, this is the test mail using php mail() function?';
$from = '[email protected]';

// 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 the emails.

Send HTML Email in the PHP

When you send a simple text message in the mail using 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 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 send HTML email php code:

The below example sends html in emails using the PHP Mail() function:

<?php
$to = '[email protected]'; // receiver email
$subject = 'Tesing Purpose'; // subject
$from = '[email protected]'; // 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

That’s all; in this tutorial, you have learned how to send email in PHP using the mail() function. Also, you have learned how to send simple text and formatted HTML in mails using the inbuilt PHP mail() function.

Recommended PHP Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *