Send mail in PHP

In PHP, mail() function allows you to send emails directly from a script.

Syntax:
mail(to, subject, message, headers, parameters);

Parameters Details:

to: Specifies the receiver / receivers of the email
subject: Specifies the subject of the email
message: Define the message to be sent
headers: Specifies additional headers, like From, Cc, and Bcc
parameters: Specifies an additional parameter to the send mail program

This function requires three mandatory arguments that specify the recipient's email address, the subject of the message and the actual message.

Return value of mail() is,
if the mail successfully send then the return value will be TRUE.
if the mail not send then the return value will be FALSE.

Settings required for mail:
Open "php.ini" file then find the section headed [mail function]. Two things you have to change,
  • The first is called SMTP that defines your email server address. 
  • The second is called sendmail_from which defines your own email address.

e.g.:

[mail function]
; For Win32 only.
SMTP = smtp.gmail.net
; For win32 only
sendmail_from = xyz@gmail.com

Follow below code for sending mail using PHP,

<?php
$ToMailAddress="abc@gmail.com";
$Name="abc";
$FromMailAddress="xyz@gmail.com";
$to = "{$ToMailAddress}";
$subject = "Inquiry Contact Us from ". $Name;
$headers = "From:".$FromMailAddress;
$headers .= "\r\nReply-To:{$ToMailAddress}";
$headers .= "\r\nContent-Type: text/html; charset=UTF-8";
$headers .= "\r\nMIME-Version: 1.0";
if(mail($to,$subject,$m_reply,$headers))
{
   echo "Send";
}else{
  echo "Not Send";
}
?>

Post a Comment

0 Comments