【问题标题】:Adding some images with PHPMailer使用 PHPMailer 添加一些图像
【发布时间】:2012-12-30 00:05:35
【问题描述】:

我正在使用 phpmailer 发送电子邮件。电子邮件是带有一堆图像的模板。所以我使用 AddEmbeddedImage() 方法来添加图像。问题是我想添加很多图像,如何指定路径参数以便一次加载所有图像? AddEmbeddedImage('images/*.jpg',...) 有意义吗?

关于信息,我实例化 $mailer = new PHPMailer(); 然后我使用 $mail->AddEmbeddedImage('img/some_image.jpg', 'image'); 但对于 20 张图像我不能这样做 20 次

【问题讨论】:

  • 你当前的代码是什么?
  • 点击问题下方的“编辑”更新内容,方便大家阅读。

标签: php image phpmailer


【解决方案1】:

您可以遍历文件夹中的所有图像并使用 foreach 循环添加它。例如:

<?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');
}
?>

目录代码取自PHP.net

【讨论】:

  • 太棒了!我虽然认为它可以通过一些特定的 PHPmailer 方法简单地完成
  • 当你可以自己做的时候,处理目录解析和添加多个文件真的不是 PHPMailer 的工作。它只会增加不必要的复杂性 IMO。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多