Sending Email using PHPMailer

Getting Started

I've used CDN for Bootstrap in this tutorial so you need internet connection for it to work. If ever you want a full documentation of this plugin, you can visit Github.

Installing the PHPMailer using Composer

If you don't have composer yet, you may use this link to download the latest version. 1. Create a folder of your app, in my case I've created my app inside htdocs folder of my xampp and name it phpmailer. 2. Create a file named composer,json inside your app folder and put the ff. line inside it. This will tell composer to install phpmailer to our app.
  1. {
  2. "phpmailer/phpmailer": "~6.0"
  3. }
composer phpmailer 3. Open command prompt, navigate to your app and type the ff.
  1. composer require phpmailer/phpmailer
This will install PHPMailer to our app. 4. After installation, you should be able to see vendor folder.

Creating our Send Email Form

Next, we're gonna create a form to send email using PHPMailer and this will be our index.php.
  1. <?php
  2. ?>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>Sending Email using PHPMailer</title>
  8. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1 class="page-header text-center">Sending Email using PHPMailer</h1>
  13. <div class="row">
  14. <div class="col-sm-4 col-sm-offset-4">
  15. <?php
  16. if(isset($_SESSION['message'])){
  17. ?>
  18. <div class="alert alert-info text-center">
  19. <?php echo $_SESSION['message']; ?>
  20. </div>
  21. <?php
  22.  
  23. unset($_SESSION['message']);
  24. }
  25. ?>
  26. <form method="POST" action="send.php" enctype="multipart/form-data">
  27. <div class="form-group">
  28. <label>Email:</label>
  29. <input type="email" class="form-control" name="email" required>
  30. </div>
  31. <div class="form-group">
  32. <label>Subject:</label>
  33. <input type="text" class="form-control" name="subject" required>
  34. </div>
  35. <div class="form-group">
  36. <label>Message:</label>
  37. <textarea class="form-control" name="message" required></textarea>
  38. </div>
  39. <div class="form-group">
  40. <label>Add Attachment:</label>
  41. <input type="file" name="attachment">
  42. </div>
  43. <button type="submit" name="send" class="btn btn-primary">Send</button>
  44. </form>
  45. </div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>

Creating our Send Email Code

This contains our code that send email using PHPMailer and we're gonna name it as send.php.
  1. <?php
  2. // Import PHPMailer classes into the global namespace
  3. // These must be at the top of your script, not inside a function
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. use PHPMailer\PHPMailer\Exception;
  6.  
  7.  
  8. if(isset($_POST['send'])){
  9.  
  10. $email = $_POST['email'];
  11. $subject = $_POST['subject'];
  12. $message = $_POST['message'];
  13.  
  14. $filename = $_FILES['attachment']['name'];
  15. $location = 'attachment/' . $filename;
  16. move_uploaded_file($_FILES['attachment']['tmp_name'], $location);
  17.  
  18. //Load composer's autoloader
  19. require 'vendor/autoload.php';
  20.  
  21. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  22. try {
  23. //Server settings
  24. $mail->isSMTP(); // Set mailer to use SMTP
  25. $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
  26. $mail->SMTPAuth = true; // Enable SMTP authentication
  27. $mail->Username = '[email protected]'; // Your Email/ Server Email
  28. $mail->Password = 'mysourcepass'; // Your Password
  29. $mail->SMTPOptions = array(
  30. 'ssl' => array(
  31. 'verify_peer' => false,
  32. 'verify_peer_name' => false,
  33. 'allow_self_signed' => true
  34. )
  35. );
  36. $mail->SMTPSecure = 'ssl';
  37. $mail->Port = 465;
  38.  
  39. //Send Email
  40. $mail->setFrom('[email protected]');
  41.  
  42. //Recipients
  43. $mail->addAddress($email);
  44. $mail->addReplyTo('[email protected]');
  45.  
  46. //Attachment
  47. if(!empty($filename)){
  48. $mail->addAttachment($location, $filename);
  49. }
  50.  
  51. //Content
  52. $mail->isHTML(true);
  53. $mail->Subject = $subject;
  54. $mail->Body = $message;
  55.  
  56. $mail->send();
  57. $_SESSION['message'] = 'Message has been sent';
  58. } catch (Exception $e) {
  59. $_SESSION['message'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
  60. }
  61.  
  62. header('location:index.php');
  63. }
  64. else{
  65. $_SESSION['message'] = 'Please fill up the form first';
  66. header('location:index.php');
  67. }

Allowing Less Secure Apps

Lastly, in our server email, we need to allow less secure apps by logging in your server email and use this link. P.S. Dont forget to create attachment folder in our app folder. This will be the container for our uploaded attachments. Also, take note of Gmails restrictions in sending emails with attachments. That ends this tutorial, Happy Coding :)

Comments

Submitted byrodney (not verified)on Thu, 06/07/2018 - 08:47

Message could not be sent. Mailer Error: SMTP Error: Could not authenticate. What's The Problem ?

Add new comment