epsilones
epsilones

Reputation: 11609

Adding some images with PHPMailer

I am using phpmailer to send an email. The email is a template with a bunch of images. So I use the AddEmbeddedImage() method to add images. The problem is that I want to add a lots of images, how could I specify the path parameter in order to load all the images in one time ? Does AddEmbeddedImage('images/*.jpg',...) makes sense ?

For information, I instantiate $mailer = new PHPMailer(); then I use $mail->AddEmbeddedImage('img/some_image.jpg', 'image'); but I can't do that twenty times for twenty images

Upvotes: 0

Views: 526

Answers (1)

mishmash
mishmash

Reputation: 4458

You could iterate over all images in a folder and add that with a foreach loop. For example:

<?php
function get_files ($dir, $_ext = 'jpg') {
    $files = array();
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if ($file == '.' || $file == '..') continue;
                $ext = pathinfo($file, PATHINFO_EXTENSION);
                
                if ($ext == $_ext) {
                    $files[] = $file;
                }
            }
            closedir($dh);
        }
    }
    return $files;
}

/**
* You can change the second parameter so you can get other image types 
* (png, gif, etc.)
*/
$images = get_files ("/path/to/folder/of/images/");
foreach ($images as $image) {
    $mail->AddEmbeddedImage ($image, 'image');
}
?>

Directory code taken from PHP.net.

Upvotes: 1

Related Questions