PHP Send email with attachments – sendmail form with attachments

February 3, 2009 69 Comments

PHP 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($fpfilesize($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>


———————————————————

Tags: , , Software

69 Responses to “PHP Send email with attachments – sendmail form with attachments”

Pages: « 1 [2] Show All

  1. 51
    Neta Ragazzo Says:

    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.

  2. 52
    Asia Tarella Says:

    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.

  3. 53
    Detra Dershem Says:

    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…

  4. 54
    full movie downloads now Says:

    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.

  5. 55
    Isaias Demuth Says:

    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.

  6. 56
    Effective Diet Says:

    Wow! I can’t believe I have found your blog. Very useful information.

  7. 57
    full movie downloads now Says:

    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.

  8. 58
    Tod Baumkirchner Says:

    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.

  9. 59
    Laurene Gitzen Says:

    It’s difficult to comprehend.

  10. 60
    Lowell Argenbright Says:

    After all, isn’t that special?

  11. 61
    chanel black tote bag Says:

    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.

  12. 62
    melt away body fat Says:

    Keep working on your blog. I’m loving it.

  13. 63
    mailing lists Says:

    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.

  14. 64
    sales leads Says:

    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.

  15. 65
    Ervin Galacio Says:

    Take a moment and check that out.

  16. 66
    Madlyn Vondran Says:

    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.

  17. 67
    Marco Gilliss Says:

    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!

  18. 68
    Cristen Baksh Says:

    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.

  19. 69
    chat groupe tranchant Says:

    I, professedly, should want to acknowledge.

Pages: « 1 [2] Show All

Leave a Reply

Powered by WP Hashcash

World Trade Center Building 7 was NOT hit by an aircraft.

9/11 Controlled Demolitions of September 11, 2001 World Trade Center Building 7 was NOT hit by an aircraft. This...

HAPPY DIWALI AND NEW YEAR, CELEBRATE Diwali

HAPPY DIWALI AND NEW YEAR TO U *. * . * . * . * . * . *...

Dipawali 2009 pooja time, Diwali Muhurats 2009, Auspicious time forLakshmi Puja

Diwali Pooja Muhurats, Auspicious time, Shubh Samay in 2009 Pushya Nakshatra October 12, 2009 is the ‘Pushya Nakshatra’ which...

Diwali sms, Free Diwali sms, Happy Diwali sms, Free Send Diwali sms

Diwali sms Diwali aai, masti chahi, rangi rangoli, Diwali aai, masti chahi, rangi rangoli, deep jalaye, Dhoom Dhadaka, chhoda...

Munna bhai Free SMS, Mobile Text Messages, Tapoti Style Akdom Zakasss

Tera bahot memory aa rela tha,Itna tem ho gela hai,tere ko dekha bhi nai,Akha life mei tere jesa 1...

Find Popular Searches in the web solutions Development Outsourcing

cheap internet marketing web design birmingham website hosting website submission ecommerce web hosting content management systems strategic internet marketing...

Some easy steps you can take to tackle a flu / swine flu virus of any kind

It is essential to remember that all kinds of viruses and bacteria can attack you when your immune system...

Mobile Phone Etiquettes – all good things should be enjoyed responsibly

We all love our mobiles… we talk, we text, we listen to music and we stay in touch.Can we...

Adsense Alternatives – available to webmasters wishing to monetize the web

Here are the Adsense Alternatives that are available to webmasters wishing to monetize the web! Adbrite AdEngage Adgenta Adhearus...

Worldwide Mobile Operator MMSC Settings – For receiving and sending MMS

Worldwide Mobile Operator MMSC Settings Setting For receiving and sending MMS From your mobile phone or GSM Modem [Albania...