Skip to main content

How to send an e-mail using php - PHPMailer Part 1


You probably want to send an email from your code, well you can use a PHP in built function mail(), but since you are here, chances are that it is either you have tried it and stuck or may be you just need a simpler way to reach your goal since the PHP mail() has a handful of limitations when it comes to making use of popular features such as: 

  • encryption 
  • authentication
  • HTML messages and 
  • attachments.
Also it needs a local mail server which is not included in Windows OS for Windows user.



Well, thanks to PHPMailer the classic email sending library for PHP, it is open source and can be can be installed through by adding this line to your composer.json file:
"phpmailer/phpmailer": "^6.2"                                                                                                  

or run

composer require phpmailer/phpmailer                                                                                     
Alternatively, if you're not using Composer, you can download PHPMailer as a zip file , then copy the contents into one of the include_path directories specified in your PHP configuration and load each class file manually.
I have downloaded the library manually but you can use any of the above steps and reach the same destination.
First, let us load the classes that we will use into a global namespace, 

ensure this is on top of your script and not inside a function.
Now load the composer's autoloader

includes and PHPMailer are my folders where vendor/autoload.php exists, you can 
change them with where the file is vendor /autoload.phpexists
After this, now we have one thing left before making your hands dirty. 
copy this code to instantiate the mailer class.

Ready now to get your hands dirty, do not forget to move to part 2
Follow and share for more tech issues.
Take your time and leave a comment. Thank you

Comments

Popular posts from this blog

How to send an e-mail using php - PHPMailer Part 3

 Being that you are here, hope you have already gone through part 1 and 2 and now set to continue with the recipients settings. Copy the code below then we go through it step by step. $ mail -> setFrom ( 'vik03@username.ac.ke' , $dept ); it has 2 options, first option is the senders email, second option is the mail header which is optional. $mail->AddAddress($result->EmailId); Add a recipient, you can also add a name as a second option but that is optional, I have stored the recipient in a variable but you can still key it in as a row text. This snippet have the variables used as part of the mail build up, PHP concatenation and conditions have also played a part and trust me the library understands all this. $ mail -> Subject = $msg ; here is where the email subject is written. Since we had already set the email format to HTML above by including this line, $ mail-> isHTML ( true ); we can continue to add the email body now $mail->Body = $body; a...

How to send an e-mail using php - PHPMailer Part 2

 If you have gone through part 1 and done everything step by step then you are now ready to continue.  We had already created an instance as shown below 👇 After instantiation, now you are ready to create a mail inside your code. Copy the code below: Let us now go through it step by step. $ mail -> isSMTP (); is to allow you send the mail using SMTP. $ mail-> SMTPAuth = true ; this is to enable SMTP authentication. $ mail-> SMTPSecure = 'tls' enable encryption, there are at least two types of encryption used by PHPMailer, PHPMailer:: ENCRYPTION_STARTTLS and TLS encryption. $ mail -> Port = 587 ; TCP port to connect to, that is if you have used TLS encryption, if you have opted for`PHPMailer::ENCRYPTION_SMTPS` then use port 465 $ mail -> isHTML ( true ); Sets an email format to HTML $ mail -> Username = 'vik03@username.ac.ke' ; SMTP username, hosts email address $ mail -> Password = 'secret...