Skip to main content

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; add the body in a html format.
If you do not intend to use HTML in the body then use $mail->AltBody = 'write here';

After this, now your email is fully built and ready to be sent. Lets now send it.

$mail->send(); This line of code sends the email that we have built.


Congratulations, you now have a fully fledged code that can now send an email.
Thanks for your time and hope you have been helped.

Take your time to check PHPMailer github account for more clarity and understanding.

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 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 t he 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 Comp...

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...