Email Code
  Home arrow Email Code arrow Simple way to send mail wih one attach...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
EMAIL CODE

Simple way to send mail wih one attached Document.
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2004-07-12

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    In this example i developed a page through wich you can send a mail with one attached file from your webpage to a desired e-mail address. You attach the file from you harddrive. It can be developed so you can send more than one file or to more users. I'll maybe start to work on it if i'll have some spare time. :P This code is developed after "PHP Email Attachment v2 by Christoph2k". Thanks for your code, it really helped me out.

    By : drcyrus3d

    Index.html

    -------------------------------------------------

    <html>
    <head>
    <title></title>
    </head>
    <body marginwidth=4 marginheight=4 topmargin=4 leftmargin=4 bgcolor=white vlink="#0000ff" link="#0000ff">
    <form name="Attachments" method=POST action="sendmail.php" enctype="multipart/form-data">
    <input type=hidden name=box value="">
    <tr>
    <td nowrap width="1%">&nbsp;&nbsp;<b>File:</b></td>
    <td colspan=2>
    <input type=file name=source_file size=20> <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
    </tr>
    <input type=submit name=btnSubmit value=Next&gt;&gt;&gt; size=20 style="border: 1px solid #0000FF">
    </form>
    </body>
    </html>

    -------------------------------------------------

    Sendmail.php

    -------------------------------------------------

    <?php

    $ftp_server='www.yourname.com';//serverip
    $conn_id = ftp_connect($ftp_server);


    // login with username and password
    $user="username";
    $passwd="******";
    $login_result = ftp_login($conn_id, $user, $passwd);

    // check connection
    if ((!$conn_id) || (!$login_result)) {
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name";
    die;
    } else {
    echo "<br>Connected to $ftp_server, for user $user<br>";
    }
    //directorylike /www.yourname.com/upload/upload
    ftp_chdir($conn_id, "public_html/upload/upload/");

    //here we create the unique name for the uploaded file so that it wont overwrite an existing file
    $uniqueID = uniqid("");
    $destination_file= $uniqueID.".doc";

    echo ("<br>");
    print $destination_file;
    echo ("<br>");

    // upload the file
    $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

    // check upload status
    if (!$upload) {
    echo "FTP upload has failed!";
    } else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

    // close the FTP stream
    ftp_close($conn_id);
    ?>
    <html>
    <head>
    <title></title>
    </head>
    <Body>
    <form name="form1" method="post" action="mail.php">
    <p>
    <input name="id" type="text" id="id" value="<?php echo $destination_file; ?>">
    </p>
    <table width="75%" border="1">
    <tr>
    <td><div align="right">Name:</div></td>
    <td><input name="nume" type="text" id="nume"></td>
    </tr>
    <tr>
    <td><div align="right">Surnume:</div></td>
    <td><input name="prenume" type="text" id="prenume"></td>
    </tr>
    <tr>
    <td><div align="right">E- mail: </div></td>
    <td><input name="email" type="text" id="email"></td>
    </tr>
    <tr>
    <td><div align="right">Aplication for : </div></td>
    <td><input name="post" type="text" id="post"></td>
    </tr>
    </table>
    <p>
    <input type="submit" name="Submit" value="Next &gt;&gt;&gt;">
    </p>
    </form>
    </Body>
    </html>

    -------------------------------------------------

    Mail.php

    -------------------------------------------------

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <style type="text/css">
    <!--
    .style17 { font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    }
    -->
    </style>
    </head>
    <body>
    <p>
    </p>
    <?php
    //to be successfull in useing this code you need to create a directory called upload
    //on you ftp create a directory upload in wich copy the content of the zip file

    //$filleant takes the value of the picture that was jut uploaded with the unique name to the ftp in the www.yourname.com/upload/upload
    $fileatt = "upload/".$_POST['id']; // Path to the file
    $fileatt_type = "application/octet-stream"; // File Type
    //here i made the file that will be sent as attachment to have the name "CV_name_surname.doc" you can make it what format you like,
    //i needed the doc format... and i'll modify this code to accept just doc file later...i'm really tired right now :D
    $fileatt_name = "CV_".$_POST['nume']."_".$_POST['prenume'].".doc"; // Filename that will be used for the file as the attachment

    //$email_from is the variable that gets the value, of the From: field that will appear in your received mail
    $email_from = $_POST['nume']." ".$_POST['prenume']; // Who the email is from

    //Here you define the subject of you message
    $email_subject = "CV."; // The Subject of the email

    //here you define the body of the message, the message itself
    //you can modify the "post" textfield in sendmail.php to a textarea....
    $email_message = $_POST['post']; // Message that the email has in it

    //here you enter the e-mail address to wich you want the message to be sent
    $email_to = "yourname@anywhere.com"; // Who the email is too

    //adds the e-mail address of the sender
    $headers = "From: ".$_POST['email'];

    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message . "\n\n";



    /********************************************** First File ********************************************/

    //$filleant takes the value of the picture that was jut uploaded with the unique name to the ftp in the www.yourname.com/upload/upload
    $fileatt = "upload/".$_POST['id']; // Path to the file

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

    //here i made the file that will be sent as attachment to have the name "CV_name_surname.doc" you can make it what format you like,
    //i needed the doc format... and i'll modify this code to accept just doc file later...i'm really tired right now :D
    $fileatt_name = "CV".$_POST['nume']."_".$_POST['prenume'].".doc"; // Filename that will be used for the file as the attachment

    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);


    $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: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}\n";
    unset($data);
    unset($file);
    unset($fileatt);
    unset($fileatt_type);
    unset($fileatt_name);

    /********************************************** End of File Config ********************************************/

    // To add more files just copy the file section again, but make sure they are all one after the other! If they are not it will not work!

    $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!");
    }
    ?>
    <p>&nbsp;</p>
    </body>
    </html>

    -------------------------------------------------

    Hope it will help you!

    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Email Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    Be the first to hear about i5/OS V6R1!

    Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br />
    FREE! Go There Now!


    NEW! Achieving True Agility -- How process can change the behavior of your tools

    Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
    FREE! Go There Now!


    NEW! Addressing software-as-a-service challenges using Tivoli security and WebSphere solutions

    Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base.
    FREE! Go There Now!


    NEW! Don't wait! Try the Rational Application Developer (RAD) v7.5 open beta code today

    Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems.
    FREE! Go There Now!


    NEW! Hello World: Monitor a simple business process using WebSphere Business Monitor V6.0.2

    This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product.
    FREE! Go There Now!


    NEW! Implement new security capabilities in DB2 9.5, Part 1: Understanding roles in DB2 9.5

    IBM DB2 9.5 provides new options for tighter security, and allows for more granularity and flexibility in administration of the database. This tutorial is the first of two tutorials that cover roles and trusted contexts. Follow the exercises in this tutorial, and learn how to take advantage of the new DB2 feature roles in combination with other essential e-business technologies such as Web services, Web application server, and DB2 database server.
    FREE! Go There Now!


    NEW! Implement new security capabilities in DB2 9.5, Part 2: Understanding trusted contexts

    IBM DB2 9.5 provides new options for tighter security, and allows for more granularity and flexibility in administration of the database. This tutorial is the second of two tutorials that cover roles and trusted contexts. Follow the exercises in this tutorial, and learn how to take advantage of the new DB2 feature trusted contexts in combination with other essential e-business technologies such as Web services, Web application server, and DB2 database server.
    FREE! Go There Now!


    NEW! Rational Modeling Extension for Microsoft.Net

    Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    EMAIL CODE ARTICLES

    - Basic Ajax contact form
    - Random validation image
    - tinySendMail
    - SaferMail 0.7
    - Smtp Auth Email Script
    - Search Mime Email Structure
    - PHP Text / HTML Email with Attachments 2.1
    - Simple way to send mail wih one attached Doc...
    - Generic POP3 Class
    - PHP Text / HTML Email with Unlimited Attachm...
    - Another SendMail
    - email with attachment
    - Get Email Addresses from Strings
    - EmailCode
    - Email Validation - Gone Wild


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway