1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

function sendMail() {

    require './mailer/class.phpmailer.php';
    $mail = new PHPMailer;

    $emails = file('emails.txt');
    $output = '';
    
    foreach($emails as $email) {

        $mail->IsSMTP();
        $mail->SMTPDebug = 2;           //enable debugging. 1 is client messages, 2 is client and server messages.
        $mail->Host = 'smtp.server.mail';
        $mail->Port = 25;

        $mail->From = '[email protected]';
        $mail->FromName = 'No Reply';
        $mail->AddAddress($email);                //HERE, I'm trying to use the foreach loop to run this entire code separately for each email address in the $emails array. It only emails the first address (first line) in the text file.
        $mail->AddReplyTo('[email protected]');

        $mail->WordWrap = 70;
        $mail->IsHTML(true);

        $mail->Subject = 'Weekly Production Report';
        $mail->Body = '<p>HTML email body</p>';

        $mail->AltBody  = 'plaintext email body';

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

            $output .= '<p>Message could not be sent to: ' . $email .  '<br />' .  $mail->ErrorInfo . '</p>';
            break;

        } else {

            $output .= '<p>Message sent to ' . $email . '!</p>';

        }
    }
    return $output;
}