Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Attaching a User Defined .JPG to an email with Sendmail

Posted on 2005-04-12
5
Medium Priority
?
364 Views
Last Modified: 2012-06-22
Hi. Here's what I'm trying to do - I have a form that users fill out, and then click BROWSE, choose a .JPG file and have it send to me via email. This is done on my school's server, as we're a school sponsored organization, and we HAVE to use SendMail and I DON'T have the capability to store the image on the server. It needs to be put into a Session Variable or Temp  Space and sent from there.

We only want .JPG files.

I've found a code online, but it doesn't seem to work. The form sends, but no attachment comes with it. That code is posted below. A solutoin  here is worth 500pts as I need it answered quickly and well :) Note, the solution does NOT have to use the code provided, rather, whatever will work to get this done. I'm simply providing what I have as a starting point and to show what other form-fields are being sent through the email.

Thanks!

THE CODE

<?php
      if ( $_POST['hidden'] == "sent" ) {

          function sendmail ($from_name, $from_email, $to_name, $to_email, $from_hometown, $from_address, $from_state, $from_zip, $from_telephone, $from_title, $from_category, $from_class, $from_professor, $from_url, $from_description, $subject, $text_message="", $html_message, $attachment="")
          {
              
                  $from = "$from_name <$from_email>";
              $to   = "$to_name <$to_email>";
              $main_boundary = "----=_NextPart_".md5(rand());
              $text_boundary = "----=_NextPart_".md5(rand());
              $html_boundary = "----=_NextPart_".md5(rand());
              $headers  = "From: $from\n";
              $headers .= "Reply-To: $from\n";
              $headers .= "X-Mailer: Q Manning (http://www.bleutuna.com)\n";
              $headers .= "MIME-Version: 1.0\n";
              $headers .= "Content-Type: multipart/mixed;\n\tboundary=\"$main_boundary\"\n";
              $message .= "\n--$main_boundary\n";
              $message .= "Content-Type: multipart/alternative;\n\tboundary=\"$text_boundary\"\n";
              $message .= "\n--$text_boundary\n";
              $message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
              $message .= "Content-Transfer-Encoding: 7bit\n\n";
              $message .= ($text_message!="")?"$text_message":"Text portion of HTML Email";
              $message .= "\n--$text_boundary\n";
              $message .= "Content-Type: multipart/related;\n\tboundary=\"$html_boundary\"\n";
              $message .= "\n--$html_boundary\n";
              $message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
              $message .= "Content-Transfer-Encoding: quoted-printable\n\n";
              $message .= str_replace ("=", "=3D", $html_message)."\n";
              if (isset ($attachment) && $attachment != "" && count ($attachment) >= 1)
              {

                      $attfile = $attachment;
                      $file_name = ($_FILES['entryField']['name']);
                      $fp = fopen ($attfile, "r");
                      $fcontent = "";
                      while (!feof ($fp))
                      {
                          $fcontent .= fgets ($fp, 1024);
                      }
                      $fcontent = chunk_split (base64_encode($fcontent));
                      @fclose ($fp);
                      $message .= "\n--$html_boundary\n";
                      $message .= "Content-Type: application/octetstream\n";
                      $message .= "Content-Transfer-Encoding: base64\n";
                      $message .= "Content-Disposition: inline; filename=\"$file_name\"\n";
                      $message .= "Content-ID: <$file_name>\n\n";
                      $message .= $fcontent;
              }
              $message .= "\n--$html_boundary--\n";
              $message .= "\n--$text_boundary--\n";
              $message .= "\n--$main_boundary--\n";
              @mail ($to, $subject, $message, $headers);
          }
          # Example
          # Sender Name
          $from_name  = $_POST['name'];
          # Sender Email
          $from_email = $_POST['email'];
          # Recipient Name
          $to_name    = "Q Manning";
          # Recipient Email
          $to_email   = "qmanning@bleutuna.com";
          # Email Subject
          $subject    = "Best of 2thousand5 Design Annual Submission";
            # Hometown
            $from_hometown = $_POST['hometown'];
            # Current Address
            $from_address = $_POST['address'];      
            # City
            $from_city = $_POST['city'];            
            # State
            $from_state = $_POST['state'];      
            # Zip Code
            $from_zip = $_POST['zip'];            
            # Telephone Number
            $from_telephone = $_POST['telephone'];            
            # Title of Work
            $from_title = $_POST['title'];            
            # Category
            $from_category = $_POST['category'];            
            # Class Created For
            $from_class = $_POST['class'];      
            # Professor Name
            $from_professor = $_POST['professor'];                  
            # Their Website URL
            $from_url = $_POST['url'];            
            # Description of Work
            $from_description = $_POST['description'];            
          # Text Portion
            $text_message = "This is HTML email and your email client softawre ain't support HTML email.";
          # HTML Portion
          $html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
          $html_message.= "<html><head><title></title>\n";
          $html_message.= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
          $html_message.= "<style type=\"text/css\">\n";
          $html_message.= "body, td {\nfont-family: Trebuchet MS;\nfont-size: 12px;\n}\n";
          $html_message.= "</style>\n";
          $html_message.= "</head>\n";
          $html_message.= "<body><table width=\"100%\" cellpadding=0 cellspacing=0>";
            $html_message.= "<tr><td><b>Name:</b> $from_name</td></tr>";
            $html_message.= "<tr><td><b>Email:</b> $from_email</td></tr>";             
            $html_message.= "<tr><td><b>Hometowm:</b> $from_hometown</td></tr>";             
            $html_message.= "<tr><td><b>Local Address:</b> $from_address</td></tr>";             
            $html_message.= "<tr><td><b>State:</b> $from_state</td></tr>";             
            $html_message.= "<tr><td><b>Zip:</b> $from_zip</td></tr>";             
            $html_message.= "<tr><td><b>Telephone:</b> $from_telephone</td></tr>";             
            $html_message.= "<tr><td><b>Title of Work:</b> $from_title</td></tr>";             
            $html_message.= "<tr><td><b>Category:</b> $from_category</td></tr>";             
            $html_message.= "<tr><td><b>Website URL:</b> $from_url</td></tr>";             
            $html_message.= "<tr><td><b>Description:</b> $from_description</td></tr>";             
          $html_message.= "</tr></table>";
          $html_message.= "</body></html>";
      
          # Attachment Location
           $file_name = ($_FILES['entryField']['name']);
          # Execute SendMail Function
          sendmail ($from_name, $from_email, $to_name, $to_email, $from_hometown, $from_address, $from_state, $from_zip, $from_telephone, $from_title, $from_category, $from_class, $from_professor, $from_url, $from_description, $subject, $text_message, $html_message, $attachment);
      }
