PHP Send email with attachments – sendmail form with attachments
February 3, 2009 69 CommentsPHP email form with attachments
php email form that can send attachments
Sending a plain email is extremely easy. mail function provided in php does everthing you need behind the seen. Only problem can occur when you need to attach one or multiple files with email using your script.
php email form that can send attachments
Now you are ready !
———————————————————
<?php
/* Mailer with Attachments */
$action = $_REQUEST['action'];
global $action;
function showForm() {
?>
<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<p>Recipient Name: <input name="to_name" size="50" />
Recipient Email: <input name="to_email" size="50" />
From Name: <input name="from_name" size="50" />
From Email: <input name="from_email" size="50" />
Subject: <input name="subject" size="50" />
Message: <textarea name="body" rows="10" cols="50"></textarea>
Attachment: <input type="file" name="attachment" size="50" />
<input type="submit" value="Send Email" /></p>
<?php
}
function sendMail() {
if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$to_name = stripslashes($_POST['to_name']);
$from_name = stripslashes($_POST['from_name']);
$subject = stripslashes($_POST['subject']);
$body = stripslashes($_POST['body']);
$to_email = $_POST['to_email'];
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
$fp = fopen($attachment, "rb"); //Open it
$data = fread($fp, filesize($attachment)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
}
//Let's start our headers
$headers = "From: $from_name<" . $_POST['from_email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['from_email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['from_email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
// send the message
mail("$to_name<$to_email>", $subject, $message, $headers);
print "Mail sent. Thank you for using the PE Mailer.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style="css" type="text/css">
<!--
body {
margin: 0px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
a {color: #0000ff}
-->
</style>
</head>
<body>
<?php
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
</body>
</html>
———————————————————
Software
August 24th, 2010 at 1:11 pm
I had been quite pleased to discover this website.I desired to thank you for this fantastic understand I absolutely enjoying each and every little bit of it and I’ve you bookmarked to check out new stuff you post.
August 24th, 2010 at 2:57 pm
You have discovered the catch to using denture adhesives. Those that work well, work a bit too well when you don’t need it to. The simpliest way to remove the upper denture is to slip a finger above the front teeth and pull down. Remove the bottom plate in the same way, although there it works best if you work with two fingers- one beneath the teeth at the corners. Actually, if you have well fitted dentures you should not need any dental adhesive to hold the plates in place. Although the dental adhesives are safe to use in the mouth from a poisoning standpoint- they are not good for the long term health of your mouth. The membranes inside your mouth still need to breath, which the adhesive will prevent. It can make you a higher risk for a mouth infection like thrush or trench mouth, and block the salivary glands. There’s no good method to remove the excess from the plates or your mouth, aside from wiping and brushing. Most of the denture cleaner tablets, those that fizz- will do an adequate job of removing the excess, but you will still want to wipe off what you can and be prepared to brush them under hot water. Honestly though, the best way to deal with slippery dentures is to return to the dentist and have them realigned. Properly adjusted dentures should stay in place on their own. That doesn’t mean you will be able to chomp into an apple, or chow down on ribs or corn on the cob the way you used to though. Having dentures means you have to adapt to new ways of biting into things, and some things you just can’t do.
August 25th, 2010 at 2:54 pm
My daughter’s doctor said I could give my daughter the Flintstones vitamins when she was only 18 months old…she’s just over 2 years old now, so she’s been taking them for a while now. She didn’t like the gummies but she likes the regular ones. Anyway, I’m sure if you gave your daughter the Flintstones, it would be fine!! And if you have any questions, why don’t you call your daughter’s doctor’s office tomorrow and ask to talk to a nurse or something! Hope I helped…
August 25th, 2010 at 5:46 pm
Pretty great post. I just stumbled upon your blog and wanted to say that I have extremely enjoyed reading your blog posts. Any way I am going to be subscribing for your feed and I hope you article once again soon.
August 26th, 2010 at 6:37 am
Do you think she’s not producing enough milk? Her milk supply will build back up between feedings. I wouldn’t worry unless the puppies don’t seem to be gaining weight. Good luck.
August 26th, 2010 at 6:44 pm
Wow! I can’t believe I have found your blog. Very useful information.
August 26th, 2010 at 10:16 pm
Can I just say what a relief to discover someone who actually knows what theyre talking about over a internet. You actually know how to bring an difficulty to light and make it important. A lot more persons must read this and understand this side on the story. I cant believe youre not much more well-liked simply because you genuinely have the gift.
August 27th, 2010 at 12:12 am
Great to be reading your website again, it’s been weeks for me. Actually this specific writing is what i have been looking for so long. I require this article in order to complete my university paper, and your post is definitely a good help. Thx, great share.
August 27th, 2010 at 8:04 pm
It’s difficult to comprehend.
August 28th, 2010 at 4:27 pm
After all, isn’t that special?
August 29th, 2010 at 4:45 am
I enjoyed reading this article. I need to learn more on this subject.. Thanks for sharing this great info.. Anyway, I am going to subscribe to your feed and I hope you post again soon.
August 30th, 2010 at 1:40 am
Keep working on your blog. I’m loving it.
August 30th, 2010 at 10:27 am
Given that we’re speaking about things in the region of PHP Send email with attachments – sendmail form with attachments | Software | PlanetEnjoy.com, Utilizing your host’s mailing list applications has the advantage that it’s free of charge (or rather, you will have already paid for it as part of your web hosting service package) and it’s (typically) ad-free (ie, no adverts are inserted). Dependant upon the software program your host makes use of, you also have quite excellent control over the list: you can add subscribers, remove them, prohibit selected subscribers, customize the verification messages, and so forth. Additionally, the automated system also frees you from the drag of manually adding and eliminating subscribers.
August 30th, 2010 at 11:28 am
As we’re on PHP Send email with attachments – sendmail form with attachments | Software | PlanetEnjoy.com, With a 3rd party provider, you do not have to bother about running the list itself. Stuff like bounced mail, subscriptions and subscription cancellations are maintained by them. A few of the services also archive the old issues of the newsletters automatically.
August 30th, 2010 at 4:33 pm
Take a moment and check that out.
August 30th, 2010 at 5:03 pm
Three-quarters of Americans believe their will last over the long term and has changed pretty much every aspect of my life over the past decade.
August 30th, 2010 at 7:42 pm
You sure do know what youre referring to. Man, this web site is just fantastic! I cant wait to read more of what youve obtained to express. Im genuinely happy that I came across this when I did simply because I was genuinely starting to have bored with the whole blogging scene. Youve turned me around, man!
August 31st, 2010 at 12:25 am
Could I make a suggestion? I think youve got some thing great right here. But what if you added a few links to a site that backs up what you are stating? Or maybe you can give us some thing to look at, something that would connect what youre saying to something real? Only a proposition.
August 31st, 2010 at 4:36 am
I, professedly, should want to acknowledge.