KX - PHP Email Notifications

PHPMailer is a class library that allows you to send emails within PHP. It was more efficient than using the standard php mail() function because it easily allowed emails to include attachments with one line of code. Remember to include the require statement at the top of the file that will use PHPMailer.

PHPMailer can be downloaded here: https://github.com/PHPMailer/PHPMailer (Links to an external site.)

We used PHPMailer to send emails to notify students that they’ve been accepted into a program, that they’ve accepted their offer and need to fill out the attached form, and to send students a reset password link.


Here is an example of how we used it: (STUDENT_accept_decline_offer.php)

while ($row=$query->fetch(PDO::FETCH_ASSOC))

{

$recipient = $row['email'];

}

$mail = new PHPMailer;

            $mail->From = "$from";  //Sender's email address

            $mail->FromName = "$from_name"; //Senders name

            $mail->Body = "$msg"; //Email message

            $mail->Subject = "$subject"; //Email subject

            $mail->addAttachment('./Filename.pdf');

            $mail->AddAddress($recipient);  //Recipient’s email address

                       

            if(!$mail->send())

            {

                        echo 'Email sent to:' . $recipient . '<br/ >';

                        echo "Mailer Error: " . $mail->ErrorInfo;

            }


Online Example:

<?php

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->setFrom('from@example.com', 'Your Name');

$mail->addAddress('myfriend@example.net', 'My Friend');

$mail->Subject  = 'First PHPMailer Message';

$mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';

if(!$mail->send()) {

  echo 'Message was not sent.';

  echo 'Mailer error: ' . $mail->ErrorInfo;

} else {

  echo 'Message has been sent.';

}