English 中文(简体)
How to send email with attachment using PHP?
原标题:

I am using following code for sending email with attachment but the proper file is not getting attach with mail.

$UnidID = $_COOKIE[ UniqueID ];
$guid = $_COOKIE[ guid ];
$target_path = "userdata/".$UniqueID."/".$iGuid."/Outputs";
$fname = getpathmail($UnidID,$guid);
$target_path = $target_path.$filname;

$fileatt_type = "application/fbf"; // File Type
$fileatt_name = $fname; 
$data = $target_path;
$email_from = "EHPAdmin@fugro.in"; 
$email_subject = "EHP/PPP process";
$email_message = "Processed result for EHP/PPP processing";

$email_to = $_GET[ Email ] ; 

$headers = "From: ".$email_from;

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "
MIME-Version: 1.0
" .
"Content-Type: multipart/mixed;
" .
" boundary="{$mime_boundary}"";

$email_message .= "This is a multi-part message in MIME format.

" .
"--{$mime_boundary}
" .
"Content-Type:text/html; charset="iso-8859-1"
" .
"Content-Transfer-Encoding: 7bit

" .
$email_message .= "

";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}
" .
"Content-Type: {$fileatt_type};
" .
" name="{$fileatt_name}"
" .
"Content-Transfer-Encoding: base64

" .
$data .= "

" .
"--{$mime_boundary}--
";

$ok = @mail($email_to, $email_subject, $email_message, $headers);
问题回答

I d suggest to use a library for sending eMails as it will handle all the header related stuff. Have a look at Zend_Mail. Sending attachments is as easy as

$mail = new Zend_Mail();

$at = $mail->createAttachment($myImage);
$at->type        =  image/gif ;
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding    = Zend_Mime::ENCODING_8BIT;
$at->filename    =  test.gif ;

$mail->send();

You need to put all of your multipart message into $email_message:

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers = "From: ".$email_from;
$headers .= "
MIME-Version: 1.0
" .
"Content-Type: multipart/mixed;
" .
" boundary="{$mime_boundary}"";

$email_message .= "This is a multi-part message in MIME format.

" .
"--{$mime_boundary}
" .
"Content-Type:text/html; charset="iso-8859-1"
" .
"Content-Transfer-Encoding: 7bit

" .
"--{$mime_boundary}
" .
"Content-Type: {$fileatt_type};
" .
" name="{$fileatt_name}"
" .
"Content-Transfer-Encoding: base64

" .
chunk_split(base64_encode($data)) .
"--{$mime_boundary}--
";

May be you missed this in your code

 ...

 $file = fopen($data, rb );
 //saves content in $data itself
 $data = fread($file,filesize($data));
 fclose($file); 

 ...

should work havn t executed myself. give it a try

You should give your attachment file as:

"Content-Disposition: attachment; filename= /path/to/the/file/filename ";




相关问题
Angle brackets in php

I want to store angle brackets in a string in PHP because i want to eventually use mail() to send an HTML email out. The following is the code that doesn t seem to work. while(...) { $msg .= "<...

authlogic auto_register feature using my options

I have auto registration working with authlogic using gaizka s version of authlogic_openid which I found on Github since pelle s original addition of the feature seemed to cause issues. http://...

Zend 邮件问题,涉及外国char子+ com子

泽斯德邮局在名称被定为具有外国性质(如“保”)和 com(”)的物品时,就放弃了一种例外(因为邮局(邮局)退回假)。 重新提出以下守则。

How to track an email in Java?

How I can track an email? I m using java on the server side for sending emails. I want to track whether it is delivered , opened, etc... How I can do that ?

Web Link in a mail is not rendering as link in yahoo

string from = "abc@gmail.com"; string to = "xyz@gmail.com,xyz@yahoo.co.in"; string password="abcxyz"; MailMessage mail = new System.Net.Mail.MailMessage(); mail.To.Add(to); mail.From = new ...

SharePoint - Approaching Website Storage Limit Email

How can i go about changing the distribution list as well as the email text for the email that goes out to site collection admin when a site collection approaches it s size limit? Thanks for your ...

How to create an email mailing list

Im creating a coming soon page for a website im developing, and im adding an option for the user to enter their email address so we can email them when the site is up. How do I do this?

CCNet email does not include MSBuild results

We re using CCNet 1.4.4.83 but when an MSBuild task fails, we don t get the MSBuild results (i.e. missing file or whatever reason the compile failed) in the email notification. I do see the build ...

热门标签