?>


0
Comment
Question by:bleutuna
4 Comments
 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 13768216
Could you post the form that the submits to the code above?
0
 

Author Comment

by:bleutuna
ID: 13768244
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
      <title>COUNTERFORM PRESENTS THE BEST OF 2005 DESIGN ANNUAL</title>
            <style>
                  <!--
                        /* Body Styles */
                        body {background-image:url("images/backgrounds/cf_bo2005_bgtile.gif"); margin:0px; padding:0px;}
                        
                        /* Text Styles */
                        
                        /* Background Styles */
                        
                        /* DIV Styles */
                        #counterformLogo {position:absolute; top:0px; left:0px; z-index:1000;}
                        #textTop {position:absolute; top:0px; left:109px; z-index:1000;}
                        #manStart {position:absolute; top:98px; left:0px; z-index:1000;}                        
                        #textBot{position:absolute; top:98px; left:109px; z-index:1000;}
                        #manStop {position:absolute; top:352px; left:0px; z-index:1000;}                        
                        #formSend {position:absolute; top:365px; right:16px; z-index:2000;}
                        #button {position:absolute; top:645px; right:20px; z-index:2000;}      
                        
                        /* Form Styles */
                        .textBox {background-color:#D80280; border-style:solid; border-width:1px; border-color:#009FDA; font-size:10px; color:#ffffff; font-family:din engrschift, arial narrow, arial, helvetica, sans-serif; text-align:right; padding-right:5px; text-transform:uppercase;}
                        submit {background-color:#F9E500; border-style:solid; border-width:1px; border-color:#D80280; font-family: din engscrhrift, arial narrow, arial, helvetica, sans-serif; font-size:10px; color:#D80280;}
                                                            
                  ->
            </style>
</head>

<body>
<form action="annual.php" method="post">
<div id="counterformLogo"><img src="images/structure/cf_bf2005_topleft_01.gif" width="109" height="98" hspace="0" vspace="0" border="0" alt="Counterform, The Graphic Design Club" /></div>
<div id="textTop"><img src="images/structure/cf_bf2005_topright_01.gif" width="325" height="98" hspace="0" vspace="0" border="0" alt="" /></div>
<div id="manStart"><img src="images/structure/cf_bf2005_topleft_02.gif" width="109" height="254" hspace="0" vspace="0" border="0" alt="" /></div>
<div id="textBot"><img src="images/structure/cf_bf2005_topright_02.gif" width="325" height="254" hspace="0" vspace="0" border="0" alt="" /></div>
<div id="manStop"><img src="images/backgrounds/cf_bo2005_bgtile_bot.gif" width="450" height="348" hspace="0" vspace="0" border="0" alt="" /></div>
<div id="formSend">
      <table border="0" cellpadding="0" cellspacing="5" cellspacing="0" align="right">
            <tr>
                  <td><input type="text" class="textBox" value="name" name="name" size="35" /></td>
                  <td><input type="text" class="textBox" value="email" name="email" size="25" /></td>                  
            </tr>
            <tr>
                  <td colspan="2" align="right"><input type="text" class="textBox" value="hometown, state" name="hometown" size="45" /></td>
            </tr>
      </table>      <br clear="all">
      <table border="0" cellpadding="0" cellspacing="5" cellspacing="0" align="right">
            <tr>
                  <td align="right"><input type="text" class="textBox" value="current street address" name="address" size="35" /></td>
                  <td align="right"><input type="text" class="textBox" value="city" name="city" size="20" /></td>                  
            </tr>      
      </table> <br clear="all">
      <table border="0" cellpadding="0" cellspacing="5" cellspacing="0" align="right">      
            <tr>
                  <td><input type="text" class="textBox" value="state" name="state" size="5" /></td>
                  <td><input type="text" class="textBox" value="zip" name="zip" size="10" /></td>                  
                  <td><input type="text" class="textBox" value="telephone" name="telephone" size="25" /></td>                                    
            </tr>
      </table>      <br clear="all">
      <table border="0" cellpadding="0" cellspacing="5" cellspacing="0" align="right">
            <tr>
                  <td align="right"><input type="text" class="textBox" value="title of work" name="title" size="35" /></td>                  
                  <td align="right">
                        <select class="textBox" name="category">
                              <option value="">select category</option>
                              <option value="identity">identity design</option>
                              <option value="illustration">poster design</option>                                                            
                              <option value="package">package design</option>                              
                              <option value="poster">poster design</option>                              
                              <option value="print">print design</option>
                              <option value="photography">photography</option>                              
                              <option value="typography">typography design</option>                              
                              <option value="web">web design</option>                              
                              <option value="misc">miscellaneous</option>                                                            
                        </select>
                  </td>                                    
            </tr>
            <tr>
                  <td align="right"><input type="text" class="textBox" value="class created for" name="class" size="25" /></td>
                  <td align="right"><input type="text" class="textBox" value="professor's name" name="professor" size="25" /></td>                  
            </tr>
            <tr>
                  <td colspan="2" align="right"><input type="text" class="textBox" value="your website url" name="url" size="45" /></td>
            </tr>            
            <tr>
                  <td colspan="2" align="right">
<textarea class="textBox" rows="3" cols="50" name="description">
DESCRIPTION OF WORK
</textarea>                  
                  </td>
            </tr>
      </table><br clear="all">
      <table border="0" cellpadding="0" cellspacing="5" cellspacing="0" align="right">      
            <tr>
                  <td colspan="2" align="right"><input type="file" class="textBox" value="" name="entryFile" size="45" /> <input type="hidden" name="hidden" value="sent" /></td>
            </tr>
      </table>
</div>
<div id="button"><input type="image" src="images/structure/cf_bf2005_button.gif" width="130" height="48" hspace="0" vspace="0" border="0" alt="send entry" /></div>
</form>
</body>
</html>
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 2000 total points
ID: 13773583
It's much easier to get multipart email working right using PHPMailer. This code has several problems which may be stopping it working:

For RFC2822 compliance, headers should be separated using \r\n, not just \n. Some email clients will accept it, but that doesn't make it right.

$message .= "Content-Type: application/octetstream\n";

should be:

$message .= "Content-Type: application/octet-stream\n";

Though strictly speaking, if you're sending a jpeg image, it should be 'image/jpeg', which may allow things like inline rendering in email clients.

This is wrong too:

$message .= "Content-Disposition: inline; filename=\"$file_name\"\n";

should be:

$message .= "Content-Disposition: attachment; filename=\"$file_name\"\n";
0
 
LVL 14

Expert Comment

by:huji
ID: 15989391
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Squinky {http:#13773583}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question