/    Sign up×
Community /Pin to ProfileBookmark

Attatchment for a formmail php script…

Is it possible to use allow a user from my site to attatch an image to an email they can send through my website? My server supports perl but will not allow the images to be stored there by the user they must be sent as an email to my inbox, is this possible? and if so what is onvolved?

to post a comment
PHP

29 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 01.2005 — whenever I send attachments I upload them, attach them then remove them from my server(this is apparently done once a week by an automated program, not that I trust it...)

have you searched the PHP manual for any advise?
Copy linkTweet thisAlerts:
@bokehApr 01.2005 — This will get you started but you will have to customise it to your needs. Change the fifth line of the email.php script so it contains your email address.

Put the two files in the same directory and call 'email.php' to try it out.

form.php
[code=php]
<div id="form">
<form action="email.php" enctype="multipart/form-data" method="post" onsubmit="
document.getElementById('container0').style.display='';
document.getElementById('container2').style.display='none';
progress();
return true;">
<fieldset id="fieldset">
<label for="name">Contact name<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($name)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="name" type="text" name="name" value="<?php print "$name"; ?>" tabindex="1" />
<br />
<label for="email">E-mail address<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="email" type="text" name="email" value="<?php print "$email"; ?>" tabindex="1" />
<br />
<label for="confirm_email">Confirm e-mail<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($confirm_email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="confirm_email" type="text" name="confirm_email" value="<?php print "$confirm_email"; ?>" tabindex="1" />
<br />
<label for="subject">Subject<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($subject)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="subject" type="text" name="subject" value="<?php print "$subject"; ?>" tabindex="1" />
<br />
<label for="comments">Comments<span style="color: red;">*</span>:</label>
<textarea <?php if (!empty ($hidden)) { if (empty ($comments)) { print 'style="background: pink;"'; } } ?>class="form_elements" id="comments" name="comments" cols="19" rows="5" tabindex="1"><?php print "$comments"; ?></textarea>
<br /><br />
<label for="fileatt">Attach document:</label>
<input id="fileatt" type="file" name="fileatt" tabindex="1" />
<br />
<input type="hidden" name="hidden" value="1" /><br />
<label for="submit"><span style="color: red;">*</span> Compulsory fields.</label>
<input id="submit" type="submit" value="Send" tabindex="1" />
</fieldset>
</form>
</div>
[/code]


email.php
[code=php]
<?php

ob_start();

$to = '[email protected]';

$name = $_POST['name'];
$email = $_POST['email'];
$confirm_email = $_POST['confirm_email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$hidden = $_POST['hidden'];
$from = $email;

print ('
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Email</title>
<script type="text/javascript">
/*<![CDATA[*/
function progress(){
intWidth = parseInt(document.getElementById("container1").style.width) + 1;
if(intWidth <= 400){
document.getElementById("container1").style.width = intWidth+"px";
}else{
document.getElementById("container1").style.width = 0;
}
setTimeout("progress()",300);
}
/*]]>*/
</script>
</head>

<body>
');

//Make sure email and confirm email are the same
if (!empty ($hidden)) {
if ($email == $confirm_email) {
}else{
$email = '';
$confirm_email = '';
}
}


//Do a reg_ex check on the email
if (!empty ($hidden)) {
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
if (eregi($regexp, $email))
{
}else{
$email = '';
$confirm_email = '';
}
}
// End of email checking




if (empty ($hidden)) {

print ('<div id="container2">
<h1 id="content_h1"><a name="text"> E-mail </a></h1> <p>Please use the following form to e-mail us:</p>

');

include ("form.php");

print ('</div>

<div id="container0" style="display: none;">

<p style="font-size: 15pt; font-family: sans-serif; color:#fd6700; background:#fff;">
Loading...
</p>

<div id="container1" style="width:0px; height:5px; background-color:#fd6700; margin-top:0px; text-align: left;"></div>

<p>Please be patient while your data is processed. This may take a few moments especially if you are uploading a file.</p>

</div>
');

}

if (!empty ($hidden)) {

if ($_FILES['fileatt']['error'] == 1){
print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1>
<p>The maximum file size that can be uploaded using this form is 2 megabytes.
</p>');

}elseif ( (!empty ($name)) && (!empty ($email)) && (!empty ($comments))&& (!empty ($subject))) {

// Get html message content
$form_data = "<p>This email is from <span class="bold">$name</span> nn ";
$form_data .= "<p>$comments</p>";

$message = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" n" .
" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> n" .
"<html xmlns="http://www.w3.org/1999/xhtml"> n" .
"<head> n" .
" <meta http-equiv="content-type" content= n" .
" "text/html; charset=iso-8859-1" /> n" .
"<style type="text/css"> n" .
"body { font-size: 9pt; font-family: verdana, sans-serif; color: #000; background:#fff; } n" .
".bold { font-weight: bold; } n" .
"</style> n" .
"</head> n" .
"<body>$form_data n" .
"</body> n" .
"</html> nn";

// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "nMIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";

// Add a multipart boundary above the html message
$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" .
$message . "nn";


// Base64 encode the file data
$data = chunk_split(base64_encode($data));

//We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:

// Add file attachment to the message
$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";
}else{
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "nMIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";

// Add a multipart boundary above the html message
$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" .
$message . "nn";
}


//That completes the modifications necessary to accommodate a file attachment. We can now send the message with a quick call to mail:

// Send the message
mail($to, $subject, $message, $headers);

$body = "Dear $name, nnThank you for your email. We will contact you as soon as possible regarding the matter. n n";

mail ($email, "Re: $subject", $body, 'From:you@your_email.com');

print ('<h1 id="content_h1"><a name="text"> Thank you </a></h1> <p>We will contact you as soon as possible. You will receive an automatic e-mail immediately confirming the reception of your email.</p>');

}else{
print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1> <p>Please fill in all the compulsory fields correctly and then resubmit the form. Thank you.</p>');
include ("form.php");
}
}

// This is the end of the insert

print ('

</div>
</body>
</html>
');

ob_end_flush();

?>
[/code]


Hope this helps
Copy linkTweet thisAlerts:
@BigMoosieauthorApr 02.2005 — thankyou very much for your reply, but i should have stated that perl is all that my server will allow, I hope it was not too much trouble finding that code...

EDIT: hmmm... perhaps putting this post in the PHP forum was not a good idea then now that i think about it...
Copy linkTweet thisAlerts:
@jamie1183May 25.2006 — I've just found this great post you put up, I have been trying for days to get a script that does this, I am trying to add more fields into the form (alot more) but keep getting errors, can you help me, which script do I add into etc, i have another form already created and want to add my other form to this one.

Still learning PHP and Forms etc so any help would be greatly appricated.

Thankyou in advance

Jamie Jackson
Copy linkTweet thisAlerts:
@NogDogMay 25.2006 — I've just found this great post you put up, I have been trying for days to get a script that does this, I am trying to add more fields into the form (alot more) but keep getting errors, can you help me, which script do I add into etc, i have another form already created and want to add my other form to this one.

Still learning PHP and Forms etc so any help would be greatly appricated.

Thankyou in advance

Jamie Jackson[/QUOTE]

I recommend using [url=http://phpmailer.sourceforge.net/]this PhpMailer class[/url] for doing emails with attachments.
Copy linkTweet thisAlerts:
@TinaBananaOct 11.2006 — script provided by bokeh works great
Copy linkTweet thisAlerts:
@bokehOct 11.2006 — I recommend using [url=http://phpmailer.sourceforge.net/]this PhpMailer class[/url] for doing emails with attachments.[/QUOTE]It is my understanding that PHPMailer is no longer maintained.

Check out [URL=http://sourceforge.net/projects/swiftmailer]Swift mailer[/URL], which is another Sourceforge project.

Swift is a fully OOP Library for sending e-mails from PHP websites and applications. Swift is comparable to PHPMailer except that it implements an extremely flexible & innovative plugin system. The interface for Swift is both tighter and more intuitive.[/QUOTE]
Copy linkTweet thisAlerts:
@TinaBananaOct 27.2006 — it seems that these variables need initialized or they cause an error on strict servers like mine.

$name = $_POST['name'];

$email = $_
POST['email'];

$confirm_email = $_POST['confirm_email'];

$subject = $_
POST['subject'];

$comments = $_POST['comments'];

$hidden = $_
POST['hidden'];

error produced.

Notice: Undefined index: name in /var/www/vhosts/domain.com/httpdocs/distinct/email.php on line 9

Notice: Undefined index: email in /var/www/vhosts/domain.com/httpdocs/distinct/email.php on line 10

Notice: Undefined index: confirm_email in /var/www/vhosts/domain.com/httpdocs/distinct/email.php on line 11

Notice: Undefined index: subject in /var/www/vhosts/domain.com/httpdocs/distinct/email.php on line 12

Notice: Undefined index: comments in /var/www/vhosts/domain.com/httpdocs/distinct/email.php on line 13

Notice: Undefined index: hidden in /var/www/vhosts/domain.com/httpdocs/distinct/email.php on line 14



so how do i initialize them?
Copy linkTweet thisAlerts:
@bokehOct 27.2006 — it seems that these variables need initialized[/QUOTE]I tested the code you sent me and it ran fine.
Copy linkTweet thisAlerts:
@TinaBananaOct 27.2006 — did you test them on your server?

some servers like mine are more strict and will post these notices, even tho the script works fine, which is does. my server spits out these notices because the script is using variables that are not initialized. now i dont know much about php but if you google the error i get you will get hundredes of pages stating the same thing.

so i imagine the simple solution is just to initialize the variables, but i dont know how to do that, how would i do that?

see the errors

http://klearz.com/distinct/email.php
Copy linkTweet thisAlerts:
@bokehOct 27.2006 — did you test them on your server?[/QUOTE]Sorry! I wrote that code a long time ago before I switched to testing everything E_ALL. Remove:[code=php]$name = $_POST['name'];
$email = $_POST['email'];
$confirm_email = $_POST['confirm_email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$hidden = $_POST['hidden'];
$from = $email;[/code]
replace with:[code=php]$keys = array('name', 'email', 'confirm_email', 'subject', 'comments', 'hidden');
foreach($keys as $key)
{
$$key = isset($_POST[$key]) ? $_POST[$key] : null ;
}[/code]
Copy linkTweet thisAlerts:
@bokehOct 27.2006 — Just in case you wanted it... Here's some CSS that will give you a good start with that form:[CODE]label,input,textarea{
width: 130px;
float: left;
margin:0 0 5px 0;
}

label {
clear: left;
margin-left: 0px;
text-align: right;
padding-right: 10px;
}[/CODE]


[upl-file uuid=65d6ce6a-edd3-43db-b720-10e5a581f50b size=12kB]outcome.jpg[/upl-file]
Copy linkTweet thisAlerts:
@TinaBananaOct 28.2006 — thx for the css, looks was the least of my worries for now.

this is getting soooooooooo frustrating. its almost like my server is changing everyday!!!

the first time i tried this script i got NO errors. and it worked great.

next time i play with it, i get the errors, but the script still works.

now with or without the changes you gave (which do get rid of the errors) the script doesnt work at all anymore. it makes noooooo sense! i dont get any emails anymore. and once again took the script above and copied it fresh and it doesnt work ?

im gonna have to look at it again tomorow
Copy linkTweet thisAlerts:
@TinaBananaNov 11.2006 — it almost seems like once i use the same email more then once for the email field in the form, it stops working. is there anything in the script that would cause that? what about using an email that doesnt exist?
Copy linkTweet thisAlerts:
@TinaBananaNov 12.2006 — what do i do with this ?

$from = $email;

you removed it from the first block but didnt add it to the second.
Copy linkTweet thisAlerts:
@bokehNov 12.2006 — Can you post the whole script so I can run it.
Copy linkTweet thisAlerts:
@TinaBananaNov 12.2006 — code doesnt fit in here

http://www.jahpul.com/distinct/distinct.zip

the only thing i altered was adding 2 extra field and using 2 forms on the same page. as mentioned the second script with no attach works great.

the other doesnt send the email.
Copy linkTweet thisAlerts:
@TinaBananaNov 15.2006 — any luck?

i just did another test, uploaded it on a new server.

first time i submit it, it works great, for both forms.

then after that it stops working.

so it must just be a matter of resetting it somehow?
Copy linkTweet thisAlerts:
@ThereminFeb 21.2007 — works good for me thanks bokeh ?

www.recordingjunkie.com/form.php

spent about 8 solid hours working on tons of different forms trying to get it to work. I should just come straight here. This place usually has the stuff that works in the end anyhow ?

S!
Copy linkTweet thisAlerts:
@gift2womenMar 16.2008 — Back in #3, Bokeh posted a great code for attachments. Unfortunately, something on my server is making mine work improperly - I only receive the emails that have attachments, if I do not make an attachment, I receive nothing. I am looking for a little assistance, my hosting company will modify my server to [I]whatever[/I], but I need to know what [I]whatever[/I] is. If anyone is using the code successfully, and would like to help out someone who knows nothing about PHP, please query your server with the following command and let me know the output:

strings which php | grep ./configure | sed s#"'"#""#g

I really appreciate any help I can get on this matter. Thanks.
Copy linkTweet thisAlerts:
@gift2womenMar 18.2008 — Okay, apparently there is an easier way to request what I have requested above:

If anyone has Bokeh's script working on their server (receiving email with and without attachments), can you create a PHP file on your site with the following single line:

<?php phpinfo(); ?>

And either let me know where I can access the file (domain/file) or the contents of the e-mail so that I can stop asking about this on here. Thanks.
Copy linkTweet thisAlerts:
@matts12290Jul 31.2008 — I need a little help. I want the page to redirect after it sends. I want it to come before the page that says: Thank you. We will contact you as soon as possible... Can it redirect to like redirect.html or something?

Thanks.
Copy linkTweet thisAlerts:
@knightmanNov 04.2008 — hello! any chance to make this work with 2 more files, not just one?
Copy linkTweet thisAlerts:
@rdingdangDec 02.2008 — Hello,

I am recieveing the following error while using the code from bokeh:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, [email][email protected][/email] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Apache/1.3.33 Server at njwebsite.net Port 80

I have attached both the page with the form and the php mailer to the zip document.

I am rather new at this so if you can, please help!
Copy linkTweet thisAlerts:
@Pr0digyMar 31.2009 — Hi,

I know this is an old post but the script Bokeh gave works perfect and i have 2 questions about it. I would like to set it so just one specific file type can be attached (.spx) and where can i specifically state the maximum file size ? I am by no means a coder so any (detailed) help with this is greatly appreciated ?

I hope this old post gets picked up.

Thanks,

Jay
Copy linkTweet thisAlerts:
@gkornbluthAug 09.2009 — Has anyone been able to modify bokeh's script to accept more than one upload file?

Thanks

Jerry K
Copy linkTweet thisAlerts:
@bokehAug 09.2009 — Has anyone been able to modify bokeh's script to accept more than one upload file?

Thanks

Jerry K[/QUOTE]

What problem are you having modifying it? If you post your mods someone will be able to tell you where you are going wrong.
Copy linkTweet thisAlerts:
@mike_bFeb 08.2010 — i think this thread is amazing, i have edited it to my need but im stuck with one little thing. Hopefully some genius can help me?!?

There is a file limit on the attachment of 2mb and i need it to be 5mb does anyone know how i can increase it to 5mb????

love the person who can help me out.

thanks if you can ?
Copy linkTweet thisAlerts:
@RezadinApr 24.2010 — Hi there. Awsome thread! It works perfectlly for me at my site!

As for the problem with the file size, i don't have any troubles uploading files bigger than the 2 MB. Maybe the problem is at your email client.

Regards!
×

Success!

Help @BigMoosie 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...