无法在phpMailer()上发送附件

时间:2017-07-02 13:48:47

标签: php phpmailer

我使用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文件夹中。

1 个答案:

答案 0 :(得分:0)

在src属性中使用真实路径网址。

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

你必须这样使用。