2017-07-02 60 views
0

我正在使用phpMailer发送电子邮件。当我附加$body电子邮件正在发送与html身体,但附件不被发送。然后我删除$body并设置一些文本到正文($email->Body = 'abcd'),并罚款,附件和正文发送。 我不能同时使用$email->Body = $body;$email->AddAttachment("img/".$file_name);无法在phpMailer上发送附件()

这是我的代码:

<?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('[email protected]'); 
$email->AddAttachment($escaped_url."/img/".$file_name); 
return $email->Send(); 
?> 

文件上传工作正常,我上传了文件,并正确地存储它img文件夹中。

+1

_Just a note:_您的html正文中的这一行:'

+0

Your title talks about an issue with the attachments, whereas you might be looking for a solution for an externally linked image. Please clarify. An alternate to what you are looking for can be embedding the images in the email body so no need to worry about any image being moved from the server. The only drawback is that the email size will increase. –

+0

i have updated my post with and added absolute paths for images.but same issue.i can't send attachment – Tje123

回答

0

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

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

你必须这样使用。

+0

i have updated my post with and added absolute paths for images.but same issue. – Tje123

+0

You are using singe quote http://prntscr.com/fqpgez, PHP variable not work inside single quore. –

+0

$body.='' 您可以像这样使用。 –