【问题标题】:can't send attachment on phpMailer()无法在 phpMailer() 上发送附件
【发布时间】:2017-12-05 21:22:10
【问题描述】:

我正在使用 phpMailer 发送电子邮件。当我附加 $body 时,电子邮件以 html 作为正文发送,但附件未发送。然后我删除了$body 并将一些文本设置为正文($email->Body = 'abcd'),很好,附件和正文已发送。 我不能同时使用$email->Body = $body;$email->AddAttachment("img/".$file_name);

这是我的代码:

<?php 
session_start(); 
$url =  "http://{$_SERVER['HTTP_HOST']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );

if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $tmp=explode('.',$_FILES['image']['name']);
      $file_ext=strtolower(end($tmp));

      $expensions= array("jpeg","jpg","png");

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }

      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"img/".$file_name);

      }else{
         print_r($errors);
      }
   }
?>
<?php

$body='<html><body>';
$body.='<img src="'.$escaped_url.'/img/img2.jpg" alt="" height="90" width="200" />'
.$_POST['date'].'';
$body .='<table rules="all" style="border-color: #666;" cellpadding="10">
<tr style="background: #6699FF;">';
$body .='<td><strong>'.$_SESSION["login_user_name"].'</strong> </td>';
$body .='<td>Income (Rs) : '.$_POST['income'].'</td>';
$body .='<td>Expences (Rs) :'.$_POST['expence'].'</td>';
$body .='<td>Balance (Rs) : '.$_POST['balance'].'</td>';
$body .='</tr>
<tr style="background: #eee;">';
$body .='<td><strong>Category</strong> </td>
<td><strong>Item Name</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Image</strong></td>
</tr>
<tr >';
$body .='<td>'.$_POST['cat'].' </td>';
$body .='<td>'.$_POST['name'].'</td>';
$body .='<td>'.$_POST['amount'].'</td>';
$body .='<td>'.$file_name.'</td>';
$body .='</tr>
</table>
</body>
</html>';
?>
<?php
require 'PHPMailer/class.phpmailer.php';
$email = new PHPMailer();
$email->From      = $_SESSION["login_email"];
$email->FromName  = $_SESSION["login_user_name"];
$email->Subject   = 'Daily Summery';
$email->Body      = $body;
$email->IsHTML(true);
$email->AddAddress( 'sample@gmail.com' );
$email->AddAttachment($escaped_url."/img/".$file_name);
return $email->Send();
?>

文件上传工作正常,我上传了一个文件并将其正确存储在img文件夹中。

【问题讨论】:

  • 请注意: html 正文中的这一行:&lt;img src="img/img2.jpg"... 将永远不会在有人打开电子邮件时显示图像。您需要在 html 电子邮件中有绝对路径。否则,路径将是用户电子邮件客户端的相对路径。
  • 您的标题涉及附件的问题,而您可能正在寻找外部链接图像的解决方案。请说清楚。您正在寻找的替代方案可以将图像嵌入电子邮件正文中,因此无需担心从服务器移动任何图像。唯一的缺点是电子邮件的大小会增加。
  • 我已经更新了我的帖子并添加了图片的绝对路径。但同样的问题。我无法发送附件
  • 您不能使用 URL 作为 AddAttachment 的来源,您必须使用绝对文件系统路径。见github.com/PHPMailer/PHPMailer/wiki/Tutorial
  • 从基础开始 - 你没有像 PHP 文档告诉你的那样处理文件上传,这是不安全的,因为你没有检查 move_uploaded_file 的返回值。将代码基于 PHPMailer 提供的 send_file_upload 示例,您可以省去一些麻烦。

标签: php phpmailer


【解决方案1】:

在 src 属性中使用真实的路径 URL。

<img src="http://example.com/img/img2.jpg" alt="" height="90" width="200" />

你必须这样使用。

【讨论】:

  • 我已经更新了我的帖子并添加了图片的绝对路径。但同样的问题。
  • 您使用的是单引号prntscr.com/fqpgez,PHP 变量在单引号内不起作用。
  • $body.='' 你可以像这样使用。
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多