Here we will learn how to send emails using swift mailer , few steps you have to follow first you have to install swift mailer sdk via composer then include the autoload.php file in your project.


install swiftMailer

Create composer.json file and paste this code and open command window and execute command composer update

{
    "require": {
        "swiftmailer/swiftmailer": "^6.0"
    }
}

Then open command window and execute this this query



This command will download all your necesarry packages about swiftmailer 



Then Create the index.php file

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body style="background: #ccc;">
        <form action="" method="post" style="font-size: 25px;background: #fff;padding: 40px;width: 400px;margin: 100px auto;">
            <h2>Swift Mailer</h2>
            <?php
            if (isset($_POST['email'])) {

                require_once './vendor/autoload.php';

// Create the Transport
                $transport = (new Swift_SmtpTransport('krishnarathor.com', 25))
                        ->setUsername('questions@krishnarathor.com')
                        ->setPassword('1N@wUtL8eZ@t')
                ;

// Create the Mailer using your created Transport
                $mailer = new Swift_Mailer($transport);

// Create a message
                $message = (new Swift_Message('Please subscrible my channel'))
                        ->setFrom(['questions@krishnarathor.com' => 'Tech. Rudranshi'])
                        ->setTo([$_POST['email'] => 'gajanand'])
                        ->setBody('Thanks for subscribing us ,Here is the message to email.');


                if ($mailer->send($message) == TRUE) {
                    echo "Email sent to " . $_POST['email'];
                }
            }
            ?>


            <input type="text" name="email" placeholder="Enter your email"><input type="submit" value="Mail Me" >
        </form>
    </body>
</html>


Add your smtp username and password it will work


Thanks.....

Download source code

Are you facing problems in understanding this article? download source code now