How can I send e-mail on form_submit?

Download and read about PHPMailer,and use following code :

                            <?php
                        if (isset ( $_POST ['submit'] )) {
                            require 'PHPMailer/PHPMailerAutoload.php';

                            $mail = new PHPMailer ();

                            $mail->isSMTP (); // Set mailer to use SMTP
                            $mail->Host="smtp.gmail.com"; // Specify main and backup SMTP servers
                            $mail->SMTPAuth = true; // Enable SMTP authentication
                            $mail->Username="[email protected]"; // SMTP username
                            $mail->Password = 'password'; // SMTP password
                            $mail->SMTPSecure="tls"; // Enable TLS encryption, `ssl` also accepted
                            $mail->Port = 587; // TCP port to connect to

                            $mail->setFrom ( 'Fromemailaddress', 'xyz' );
                            $mail->addReplyTo ( 'toemailaddress', 'xyz' );
                            $mail->addAddress ( 'Fromemailaddress' ); // Add a recipient
                                                                      // $mail->addCC('[email protected]');
                                                                      // $mail->addBCC('[email protected]');

                            $mail->isHTML ( true ); // Set email format to HTML

                            $bodyContent="<h1>hello </h1>";
                            $bodyContent .= '<p>This is my 1st email through php</p>';

                            $mail->Subject="Email from Localhost by CodexWorld";
                            $mail->Body = $bodyContent;

                            if (! $mail->send ()) {
                                echo 'Message could not be sent.';
                                echo 'Mailer Error: ' . $mail->ErrorInfo;
                            } else {
                                echo 'Message has been sent';
                            }
                        }

                        ?>
        <form action="#" method="post">
            <input name="username" type="text"> <input name="password"
                type="password"> <input type="submit" name="submit" id="submit" />
        </form>

Leave a Comment