dougan5772 2017-07-04 11:38
浏览 61

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

I have email content with data=> name, email, phone , description and attachment. I am able send the email with all the data to email. But what I want is , Actually I want to send attachments to the email content. But in my code I just sending the links to the email content. But If I click on that links it is not downloading. I need to send attachments to the email and also when I click on that it should download. How to resolve this. Plese somebody help me.

Below is 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(); 
  • 写回答

1条回答 默认 最新

  • douzhuo6931 2017-07-04 11:45
    关注

    you can attach the file to the body like this

    $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 . "
    ";
    
    // 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();
    
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择