我正在尝试构建一个自动发送的电子邮件,它将根据包含使用PHPmailer的文件相对路径的变量发送附件。文件路径存储在MYSQL的表中。我想获取使用while循环发送的每封电子邮件的文件路径。有人这样做过吗?电子邮件发送正常但没有附件,并打印出以下错误:
无法访问文件:$ filepath
有问题的代码行是第11,12和27行。感谢您的帮助。
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...
答案 0 :(得分:2)
27 $mailer->AddAttachment('$filepath', '$filename');
应该是
27 $mailer->AddAttachment($filepath, $filename);
否则你只需将“$ filepath”和“$ filename”传递给AddAtachment,而不是变量背后的实际数据。
答案 1 :(得分:0)
尝试:
$mailer->AddAttachment($filepath, $filename);