/    Sign up×
Community /Pin to ProfileBookmark

Email attachment

I have created a html form containing fields like name, age , gender, qualification etc along with file attachment field and a submit button.

So I need a php code for file attachment.

When user clicks on submit button, the values in name, age , gender, qualification field and file attached should go to an email account called as ‘[email protected]‘.

So how to write php code for this file attachment.

Regards.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehJul 04.2005 — 

So how to write php code for this file attachment.

Regards.[/QUOTE]

Its really easy! Grab the file from the $_FILES array, base 64 encode it. Then construct a multi part mime email and add the base 64 encoded data as one of the mail parts. It's as simple as that!
Copy linkTweet thisAlerts:
@j_vijayanandJul 19.2007 — This code will allow you to send an email using PHP and include an attachment with it! Its really simple to use!

By : Christoph2k

<?php

$fileatt = ""; // Path to the file

$fileatt_type = "application/octet-stream"; // File Type

$fileatt_name = ""; // Filename that will be used for the file as the attachment

$email_from = ""; // Who the email is from

$email_subject = ""; // The Subject of the email

$email_txt = ""; // Message that the email has in it

$email_to = ""; // Who the email is too

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');

$data = fread($file,filesize($fileatt));

fclose($file);

$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "nMIME-Version: 1.0n" .

"Content-Type: multipart/mixed;n" .

" boundary="{$mime_boundary}"";

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

"--{$mime_boundary}n" .

"Content-Type:text/html; charset="iso-8859-1"n" .

"Content-Transfer-Encoding: 7bitnn" .

$email_message . "nn";

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

$email_message .= "--{$mime_boundary}n" .

"Content-Type: {$fileatt_type};n" .

" name="{$fileatt_name}"n" .

//"Content-Disposition: attachment;n" .

//" filename="{$fileatt_name}"n" .

"Content-Transfer-Encoding: base64nn" .

$data . "nn" .

"--{$mime_boundary}--n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {

echo "<font face=verdana size=2>The file was successfully sent!</font>";

} else {

die("Sorry but the email could not be sent. Please go back and try again!");

}

?>
×

Success!

Help @psn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...