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

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'].