How to send mail with attachment in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ravigandha
    New Member
    • Mar 2008
    • 17

    How to send mail with attachment in php

    Exactly the question is i have a code that is working fine in sending mail with attachments, but when the mail is sent to yahoo address the attachment file is not displaying ? Why this is happening where as it works fine for gmail, mydomain mails displaying the attachment !
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    It sounds like a security measure implemented in Yahoo. Maybe, like in Hotmail, you can confirm the message is not spam, somehow? What mime type do you use?

    Comment

    • Ravigandha
      New Member
      • Mar 2008
      • 17

      #3
      Here is my code
      Code:
      $id=$_SESSION['mailid'];
        $sub=$_POST[subj];
        $msg=$_POST[message];
         $body=       "----------------------------Messagee--------------------------------\n" .
                    "$msg\n";
        $from="info@gokulmarriage.com";
         "Upload: " . $_FILES["fileatt"]["name"] . "<br />";
       "Type: " . $_FILES["fileatt"]["type"] . "<br />";
       "Size: " . ($_FILES["fileatt"]["size"] / 1024) . " Kb<br />";
       "Temp file: " . $_FILES["fileatt"]["tmp_name"] . "<br />";
      
      if(is_uploaded_file($_FILES["fileatt"]["tmp_name"]))
                              {
                              $tmpname=$_FILES["fileatt"]["tmp_name"];
                              $orgname=$_FILES["fileatt"]["name"];
                              $fileUp=time()."_".$orgname;
                              $dest="uplode_file/".$fileUp;
                           move_uploaded_file($_FILES["fileatt"]["tmp_name"],
                          "uplode_file/" . $fileUp);
                           "Stored in: " . "uplode_file/" . $fileUp;
                                                   
                              } 
      $fileatt = $dest; // Path to the file
      $fileatt_type = $_FILES["fileatt"]["type"]; // File Type
      $fileatt_name = $_FILES["fileatt"]["name"]; // Filename that will be used for the file as the attachment
         foreach ($id as $value)
              {
                   "$value is checked";
                   $sat="select * from  registration where reg_id='$value' ";
                  $res_del=mysql_query($sat);
                  $row=mysql_fetch_object($res_del);
                  $toemail=$row->email;    
                  $headers = "From: $from";
                   $fileatt= $dest;
               $file = fopen($fileatt,'rb');
               $data = fread($file,filesize($fileatt));
               fclose($file);
                 // Generate a boundary string
               $semi_rand = md5(time());
               $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
               
               // Add the headers for a file attachment
               $headers .= "\nMIME-Version: 1.0\r\n" .
                           "Content-Type: multipart/mixed;\n" .
                           " boundary=\"{$mime_boundary}\"\r\n\r\n";
                           // Add a multipart boundary above the plain message
               $message = "This is a multi-part message in MIME format.\r\n\r\n" .
                          "--{$mime_boundary}\r\n" .
                          "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
                          "Content-Transfer-Encoding: 7bit\r\n\r\n" .
                          $body . "\r\n\r\n";
                          // Base64 encode the file data
               $data1= chunk_split(base64_encode($data),72);
               // print("<pre>$data1</pre>");
               // Add file attachment to the message
               $message .= "--{$mime_boundary}\r\r" .
                           "Content-Type: {$fileatt_type};\r\n" .
                           " name=\"{$orgname}\"\r\n" .
                          
                            "Content-Disposition: attachment; filename=\"{$orgname}\"\r\n" .
                             "Content-Transfer-Encoding: base64\r\n\r\n" .
                            $data1 . "\r\n\r\n" .
                           "--{$mime_boundary}--\r\n\r\n\r\n";
                           
                  $ok = @mail($toemail, $sub, $message, $headers);
                  if ($ok)
                       {
                 echo "sent";
                       } 
                  else
                       {
                  echo "Failed";
                       }

      Comment

      • Ravigandha
        New Member
        • Mar 2008
        • 17

        #4
        No the message is not spam.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Ravigandha.

          Are only certain types of files not showing up, or is Yahoo blocking all your attachments?

          Comment

          • Ravigandha
            New Member
            • Mar 2008
            • 17

            #6
            I think yahoo and hotmail is blocking all the attachments and displaying it in junk format ! Where as in others it is displaying attachments !

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Sounds like an issue you need to take up with Yahoo. The problem is in their policy, not your code.

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Yeah, spam checkers are often treating my emails as spam because they include some key spam words in the subject (Register, Sign up, etc) as well as my site not being well known. Spam protection is very tight on some mail servers where you are guilty until proven innocent, so have a play around with wording in subject, content and attachment names and see if that helps.

                Comment

                Working...