how to send an email using php ?

In this article, We’ll see how to send an email using php?

before sending an email, we’ll read about mail function.




php mail function used to send an email, it takes require three parameter i.e recipient’s email address, subject of the email and message. There are other two optional parameters.

Below the syntax of mail function.

   bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

let’s see example

Example1: Send an email with extra headers:

	$to      = "user@example.com";
	$subject = "Mail Subject";
	$txt     = "Welcome to nexladder.com!";
	$headers = "From: no-reply@example.com" . "\r\n" .
	"CC: admin@example.com";

	mail($to,$subject,$txt,$headers);

Example2: Send an html email:

$to      = "user@example.com";
$subject = "Php html email";

$msg     = "<html>
			<head>
				<title>Php Html email</title>
			</head>
			<body>
				
<table>
					
<tr>
						
<th>Tutorial Site</th>

					</tr>

					
<tr>
						 
<td>nexladder.com</td>

					</tr>

				</table>

			</body>
			</html>";

// 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: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: user@example.com' . "\r\n";

mail($to, $subject, $msg, $headers);

That’s it!. Please share your thoughts or suggestions in the comments below.

Posted in PHP

Leave a Reply

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