PHP mail() function is used to send email in PHP. You can send text messages, HTML messages, and attachments with a message using PHP mail() function.
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
mail($to,$subject,$message,$headers,$parameters);
Parameter | Description |
---|---|
to | Required. Specifies the receiver/receivers of the email |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters |
message | Required. Defines the message to be sent. Each line should be separated with an LF ( ). Lines should not exceed 70 characters. Windows note: If a full stop is found at the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot: |
headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF ( ). Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file. |
parameters | Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option) |
Note: Each line of the message should be separated with a CRLF ( ) and lines should not be larger than 70 characters.
$additional_headers (optional): specifies the additional headers such as From, CC, BCC etc. Extra additional headers should also be separated with CRLF ( ).
File Name: php_mailer.php
<?php
ini_set("sendmail_from", "supprot@easytolearning.com");
$to = "easytolearning2@gmail.com";//change receiver address
$subject = "This is subject";
$message = "This is simple text message.";
$header = "From:supprot@easytolearning.com
";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
If you run this code on the live server, it will send an email to the specified receiver.
To send HTML message, you need to mention Content-type text/html in the message header.
<?php
$to = "easytolearning2@gmail.com";//change receiver address
$subject = "This is subject";
$message = "<h1>This is HTML heading</h1>";
$header = "From:support@easytolearning.com
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-type: text/html;charset=UTF-8
";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning