2017-07-02 2 views
0

J'utilise phpMailer pour envoyer un email. Quand j'ai joint $body l'email est envoyé avec le html comme corps, mais la pièce jointe n'est pas envoyée. Ensuite, j'ai supprimé $body et définir un texte à corps ($email->Body = 'abcd') et était bien, la pièce jointe et le texte du corps a été envoyé. Je ne peux pas utiliser $email->Body = $body; et $email->AddAttachment("img/".$file_name); en même temps.ne peut pas envoyer de pièce jointe sur phpMailer()

Ceci est mon code:

<?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(); 
?> 

téléchargement de fichier fonctionne bien, j'uploadé un fichier et emmagasinés dans img dossier correctement.

+1

_Juste une note: _ Cette ligne dans votre corps 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

Répondre

0

Utilisez l'URL du chemin d'accès réel dans l'attribut src.

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

Vous devez utiliser comme ceci.

+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.=' ' Vous pouvez utiliser comme ceci. –