如何在php中发送带有电子邮件的附件

时间:2017-07-04 11:38:49

标签: php html

我的电子邮件内容包含data =>姓名,电子邮件,电话,说明和附件。 我可以将包含所有数据的电子邮件发送到电子邮件。但我想要的是,实际上我想发送附件到电子邮件内容。但在我的代码中,我只是将链接发送到电子邮件内容。但如果我点击该链接,则不会下载。我需要发送附件到电子邮件,当我点击它应该下载。如何解决这个问题。请有人帮助我。

以下是代码,

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

1 个答案:

答案 0 :(得分:0)

您可以像这样将文件附加到正文

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