How toTech

How to send an email in PHP?

Send an email in PHP: You can send emails using PHP. Yes, PHP allows you to do so using the mail() function. Using this mail() function you can directly send emails through the script. This mail() function is a built-in PHP function. The mail() function has some parameters listed. In this article, we will get to know the different ways on how to send mail in PHP.

Using PHP you can send mails with plain text, HTML text and also add attachments to your mail. In PHP, programmers have to write web applications based on complex business requirements. The tools, features, and code snippets provided by PHP frameworks help developers to accelerate custom web application development. Although the PHP developers need to write long codes, the PHP frameworks reduce coding time significantly by providing code generation feature.

Sending emails in PHP

As quoted above, the mail() function in PHP allows you to send mails directly from the script. But, for the mail functions to be available PHP requires an installed and working mail system. The program to be used is defined by the configuration settings in the php.ini file. Unlike other platforms, there is no need to install the functions in PHP. The functions are a part of the PHP core.

Here is a picture of the run time configuration of PHP mail() functions. The behavior of the mail functions is affected by the settings in php.ini.

Run time configuration of PHP mail functions
Run time configuration

Syntax: The Syntax of the PHP mail() function is as follows,


Here is the description of the above parameters in the syntax,

to – Specifies the receiver or receivers of the email

subject – Specifies the subject of the email

message – Defines the message to be sent

headers – Specifies additional headers, like From, Cc, and Bcc.

parameters – Specifies an additional parameter to the send mail program 

As soon as you initiate the mail() function PHP will attempt to send the email. It will return the true value if it is successful or it will return false if it is not.

There are pretty much SQL injection in PHP. To know how you can get rid of them and also prevent them click on the link highlighted below.

How to prevent SQL injections in PHP


In this section we will go through the detailed application of the mail() function. Firstly we will get to see how the windows users can handle this mail() function.

Windows users need to ensure that two directives are supplied. The first one is the SMTP that defines your email server address. The second is the sendmail_from which defines your own email address. The configuration for windows should looks like,

[mail function] 
; For Win32 only.

; For win32 only
sendmail_from = [email protected]

And, for the Linux users you can simply let PHP know the location of their send mail application. The path and any desired switches should be specified to the sendmail_path directive. The configuration for Linux looks like,

[mail function] 
; For Win32 only.

; For win32 only
sendmail_from =

; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i

Simple Transmission Protocol (SMTP)

PHP uses the SMTP to send emails. In PHP on a hosted server the SMTP settings would already have been set. The SMTP mail settings can be configured from the “php.ini” file in the PHP installation folder. Let us configure the SMTP settings on your localhost. Assuming you are using xampp on windows, locate the “php.ini” in the directory “C:\xampp\php”.

a) Open it using notepad or any text editor

Open in notepad notepad

b) Click on Find menu

Click on Find

c) The find dialog menu will appear

You will get to open the find dialog window
find dialog

d) Click on Find next button

Click on the find next button
Find next

e) Locate the entries

  • [mail function]
  • ; XAMPP: Don’t remove the semi column if you want to work with an SMTP Server like Mercury
  • ; SMTP = localhost
  • ; smtp_port = 25
  • Remove the semi colons before SMTP and smtp_port and set the SMTP to your smtp server and the port to your smtp port. Your settings should look as follows
    • SMTP =
    • smtp_port = 25
    • Note the SMTP settings can be gotten from your web hosting providers.
    • If the server requires authentication, then add the following lines.

Sending Emails using mail() function

Explore the different ways on how you can send mails using the PHP mail() function.

a) Send a simple mail
Here is a simple example you can refer on how can send a simple mail using PHP mail() function.

// the message
$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail("[email protected]","My subject",$msg);

b) Send an email with extra headers
Here is a simple example you can refer to on how you can send an email with extra headers.

$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";


c) Send an HTML email
Here is a simple example you can refer to on how you can send a HTML email.

$to = "[email protected], [email protected]";
$subject = "HTML email";

$message = "
<title>HTML email</title>
<p>This email contains HTML Tags!</p>

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";



Here are some basic FAQ’s which are trending on web. Scroll through them and clear up your doubts in regard to this topic if any. These are some random handpicked FAQ’s from web.

What is PHPmailer in PHP?

PHPMailer is a class library for PHP that provides a collection of functions to build and send email messages. PHPMailer supports several ways of sending email: mail(), Sendmail, email & direct to SMTP servers. PHPMailermakes it easy to send e-mail, makes it possible to attach files, send HTML e-mail, etc.

How i enable SMTP in gmail?

1) Go to the “Settings”, e.g. click on the “Gears” icon and select “Settings”.
2) Click on “Forwarding and POP/IMAP”.
3) Enable “IMAP Access” and/or “POP Download”

Can we send mail from local host in PHP?

But PHP mail() function will not work at the localhost. Using this simple PHP script you can send email from any localhost server, like XAMPP, WAMP, or any others. We’ll use PHPMailer for helping to send email from localhost.

To conclude: Hence, through this article, we have got to learn the different ways to send an email through PHP and also the various steps involved. If you have any queries in regard to this article comment them below in the comment section. Do comment your views too. Also, share the content if you like it. For some more of interesting articles in regard to the tech and tech news visit and delve into some amazing stuff.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Related Articles

Leave a Reply

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

Back to top button