PHP Mail

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

PHP mail() function

Syntax

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

$to: specifies receiver or receivers of the mail. The receiver must be specified one of the following forms.

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User <user@example.com>
  • User <user@example.com>, Another User <anotheruser@example.com>

$subject: represents subject of the mail.

$message: represents message of the mail to be sent.

Note
Note: Each line of the message should be separated with a CRLF ( \r\n ) 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 ( \r\n ).

PHP Mail Example

File: mailer.php
snippet

If you run this code on the live server, it will send an email to the specified receiver.

PHP Mail: Send HTML Message

To send HTML message, you need to mention Content-type text/html in the message header.

snippet
This is HTML heading";

   $header = "From:xyz@example.com \r\n";
   $header .= "MIME-Version: 1.0 \r\n";
   $header .= "Content-type: text/html;charset=UTF-8 \r\n";

   $result = mail ($to,$subject,$message,$header);

   if( $result == true ){
      echo "Message sent successfully...";
   }else{
      echo "Sorry, unable to send mail...";
   }
?>

PHP Mail: Send Mail with Attachment

To send message with attachment, you need to mention many header information which is used in the example given below.

snippet
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +