PHP Code to Send Email
In this tutorial, you will learn how to send email using PHP.
First of all, create a file named sample.php.
Put the following code in the sample.php.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $title = "sending Email using PHP"; $content = "How to send email using PHP."; if ( mail($send_to, $title, $content, $header)) { echo("Successfully sended email to $send_to..."); } else { echo("Failed: Sending Email ..."); } ?> |
Sending Email With HTML Form Using PHP
Here is how you can get the receiver email, title/subject, message, and sender email from the user using HTML forms.
Create a new file or do it in the same one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if (!isset($_POST["submit"])) { ?> <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>"> To: <input type="text" name="send_to"><br> From: <input type="text" name="sender_email"><br> Title: <input type="text" name="title"><br> content: <textarea rows="10" cols="25" name="content"></textarea><br> <input
type="submit" name="submit" value="Send Email"> </form> <?php } else { if (isset($_POST["send_to"])) { $send_to = $_POST["send_to"]; $sender_email = $_POST["sender_email"]; $title = $_POST["title"]; $content = $_POST["content"]; if ( mail($send_to, $title, $content, $sender_email)) { echo("Successfully sended email to $send_to..."); } else { echo("Failed: Sending Email ..."); } } } ?> |