How to send Mail Using PHP
How to send the mail using mail() function in PHP
In this tutorial I will explain how to send mail using PHP mail function.The mail() function allows you to send emails directly from a script.
PHP Mail Function
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 a LF (\n). Lines should not exceed 70 characters.Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot: <?php $txt = str_replace(“\n.”, “\n..”, $txt); ?> |
headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).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) |
First, create a HTML form with three fields
- Subject
- Message
Index.php
1 2 3 4 5 6 7 8 9 10 11 |
<p style="color:#FF0000; font-size:12px;"> <?php echo $_SESSION['msg'];?><?php echo $_SESSION['msg']="";?></p> <form name="mail" action="" method="post"> Email :<input type="email" name="email" value="" class="form-control" required /> Subject :<input type="text" name="subject" value="Demo from PHP GURUKUL Programing Blog" class="form-control" readonly /> Message : <textarea name="message" rows="6" class="form-control" readonly> This is sample text for testing. From : PHP GURUKUL Programing Blog </textarea> <input type="submit" value="Send" name="send" class="btn-primary" /> </form> |
PHP CODE put this code top on the index.php page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php if($_POST['send']) { $email=$_POST[email]; $fromAddr='PHP Gurukul Programing Blog <www.phpgurukul.com>'; // the address to show in From field. $recipientAddr = $_POST[email]; $subjectStr = 'PHP Gurukul demo from Website'; $mailBodyText = <<<HHHHHHHHHHHHHH <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Demo From Website</title> </head> <body> <table> <tr> <td style="width:150px;"><b>Subject:</b></td> <td><b>:</b></td> <td style="text-transform:capitalize;">$_POST[subject]</td> </tr> <tr> <td><b>Message</b></td> <td><b>:</b></td> <td style="text-transform:capitalize;">$_POST[message]</td> </tr> </table> </body> </html> HHHHHHHHHHHHHH; $headers= <<<TTTTTTTTTTTT From: $fromAddr MIME-Version: 1.0 Content-Type: text/html; TTTTTTTTTTTT; mail($recipientAddr,$subjectStr,$mailBodyText,$headers); //echo "($recipientAddr,$subjectStr,$mailBodyText,$headers)"; $_SESSION['msg']="Your Information has been send sucessfully..."; } ?> |