Mail form

Discussion in 'Programming' started by William, Oct 8, 2008.

  1. #1
    Hi
    How can I create a mail form that allow people to send emails with picture attachements to via stmp mail. (I don´t allow"nobody" to send mails)

    thanks in advance
     
    William, Oct 8, 2008 IP
  2. crivion

    crivion Guest

    Best Answers:
    0
    #2
    Just make a input file field on the form and upload the give file to a server!
     
    crivion, Oct 8, 2008 IP
  3. William

    William Well-Known Member

    Messages:
    1,310
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    140
    #3
    I do not want to upload the file to the server but rather have it being sent as an email.

    (also pleaselnow that I am a complete noob and don´t know how to"just" ;-) )
     
    William, Oct 8, 2008 IP
  4. nyxano

    nyxano Peon

    Messages:
    417
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What programming language are you attempting this in? Most programming languages allow the file to be uploaded to the server and then deletes it once it is done with it... that way, you don't bog down your server with a bunch of useless files.
     
    nyxano, Oct 8, 2008 IP
  5. William

    William Well-Known Member

    Messages:
    1,310
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    140
    #5
    I would prefer to use PHP (perhaps temporary storing the file in q mysql DB).

    I prefer to have as little as possible stored on the server to avoid the risk of egg drops and other malicious code being uploaded.
     
    William, Oct 8, 2008 IP
  6. nyxano

    nyxano Peon

    Messages:
    417
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I know extremely little about PHP so I have no idea if this code works or not but could be worth a try. A quick Google search brought me to http://icrontic.com/forum/showthread.php?t=7019 which had the following post.

    Although the post is five years old, should be enough to get your started. Maybe someone with more PHP knowledge could shed some light on whether this code is useful or useless.

    
    <?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 MyNewName5333 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>
    
    PHP:
     
    nyxano, Oct 8, 2008 IP