duanfei1930 2017-08-17 12:36
浏览 45

PHPMailer - 发送带有附件的电子邮件给多个人和从MySql下载的电子邮件地址[关闭]

First, sorry for my English :) I have some problem with phpmailer. I want to send emails to multiple people with dedicated attachment to them (pdf). Email address must by downloaded from MySQL. But I dont know how can I do it. PDF file have the same name like column 'name' in datebase table. For example I have:

  • User name: XXX
  • PDF name (from ftp): XXX.pdf
  • email: somemail@example.com

this is one example, I have a lot of users with lots of pdfs.

Simplifying what I want: user XXX must receive email with XXX.pdf but user YYY must receive email with YYY.pdf.

I dont know if you understand what I mean :)

At this moment I have something like this and i dont know how to assign name to email (dynamically?) in query

$stmt = $dbh->prepare('SELECT id, name, email FROM users WHERE name ="SomeUserName" '); 
// how to assign name to email??
$stmt->execute();


$mail             = new PHPMailer(); // defaults to using php "mail()"

$mail->CharSet = 'UTF-8';

$body             = 'SOME TEXT IN BODY';



$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'name@yourdomain.com');



while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
     $mail->addAttachment("pdf/07.2017/".str_replace('.','_',$row['name']).".pdf"); 
                        $address = $row["email"];
                    }


$mail->AddAddress($address, "SOME TEXT");

$mail->Subject    = "SOME SUBJECT";

$mail->MsgHTML($body);

if(!$mail->Send()) {
    echo 'send';
} else {
  echo 'didnt send';
}
  • 写回答

1条回答 默认 最新

  • dtdvlazd56637 2017-08-17 13:14
    关注

    You need to setup all the general information before the while loop.

    Then inside your while loop you need to:

    1. Add the attachment
    2. Set the address for the receiver
    3. Send the email
    4. Clear the attachment
    5. Clear the address for the receiver

    A for assigning the variable to your MySQL query

    $stmt = $dbh->prepare('SELECT id, name, email FROM users WHERE name =:name');
    $stmt->bindParam(":name", "somename");
    

    Obviously "somename" can also be a variable like $_POST['somename'].

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序