1

I have PHP code where i code for sending attached file via email from one email id to another email id, But the name ,message ,email_id all send but not attached file sended.

<?php

   $name=$_REQUEST['name'];
   $email_id=$_REQUEST['email_id'];
   $message=$_REQUEST['message'];
   $attached_file=$_REQUEST['attached_file'];
   $name= mysql_real_escape_string($name);
   $email_id = mysql_real_escape_string($email_id);
   if(isset($_REQUEST['submit']))
   {
    $sql="select * from user where BINARY name = BINARY '$name' AND
     BINARY email_id = BINARY '$email_id' and status='1'";  
   $qex=mysql_query($sql);
   $numrow=mysql_num_rows($qex);
   $res=mysql_fetch_array($qex);    
   if($numrow==1)
  {
    $_SESSION['name']=$res['name'];
   $_SESSION['email_id']=$res['email_id'];


        $email_to .= "nehamitta919@gmail.com";
        $email_subject="A user send Tariffs file to their Client";
        $email_from="neha@studioscue.in";

        $email_message = "User Attached File Successfully send     ".$time."\n\n";


        $email_message .= "User Name  : ".$name."\n\n";
        $email_message .= "User Email: ".$email_id."\n\n";
        $email_message .= "User Message: ".$message."\n\n";
        $email_message .= "User Send File: ".$attached_file."\n\n";


        $headers = 'From: '.$email_from."\r\n".
        'Reply-To: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);

        ?>

html code :

<form class="form-horizontal" method="post" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="inputEmail3" class="col-sm-4 control-label">User name</font></label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="inputEmail3" placeholder="Name" required
                                name="name" value="<?Php echo $uvw['name']; ?>">
                            </div>
                            </div>
                        </div>
                    </div><!----row-------->
                    <br>
                    <br>
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-4 control-label">User Email Id<br><strong>(Send From)</strong></font></label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="inputEmail3" ng-model="email_id" placeholder="email" required
                            name="email_id" value="<?Php echo $uvw['email_id']; ?>">
                        </div>
                        </div>
                    </div>
                </div><!----row-------->
                <br>
                <br>                                              
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-4 control-label">Send To</font></label>
                    <div class="col-sm-4">
                        <select required class="form-control" id="client_id" name="client_id" ng-model="client_id">
                                          <option required="required" selected="" hidden="" value="">Select Your Client Email Id</option>
                                           <?php
                        $id= $_SESSION['id'];
                        $qex = mysql_query("select * from client where status!='4' and status!='0' and client.user_id='$id'");
                                            while ($res = mysql_fetch_array($qex)) {
                                                ?>
                                                <option value="<?php echo trim($res['group_id']); ?>">
                                                    <?php
                                                    echo trim($res['group_id']);
                                                    ?>
                                                </option>
                                            <?php }
                                            ?>

                    </select>
                </div>
                </div>
            </div>
        </div><!----row-------->
        <br />
        <br />
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Message</font></label>
                <div class="col-sm-4">
                    <textarea class="form-control" id="inputEmail3" placeholder="Message" required style="height: 50px;"
                    name="message"></textarea>
                </div>
                </div>
            </div>
        </div><!----row-------->
        <br>
        <br>
    <div class="row"
        <div class="col-md-12">
            <div class="form-group">
                <label for="inputEmail3" class="img-rounded col-sm-4 control-label animate-css-css" >Attach File</font></label>
            <div class="col-sm-3">
                <input type="file"name="attached_file" id="attached_file" required>
            </div>
            </div>
        </div><!----row-------->
        <br>
        <br />  
    <div class="form-group">
        <div class="col-sm-offset-5 col-sm-5">
            <button type="submit" class="btn btn-primary col-md-5" name="submit">Send File</button>
        </div>

    </div>
            </form>

Please check & reply me. what am i missing. Thanks & Regards Ankit

Ankit
  • 59
  • 2
  • 7
  • duplicate to this http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – tinker Mar 29 '17 at 06:26

3 Answers3

0

try this code,mention your file name as attachment...

 <?php
        ini_set('max_execution_time', 300);

        $to = "example@gmail.com";
$subject= "Contact Mail";

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

         $headers = "From:<developer@gmail.com>"."\r\n" .
         "MIME-Version: 1.0\r\n" .
            "Content-Type: multipart/mixed;\r\n" .
            " boundary=\"{$mime_boundary}\"";

         $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" .
            "Contact Details"."\n".
         "Name :". $_POST['name']."\n".
          "email :". $_POST['email']."\n";

 $message.="comments :". $_POST['comments']."\n"        
 ."\n\n";



         foreach($_FILES as $userfile)
         {
            $tmp_name = $userfile['tmp_name'];
            $type = $userfile['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
            $name = $userfile['name'];
            $size = $userfile['size'];

            if (file_exists($tmp_name))
            {
               if(is_uploaded_file($tmp_name))
               {
                  $file = fopen($tmp_name,'rb');

                  $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";
            }
         }

         $message.="--{$mime_boundary}--\n";



         if (mail($to, $subject, $message, $headers)){
         echo "<script>alert('Mail sent Successfully');</script>";
            echo "<script>window.location = 'contact.php';</script>";

         } else {
            echo "<script>alert('Mail Not Send');</script>";
            echo "<script>window.location = 'contact.php';</script>";


        } 
        ?>
Mahesh
  • 93
  • 9
  • this is a example code for sending attachment you have to encode that image file.... – Mahesh Mar 29 '17 at 06:26
  • i use your exact code & its not showing me form to fill values to select file & write message. It just show me alert message as " Mail Sent Successfully. but blank mail. i want to send form details user name , user email id, user message & user attached file. & after that i should be able to download that sendded file. Please check again & reply me sir. - Regards -- Ankit – Ankit Mar 29 '17 at 06:43
  • online llive site sir – Ankit Mar 29 '17 at 06:56
  • we are using this exact code for live sites...its working fine for us...according to your requirement you have to customize your code...or you can use php mailer function is the alternative solution.. – Mahesh Mar 29 '17 at 07:04
  • with this exactly same code you can add form details? and can you please share you live site link with me so i can test that. or show me via jsfiddel . – Ankit Mar 29 '17 at 07:25
0

u must be use phpmailer https://github.com/PHPMailer/PHPMailer

$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "yourname@example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images              to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
   } else {
    echo "Message sent!";
   }
Suat Kocabas
  • 63
  • 1
  • 9
  • i have to use this one "class.phpmailer.php" file right ? – Ankit Mar 29 '17 at 06:33
  • yepp ;) include("class.phpmailer.php"); – Suat Kocabas Mar 29 '17 at 06:36
  • sir i add that file but nothing happens. same result as before. other details send via email but not attached file. Please check again & Reply me. Regards - Ankit – Ankit Mar 29 '17 at 06:55
  • sir my code is already here in top i just add the phpmailer class file – Ankit Mar 29 '17 at 07:23
  • no sir not working. this is not static its dynamic both emails will be dynamic. with my PHP code in top cam you please tell me that how can i pass selected "client_id" value to $email_to variable & email_id in $email_from. please answer me for this.Regards - Ankit – Ankit Mar 29 '17 at 08:14
  • $email_message .= "User Name : ".$name."\n\n"; $email_message .= "User Email: ".$email_id."\n\n"; $email_message .= "User Message: ".$message."\n\n"; $email_message .= "User Send File: ".$attached_file."\n\n"; $mail->msgHTML($email_message); // ??? sorry i can understand your problem. – Suat Kocabas Mar 29 '17 at 08:34
0
include("class.phpmailer.php");   
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "mail.example.com";                       //your host name
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "yourname@example.com";                //email username
$mail->Password = "yourpassword";                        // email pass
$mail->setFrom('from@example.com', 'First Last');        //email adress
$mail->addReplyTo('replyto@example.com', 'First Last');,

$mail->addAddress('whoto@example.com', 'John Doe');      // whoto 1
$mail->addAddress('whoto@example.com', 'John Doe');      // whoto 2
$mail->addAddress('whoto@example.com', 'John Doe');      // whoto 3


$mail->Subject = 'PHPMailer SMTP test';                  // title
$mail->msgHTML($body);                                   // your message body


$mail->addAttachment('images/phpmailer_mini.png');        // file url
$mail->addAttachment('images/phpmailer_mini.png');        // file url
$mail->addAttachment('images/phpmailer_mini.png');        // file url

if (!$mail->send()) {
     echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
      echo "Message sent!";
 }
Suat Kocabas
  • 63
  • 1
  • 9