PHP | Sending E-mail With Example

PHP | Sending E-mail With Example

PHP uses the Simple Mail Transmission Protocol (SMTP) to send mail.

Settings of the SMTP mail can be done through "php.ini" file present in the PHP installation folder.

Any text editor will be used to open and edit  "php.ini" file

Locate [mail function) in the file

PHP mail() Function:

PHP mail is an inbuilt PHP function which is used to send emails from PHP scripts.

It is a cost-effective way of notifying users of important events.

The user gets to contact you via email by providing a contact us form on the website that emails the provided content.

It can also be used to send an email, to your newsletter subscribers, password reset links to users who forget their passwords, activation/confirmation links, registering users and verifying their email addresses
Syntax :
mail( to, subject, message, headers, parameters ); The mail function accepts the following parameters:


Parameters
Descriptions
to
Required. Specifies the receiver or receivers email ids.
subject
Required. Specifies the subject of the email.This parameter cannot contain any newline characters.
message
Required. Defines the message to be sent. Each line should be separated with a (\n). Lines should not exceed 70 characters.
headers
Descriptions Optional Specifies additional headers, like From, Cc, and Bcc.
parameters
Optional. Additional parameters to the send mail can be mentioned in this section.

Example of mail() in PHP:
<!DOCTYPE html>
<html>
<head>
 <title>PHP | Sending E-mail With Example</title>
</head>
<body>
<?php
$to="shivamhande0000@gmail.com";
$sub="Learn PHP";
$msg="<br>HEY Shivam, Want to learn PHP</br>";
$header="From:business@cwipedia.in \r\n";
$retvalue=mail($to,$sub,$msg,$header);
if($retvalue===true)
{
 echo "Message is Sent";
}
else
{
 echo "Message is not sent";
}
?>
</body>
</html>


Comments