Dear Reader, let us know that, “How to send mail using mail function in PHP“. So in this Tutorial, We Learn Step By Step Mail Sending Process using mail() function in PHP.
Mail Sending functionality is very important for any Organisation to notify any user to send information Immediately.
In PHP mail() function allows us to send email directly from a script. The mail() function accepts the no. of parameters like
i) Email
ii) Subject
iii) Message
iv) CC Email or BC Email
In the above Parameters Email, Subject and Message Parameters are Mandatory and Rest of Parameters are Optional.
Lets first we see a demo by click on bellow “Demo” Button
Step 1:- Now we need to create index.php file in our project like “C:\xampp\htdocs\php_demo\mail\index.php” and paste below code
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 41 42 43 44 |
<?php include('../header.php'); if(isset($_REQUEST['send'])) { $to = $_REQUEST['email']; $subject = "This is Test Mail from Web Preparations"; $message = "<b class='alert alert-warning'>How to send mail in php using PHP mail function</b>"; $message .= "<p>“The best preparation for tomorrow is doing your best today.” No one is born successful, success requires preparation .So prepare yourself online at very ease...</p>"; $message .="<a href='http://www.webpreparations.com'><button type='submit' name='send' class='btn btn-info' style='background-color:#449D44; color:#fff; font-weight:bold;height:50px; border:1px;'>Click Her for Visit Web Preparations</button></a>"; $header = "From:test@webpreparations.com \r\n"; $header .= "Cc:test@webpreparations.com \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "<div class='alert alert-success'> Message sent successfully...</div>"; }else { echo "<div class='alert alert-danger'>Message could not be sent...</div>"; } } ?> <html> <head> <title>send mail using mail function in php</title> </head> <body> <div class="container"> <h2>Mail sending form of Web Preparations</h2> <form action="" method="POST"> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control" id="email" placeholder="Please enter your email" name="email"> </div> <button type="submit" name="send" class="btn btn-info">Send Mail</button> </form> </div> </body> </html> |
Step 2:- Now run your project, so we need to type in browser localhost/your_project_name/mail/
like
http://localhost/php_demo/mail/ then you can see the result as in below snapshot, you can Send mail By click on Send Mail button
Congratulations you have successfully Completed Mail Sending Process using mail() function in PHP, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below and don’t Forget Like and Follow us on Social Media, Thanks.