w3resource

PHP mail() function

Description

The mail() function is used to send a mail.

Version:

(PHP 4 and above)

Syntax:

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

Parameter:

Name Description Required /
Optional
Type
to Mail address to which you want to send mail  Required String
subject Subject of the mail  Required String
message Message to be sent with the mail. Each line of the message should be separated with a LF (\n). Lines should not be larger than 70 characters. Required String
extra headers Additional headers like from, CC, BCC. If more than one additional headers are used, they must be separated with CRLF (Carriage return line feed), i.e. new line.  Optional string
additional parameters Additional parameters like -  the envelope sender address when using sendmail with the -f sendmail option, can be used using this parameter.  Optional string

Return value:

Returns true if the mail is successfully sent, otherwise, it returns false. 

Value Type: boolean

Example:

<?php
$to  = '[email protected]';
$subject = 'Demo mail ';
$message = 'This is a demo mail. Please reply to make sure the mail communication is okay.';
mail($to, $subject, $message);
?> 

Send a simple mail using mail() function

Browse view of the form for sending simple mail in php

send-simple-mail-php

 

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" /><title>send mail in 
php</title>
<style type="text/css">
li {list-style-type: none;
font-size: 16pt;
}
.mail {
margin: auto;
padding-top: 10px;
padding-bottom: 10px;
width: 400px;
background : #D8F1F8;
border: 1px solid silver;
}
.mail h2 {
margin-left: 38px;
}
input {
font-size: 20pt;
}
input:focus, textarea:focus{
background-color: lightyellow;
}
input submit {
font-size: 12pt;
}
</style>
</head>
<body>
<div class="mail">
<h2>Mail us</h2>
<ul>
<li><form name="mail" method="POST" action="send.php"></li>
<li>To:</li>
<li><input type="text" name="to" /></li>
<li>Subject:</li>
<li><input type="text" name="subject" /></li>
<li>Message:</li>
<li><input type="text" name="Message" /></li>
<li>&nbsp;</li>
<li><input type="submit" name="submit" value="Send"/></li>
<li>&nbsp;</li>
</form>
</ul>
</div>
</body></html>

Code of the file for handling form (code above) data :

<?php
$to = $_POST['to'] ;    
$message = $_POST['Message'] ;
mail( $to, "Mail", $message,  );
header( "Location: http://localhost/php/simple-mail.php" );
?>

Send mail with extra headers using mail() function

Browser view of the form for sending mail with extra headers in php 

simple-mail-php-extra-headers

 

Code:

<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type" /><title>send mail in
php</title>
<style type="text/css">
li {list-style-type: none;
font-size: 16pt;
}
.mail {
margin: auto;
padding-top: 10px;
padding-bottom: 10px;
width: 400px;
background : #D8F1F8;
border: 1px solid silver;
}
.mail h2 {
margin-left: 38px;
}
input {
font-size: 20pt;<
}
input:focus, textarea:focus{
background-color: lightyellow;
}
input submit {
font-size: 12pt;
}
</style>
</head>
<body>
<div class="mail">
<h2>Mail us</h2>
<ul>
<li><form name="mail" method="POST"
action="send-extra.php"></li>
<li>To:</li>
<li><input type="text" name="to"
/></li>
<li>Message:</li>
<li><input type="text" name="Message"
/></li>
<li>From:</li>
<li><input type="text" name="from"
/></li>
<li>CC:</li>
<li><input type="text" name="cc"
/></li>
<li>&nbsp;</li>
<li><input type="submit" name="submit"
value="Send"/></li>
<li>&nbsp;</li>
</form>
</ul>
</div>
</body></html>

Code of the file for handling form (code above) data:

<?php
  $to = $_POST['to'] ;    
  $message = $_POST['Message'] ;
  $from = $_POST['from'];
  $cc = $_POST['cc'];
  $headers =  "From: ".$_POST['from']."\r\n" .
"CC: ".$_POST['cc'];
  mail( $to, "Mail", $message, $headers);
  header( "Location: http://localhost/php/simple-mail.php" );
?>

We encourage you to replace the value of the parameters used in the above example and test it.

Send mail in PHP with additional parameters

You can pass additional parameters as command line options to the program configured to be used while sending mail. Those said configurations are defined by the sendmail_path configuration. One example of using additional parameter is setting the envelope sender address with -f option.

<?php
mail('[email protected]', 'Demo mail', 'Testing mail communication', null,
   '-f [email protected]');
?> 

We encourage you to replace the value of the parameters used in the above example and with your's and test it.

Previous: warning_count
Next: PHP secure mail



PHP: Tips of the Day

The ternary operator can be thought of as an inline if statement. It consists of three parts. The operator, and two outcomes. The syntax is as follows:

Example:

$value = <operator> ? <true value> : <false value>

If the operator is evaluated as true, the value in the first block will be returned (<true value>), else the value in the second block will be returned (<false value>). Since we are setting $value to the result of our ternary operator it will store the returned value.

Example:

$action = empty($_POST['action']) ? 'default' : $_POST['action'];

$action would contain the string 'default' if empty($_POST['action']) evaluates to true. Otherwise it would contain the value of $_POST['action'].

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1evaluates to true, and expr3 if expr1 evaluates to false.

It is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. ?: is often referred to as Elvis operator.

This behaves like the Null Coalescing operator ??, except that ?? requires the left operand to be exactly null while ?: tries to resolve the left operand into a boolean and check if it resolves to boolean false.

Example:

function setWidth(int $width = 0){
 $_SESSION["width"] = $width ?: getDefaultWidth();
}

In this example, setWidth accepts a width parameter, or default 0, to change the width session value. If $width is 0 (if $width is not provided), which will resolve to boolean false, the value of getDefaultWidth() is used instead. The getDefaultWidth() function will not be called if $width did not resolve to boolean false.

Refer to Types for more information about conversion of variables to boolean.