Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending email with pre determined attachment

Status
Not open for further replies.

ckennerdale

Programmer
Dec 23, 2000
158
GB
I have a form to email script which uses the mail() function

It successfulyy sends attachmentsi if the form has a 'file' element in which allows the user to select a file from their local hard drive.

what i would like to do is send an email with an attachmnet that I designate- ie like a virtual postcard.

I have sent the mail script the location of the file i want to attach both relative and absolute. But the attachment does not come thorough

does anyone have any expereince with this?

my code is below

<?



// * global variables received from a form or third party application via HTTP POST

global $mail_to, $mail_from, $mail_body_orig1, $mail_title, $file_name;


// * get time from server to give email unique id

$mail_boundary = md5(uniqid(time()));

//* mail header attributes including multipart email type and id


$mail_headers .= &quot;From: $mail_from \n&quot;;
$mail_headers .= &quot;MIME-Version: 1.0\r\n&quot;;
$mail_headers .= &quot;Content-type: multipart/mixed;&quot;;
$mail_headers .= &quot;boundary= \&quot;$mail_boundary\&quot;&quot;;
$mail_headers .= &quot;\r\n\r\n&quot;;

//* mail body attributes

$new_mail_body .= &quot;--$mail_boundary\n&quot;;
$new_mail_body .= &quot;Content-type: text/plain; charset=us-ascii\r\n&quot;;
$new_mail_body .= &quot;Content-transfer-encoding: 7bit\r\n\r\n&quot;;
$new_mail_body .= $mail_body_orig1;
$new_mail_body .= &quot;\r\n&quot;;

// new 23/10/01
$new_mail_body .= $file_name;
$new_mail_body .= &quot;\r\n&quot;;
//

//* mail attachment attributes including MIME type


$new_mail_body .= &quot;--$mail_boundary\n&quot;;
$fp = fopen($file_name, &quot;r&quot;);
$file = fread ($fp, filesize ($file_name));
$file = chunk_split(base64_encode($file));

$mime_type = $file_name_type;

$new_mail_body .= &quot;Content-type:$mime_type; name=\&quot;$file_name\&quot;\r\n&quot;;
$new_mail_body .= &quot;Content-transfer-encoding:base64\r\n\r\n&quot;;
$new_mail_body .= $file;
$new_mail_body .= &quot;\r\n\r\n&quot;;

$new_mail_body .= &quot;--$mail_boundary--&quot;;


//* convert variable if required for the mail() function and return

$mail_body = $new_mail_body;
$mail_subject = $mail_title;

return mail($mail_to , $mail_subject, $new_mail_body, $mail_headers);


?> Caspar Kennerdale
Senior Media Developer
 
I've been meaning to post this FAQ, this email script handles attachments and is very handy!

faq434-1203
 
I actually solved the problem- the asset I was trying to attach (ie the one stored on a server) was not on the same server as the php script (this server did not have php nstalled or the appropriate permissions to to fopen.

So everything was actually working- I just need to move the assets to my server Caspar Kennerdale
Senior Media Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top