- The mail() function is used to send emails directly from a script.
- For the mail functions to be available in PHP, it requires an installed and working email system where the program to be used is defined by the configuration settings in the php.ini file.
Example1
Script for sending mail:
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('[email protected]', 'My Subject', $message);
?>
Example2
Script for Sending mail with extra headers:
The addition of basic headers, tells the MUA the from and Reply-To addresses.
Syntax:
mail(to,subject,message,headers,parameters);
Parameter
|
Description
|
to
|
Mandatory. Define the receiver / receiver of the
email
|
subject
|
Mandatory. Specifies the subject of the email and
in this subject we cannot define any new line.
|
Message
|
Mandatory. Defines the message to be sent. Each
line should be separated with a LF
- line feed (\n). Lines should not exceed 70 characters.
|
headers
|
Optional. Specifies additional headers, like
From, Cc, and Bcc. The additional headers should be separated with a CRLF
(\r\n).
|
parameters
|
Optional. Specifies an additional parameter to
the send mail program (the one defined in the sendmail_path configuration
setting). (i.e. this can be used to set the envelope sender address while
using sendmail with the -f sendmail option)
|
Sample Script for sending mail using mail () function:
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'welcome to wikitechy';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Example3:
Sending mail with an additional command line parameter
- The additional_parameters parameter can be used to pass an additional parameter for the program configured to use while sending mail using the sendmail_path.
<?php
mail('[email protected]', 'the subject', 'the message', null,
'[email protected]');
?>
Example4:
Sending HTML email
In php using the mail () function we can send a HTML email
<?php
// multiple recipients
$to = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';
// subject
$subject = 'Wikitechy Event Meet';
// message
$message = '
<html>
<head>
<title>Wikitechy Event Meet</title>
</head>
<body>
<p>Here are the Wikitechy upcoming event meet in November</p>
<table>
<tr>
<th>Place</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Bangalore</td><td>3rd</td><td> November </td><td>2016</td>
</tr>
<tr>
<td>Chennai</td><td>17th</td><td> November </td><td>2016</td>
</tr>
</table>
</body>
</html>
';
// 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";
// Additional headers
$headers .= 'To: kavi <[email protected]>, arunprasath < [email protected]>' . "\r\n";
$headers .= 'From: Event Remainder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected] ' . "\r\n";
$headers .= 'Bcc: [email protected] ' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Applies to:
Related tags:
- Basic PHP mail() Function code to send emails from
- How to send an email using PHP?
- Sending Nice HTML Email with PHP
- send email in php using smtp
- send email in php code with form