Jump to content

Please help sending email attachments


lingo5

Recommended Posts

Hi,

I have done this script that sends emails without attachments.

What I want to do now is add the functionality to attach multiple files using one single field, kind of what Outlook does...you can select one or more files to attach to your email.

Any ideas how I can do this?

Thanks

<?php

$EmailTo = Trim(stripslashes($_POST['to']));
$Subject = Trim(stripslashes($_POST['subject']));
$email = Trim(stripslashes($_POST['email']));



// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers

$headers .= 'From: My company<info@mycompany.com>' . "\r\n";
$headers .= "Cc: {$_POST['cc']}\r\n";
$headers .= "Bcc: {$_POST['bcc']}\r\n";


// prepare email body text
$Body = "";
$Body .= $email;
$Body .= "\n";
$Body .= "\n";


// send email 
$success = mail($EmailTo, $Subject, stripslashes($Body), $headers);

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../PC_email_compose.php?emailsentok=true\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../PC_email_compose.php?emailsenterror=true\">";
}
?>

Link to comment
Share on other sites

Have you tried:

 

<?php
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$body	 = "This is a multi-part message in mime format.\n\n";
$body	.= "--$mime_boundary\n";
$body	.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body	.= "Content-Transfer-Encoding: 7bit\n\n";
$body	.= $text;
$body	.= "\n\n";
$body	.= "--$mime_boundary\n";
$body	.= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body	.= "Content-Transfer-Encoding: 7bit\n\n";
$body	.= $html;
$body	.= "\n\n";
$body	.= "--$mime_boundary--\n";
$headers .= "From:\"$mymailtitle XXXX\" <$mymail>\r\n";
$headers .= "X-Sender-IP: $_SERVER[sERVER_ADDR]\r\n";
$headers .= 'Date: '.date('n/d/Y g:i A')."\r\n";
return mail($EmailTo, $Subject, $body, $headers); ?>

 

Don't forget to add the $text and $html for the format of your mail in each.

Link to comment
Share on other sites

Oooops, my mistake. I should have read your post properly. Somehow i thought you wanted to send an email with image attachments too.

 

Do you mean adding images as an attachment to the email? If so then the code i gave you should work for a mail server like outlook that accepts images as attachments. You will need to add in the html code for your mail and place it in the $html code above the line "$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';" with what i gave you.

 

Can you provide the html form you have?

Link to comment
Share on other sites

thanks again, here's my form:

 

<form action="utilities/sendEmail.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="850" border="0" align="center" cellpadding="8" cellspacing="0">
    <tr>
      <td height="524"><table width="100%" border="0" cellspacing="0" cellpadding="10">
        <tr>
          <td><table width="100%" border="0" cellspacing="1" cellpadding="6">
            <tr>
              <td width="8%" align="right" valign="middle" class="NoResultsTXT">
                to:                </td>
              <td width="92%" valign="middle"><input name="to" type="text" class="EmailFieldTXT" id="to" value="<?php echo $row_email_send_RS['cliente_email']; ?>" size="70" /></td>
            </tr>
            <tr>
              <td align="right" valign="middle" class="NoResultsTXT">CC:                </td>
              <td valign="middle"><input name="cc" type="text" class="EmailFieldTXT" id="cc" size="70" /></td>
            </tr>
            <tr>
              <td align="right" valign="middle" class="NoResultsTXT">BCc:                </td>
              <td valign="middle"><input name="bcc" type="text" class="EmailFieldTXT" id="bcc" size="70" /></td>
            </tr>
            <tr>
              <td align="right" valign="middle" class="NoResultsTXT">subject:                </td>
              <td valign="middle"><input name="subject" type="text" class="EmailFieldTXT" id="subject" size="70" /></td>
            </tr>
            </table></td>
        </tr>
      </table>
        <table width="100%" border="0" cellpadding="10" cellspacing="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="6">
              <tr>
                <td height="54">
                  <input name="attachments[]" class="multi" type="file"/>
                </td>
              </tr>
              <tr>
                <td>
                  <textarea name="email" id="email">
</textarea>
<script>
			CKEDITOR.replace( 'email',
			{
			language: 'en',
			toolbar: 'redbook'
			});
            	</script>
                  </td>
              </tr>
            </table></td>
          </tr>
        </table>
        <table width="100%" border="0" cellpadding="10" cellspacing="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="6">
              <tr>
                <td width="24%" height="40"><input name="upload" type="submit" class="box" id="upload" value=" <?=CNT_TXT_ETIQCLOUD_SENDEMAIL?>" /></td>
                <td width="76%"> </td>
              </tr>
            </table></td>
          </tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">

      </table>
      </td>
    </tr>
  </table>
</form>

Link to comment
Share on other sites

OK, I'm making some progress here.

I have am using jquery to attach multiple files.

I have put together this script that sends an email but without the attachments....I can't see what I have done wrong.

Please help. Thanks

 

<!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">
<head> 
<title>E-mail with Attachment</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../css/demos.css"/>
<script src="../js/jquery-1.7.js" type="text/javascript"         
<script src="../js/jquery.MultiFile.pack.js" type="text/javascript" language="javascript">    
</script> 
</head>

<body> 
<?php 
if ($_SERVER['REQUEST_METHOD']=="POST"){ 

$to="info@mycompany.com"; 
$subject="E-mail with attachment"; 

$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; 

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; 

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

$message="This is an example"; 

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


foreach($_FILES as $userfile){ 

  $tmp_name = $userfile['tmp_name']; 
  $type = $userfile['type']; 
  $name = $userfile['name']; 
  $size = $userfile['size']; 


  if (file_exists($tmp_name)){ 

     if(is_uploaded_file($tmp_name)){ 

        $file = fopen($tmp_name,'rb'); 

        // read the file content into a variable 
        $data = fread($file,filesize($tmp_name)); 

        fclose($file); 

        $data = chunk_split(base64_encode($data)); 
     } 

     $message .= "--{$mime_boundary}\n" . 
        "Content-Type: {$type};\n" . 
        " name=\"{$name}\"\n" . 
        "Content-Disposition: attachment;\n" . 
        " filename=\"{$fileatt_name}\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . 
     $data . "\n\n"; 
   } 
   } 


// here's our closing mime boundary that indicates the last of the message 
$message.="--{$mime_boundary}--\n"; 
// now we just send the message 
if (@mail($to, $subject, $message, $headers)) 
  print_r($_FILES); 
else 
  echo "Failed to send"; 
} else { 
?> 
<p>Send an e-mail with an attachment:</p> 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"  
  enctype="multipart/form-data" name="form1"> 
  <p>Your name: <input type="text" name="fromname"></p> 
  <p>Your e-mail: <input type="text" name="fromemail"></p> 
  <p>Mod List: <textarea  name="question" maxlength="1000" cols="25" rows="6" name="modlist"></textarea> 
  <p><input type="file" name="file[]" class="multi" /></p> 
  <p><input type="submit" name="Submit" value="Submit"></p> 
  </form> 
  <?php } ?> 
  </body> 

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.