2017-07-04 1 views
1

J'ai un contenu e-mail avec des données => nom, e-mail, téléphone, description et pièce jointe. Je peux envoyer l'email avec toutes les données à l'email. Mais ce que je veux, en fait, je veux envoyer des pièces jointes au contenu de l'e-mail. Mais dans mon code, je viens d'envoyer les liens vers le contenu de l'e-mail. Mais si je clique sur ce lien, il ne télécharge pas. J'ai besoin d'envoyer des pièces jointes à l'e-mail et aussi quand je clique dessus, il devrait télécharger. Comment résoudre ceci. Plese que quelqu'un m'aide.Comment envoyer des pièces jointes avec email en php

Ci-dessous le code,

$name = $this->request->post['name']; 
$email = $this->request->post['email'];     
$description = $this->request->post['description']; 
$phone = $this->request->post['phone']; 
$upload_base_dir="image/service-images/"; 
$upload_time_dir=time(); // setup directory name 
$upload_dir = $upload_base_dir.$upload_time_dir; 

if (!file_exists($upload_dir)) { 
mkdir($upload_dir, 0775, true); //create directory if not exist 
} 
foreach($_FILES['attachment']['error'] as $k=>$v) 
{ 
$uploadfile = $_FILES['attachment']['name'][$k]; 
move_uploaded_file($_FILES['attachment']['tmp_name'][$k],$upload_dir."/".$uploadfile); 
$subject = 'Service Request'; 

$email_header ='<html style="color: rgb(34, 34, 34); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); max-width: 575px; line-height: 18px; font-size: 12px; font-family: Arial, Helvetica, sans-serif;"><head><meta name="viewport" content="width=device-width" /><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body bgcolor="#FFFFFF" text="#000000" style="color: rgb(34, 34, 34); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); max-width: 575px; line-height: 18px; font-size: 12px; font-family: Arial, Helvetica, sans-serif;"><table style="font-family:Helvetica,Arial,sans-serif;background:#EFEDED;" cellpadding="0" cellspacing="0" bgcolor="#EFEDED" border="0" width="100%"><tbody><tr><td align="center"><table width="96%" cellpadding="0" cellspacing="0"border="0"><tbody><tr><td style="border-top:5px solid #1e96d3; background:#fff;margin:0; padding:20px; border-spacing:0px;">'; 

$email_footer = '</td></tr><tr><td style="margin:0; padding:15px 0;"><table style="font-family:Arial, Helvetica,sans-serif;font-size:11px;color:#999999;" cellpadding="0" cellspacing="0" border="0" width="100%"><tbody> <tr> 
<td style="font-family:Arial, Helvetica,sans-serif; margin:0; font-size:11px; padding:8px 0 0 0;">This email was intended for Inf. </td></tr><tr> 
<td style="font-family:Arial, Helvetica,sans-serif; margin:0; font-size:11px; padding:8px 0 0 0;"></td></tr></tbody></table></td></tr></tbody></table></td> 
</tr></tbody></table></body></html>'; 

$body = $email_header.'<table width="100%" cellpadding="0" cellspacing="0"><tr> 
<td style="margin:0; padding:0px 0px 15px 0px; border-spacing:0px;"> 
<p style="font-size:14px; color: rgb(0, 0, 0); font-family: Arial, Helvetica, sans-serif; font-weight: bold; line-height: 1.5em; margin: 0px; padding: 0.4em; text-align: left;">'.$subject.'</p></td></tr><tr><td style="margin:0; padding:0px 0px 15px 0px; border-spacing:0px;"><p style="color:#000; font-size:13px; margin:0; font-family:Arial, Helvetica,sans-serif;"><strong>Dear Sir/Madam,</strong><br></p></td></tr><tr><td style="margin:0; padding:0 0 5px 0;"><p style="font-size:13px; background-color: rgb(234, 234, 234); color: rgb(0, 0, 0); font-family: Arial, Helvetica, sans-serif; font-weight: bold; line-height: 1.5em; margin: 0px; padding: 0.4em; text-align: left;">Details:</p></td></tr><tr><td style="margin:0; padding:0px 0px 15px 0px; border-spacing:0px;"><table style="font-family:Helvetica,Arial,sans-serif; font-size:12px; font-weight:bold; margin-top:10px; width:100%"><tbody> 
<tr><td style="padding:4px 0;">Name</td> 
<td>:</td><td style="font-weight:normal;">'.$name.'</td></tr> 
<tr><td style="padding:4px 0;">Email Address</td> 
<td>:</td><td style="font-weight:normal;">'.$email.'</td></tr> 
<tr><td style="padding:4px 0;">Phone</td><td>:</td><td style="font-weight:normal;">'.$phone.'</td></tr> 
<tr><td style="padding:4px 0;">Description</td><td>:</td><td style="font-weight:normal;">'.$description.'</td></tr> 
<tr><td style="padding:4px 0;">Attachments</td><td>:</td><td style="font-weight:normal;">'; 
foreach($_FILES['attachment']['error'] as $k=>$v) 
{ 
$uploadfile = $_FILES['attachment']['name'][$k]; 

move_uploaded_file($_FILES['attachment']['tmp_name'][$k],$upload_dir."/".$uploadfile); 
$images[] = $_FILES['attachment']['name'][$k]; 
$count=0; 
foreach($images as $image){ 
$count++; 
} 
if($_SERVER['HTTP_HOST'] == "localhost"){ 
$body .= '<a href='.'localhost'."/inf/".$upload_dir."/".$image.' download>Attachment'.$count.'</a>&nbsp;&nbsp;&nbsp;&nbsp;'; 
} 
if($_SERVER['HTTP_HOST'] == "192.168.1.156"){ 
$body .= '<a href='.'192.168.1.156'."/inf/".$upload_dir."/".$image.' download>Attachment'.$count.'</a>&nbsp;&nbsp;&nbsp;&nbsp;'; 
} 
if($_SERVER['HTTP_HOST'] == "floretmedia.net"){ 
$body .= '<a href='."http://floretmedia.net/temp"."/inf/".$upload_dir."/".$image' download>'.$image.'</a>&nbsp;&nbsp;&nbsp;&nbsp;'; 
}   
if($_SERVER['HTTP_HOST'] == "www.arbour.in" || $_SERVER['HTTP_HOST'] == "arbour.in"){ 
$body .= '<a href='.'http://www.arbour.in'."/".$upload_dir."/".$image.' download>Attachment'.$count.'</a>&nbsp;&nbsp;&nbsp;&nbsp;'; 
}   
} 
$body .= '</td></tr></tbody></table></td></tr></table>'.$email_footer.''; 

//echo $body;die; 

$mail = new Mail($this->config->get('config_mail')); 
$mail->setTo($this->config->get('config_email')); 
$mail->setFrom($email); 
$mail->setSender($name); 
$mail->setSubject($subject); 
$mail->setHtml($body); 
$mail->send(); 
+0

Utilisez une bibliothèque existante pour créer des e-mails avec des pièces jointes telles que PHPMailer, beaucoup plus simple, et fiable. –

Répondre

0

vous pouvez joindre le fichier au corps comme celui-ci

$body .= '</td></tr></tbody></table></td></tr></table>'.$email_footer.''; 

$filename = 'myfile'; 
$path = 'your path goes here'; 
$file = $path . "/" . $filename; 

$content = file_get_contents($file); 
$content = chunk_split(base64_encode($content)); 

$body .= $content . "\r\n"; 

// send the mail 


$mail = new Mail($this->config->get('config_mail')); 
$mail->setTo($this->config->get('config_email')); 
$mail->setFrom($email); 
$mail->setSender($name); 
$mail->setSubject($subject); 
$mail->setHtml($body); 
$mail->send();