I am trying to build an automated email that will send attachments based on a variable that contains the relative path to a file using PHPmailer. The file path is stored in a table in MYSQL. I want to fetch the file path for each email sent using a while loop. Anyone ever done this? The emails get sent fine but without the attachments and the following error is printed:
Could not access file: $filepath
The lines of code in question are lines 11, 12 and 27. Thanks for your help.
1
require("PHPMailer_v5.1 2/class.phpmailer.php");
2 while ($row = mysqli_fetch_array($result)){
3 $to = $row['email'];
4 $first_name = $row['first_name'];
5 $last_name = $row['last_name'];
6 $msg = $row['msg'];
7 $id = $row['id'];
8
9 //the code in question -lines 11 and 12:
10
11 $filepath= $row['filepath'];
12 $filename= $row['filename'];
13
14 $mailer = new PHPMailer();
15 $mailer->IsSMTP();
16 $mailer->Host = 'ssl://smtp.gmail.com:465';
17 $mailer->SMTPAuth = TRUE;
18 $mailer->Username = 'xx'; // Sender's gmail address
19 $mailer->Password = 'xx'; // Sender's gmail password
20 $mailer->From = 'xx'; // Sender's email address
21 $mailer->FromName = 'xx'; // This is the from name in the email
22 $mailer->Body = "$msg";
23 $mailer->Subject = "$id";
24 $mailer->AddAddress('xx'); // Recipient
25
26 //Applying the variables fetched from the database - can this be done?
27 $mailer->AddAttachment('$filepath', '$filename');
28
29 if(!$mailer->Send())
30 //more code...
27 $mailer->AddAttachment('$filepath', '$filename');should be
27 $mailer->AddAttachment($filepath, $filename);otherwise you just pass “$filepath” and “$filename” to AddAtachment, not the actual data behind the variables.