PHPMailer - 打印上传的所有文件

时间:2017-02-24 13:51:50

标签: php phpmailer

我正在使用PHPMailer发送电子邮件,就像上传新文件的通知一样。 PHPMailer看起来像这样创建HTML电子邮件:

$mail->Body    = 'Hi!<br/>'
. '<br/>These files has been uploaded:<br/><br/>'
. '<b> ' . for($i = 0; $i < count($data['data']['metas']); $i++){$file = $data['data']['metas'][$i]["name"]; echo $file <br />;} . '<br/>'

这根本不起作用,我知道为什么。但是,我无法想象在电子邮件中以纯文本形式发送已上传的文件。

PHP中的这段代码完美无缺,但PHPMailer发送的HTML电子邮件却没有。

for($i = 0; $i < count($data['data']['metas']); $i++){$file = $data['data']['metas'][$i]["name"]; echo $file <br />;}

任何想法如何解决这个问题,所以我可以发送一封包含所有文件的电子邮件?

2 个答案:

答案 0 :(得分:0)

如何使用电子邮件发送文件:

    require_once '../class.phpmailer.php';
error_reporting(E_ERROR & ~E_NOTICE | E_STRICT);     
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  // Add files
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

答案 1 :(得分:0)

问题在于您构建$mail->Body字符串的方式。请尝试以下方法;

$mail->Body = 'Hi!<br/><br/>These files has been uploaded:<br/><br/>';
for($i = 0; $i < count($data['data']['metas']); $i++) {
    $mail->Body .= $data['data']['metas'][$i]["name"] . '</br>';
}