Husna
Husna

Reputation: 1386

Attachments not sent using php mail() function

I'm working on file attachment here. The mail function is working fine other than the file field is empty. I have tried using Content-Type: multipart/mixed and some other methods but unable to achieve the desired output. I have searched for a different answer and tried but am still facing the same issue. There seems to be a duplicate but I had tried all methods can anyone guide me the right direction as per my script what i missing here. How can I get the file attachment? Can anyone suggest me in the right direction what I'm missing here why other fields are not coming if I remove file attachment code then other fields are working fine and mail has come. HTML

<form class="test" action="contactMail.php" method="POST">
  <input type="hidden" name="formname" value="Form sent by About page">
  <select class="user-select">
    <option value="">Pick Job Role</option>
    <option>Web Developer</option>
    <option>Java Developer</option>
  </select>
  <input type="text" name="name" placeholder="Your Name">
  <input type="tel" name="phone" placeholder="Contact Number">
  <label for="file-upload" class="file-upload">Upload Image</label>
  <input type="file" id="file-upload" name="upload" required>
  <textarea name="message" placeholder="Your Message" rows="3">/textarea>
            <button type="submit" class="from-submit form-group form-fields">Submit</button>
    <div id="success_contact">                                               
        <h2>Request Sent!</h2>
    </div>
</form>

   `<?php
// Receiver mail id 
$mail_to = '[email protected]';

// Mail Subject 
$subject = 'Virtual Raasta';

$upload = $_FILES["upload"];
$path = 'assets/file';
$filename = 'myfile';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if ( isset($_POST['name']) ) {
        $name = $_POST['name'];
    }

    if (isset($_POST['phone'])) {
        $phone = $_POST['phone'];
    }

    if (isset($_POST['company'])) {
        $company = $_POST['company'];
    }

    if(isset($_POST['message'])) {
        $message = $_POST['message'];
    }
    if(isset($_POST['industry'])) {
        $industry = $_POST['industry'];
    }
    if(isset($_POST['job'])) {
        $job = $_POST['job'];
    }
    if(isset($_FILES['upload'])) {
        $upload = $_FILES['upload'];
    }

    $reqBy = $_POST['formname'];

    // Message body

    $msg = '<html><body><p>';

    $msg .= '<b> Request Sent From : </b>' . $reqBy . '<br/>';

    $msg .= '<b> Name : </b>' . $name . '<br/>';

    if($_POST["phone"] != "") {
       $msg .= '<b> Phone : </b>' . $phone . '<br/>';
    }

    if($_POST["company"] != "") {
       $msg .= '<b> Company : </b>' . $company . '<br/>';
    }

    if($_POST["message"] != "") {
        $msg .= '<b> Message : </b>' . $message . '<br/>';
    }

    if($_POST["industry"] != "") {
        $msg .= '<b> Industry : </b>' . $industry . '<br/>';
    }

    if($_POST["job"] != "") {
        $msg .= '<b> Job Role : </b>' . $job . '<br/>';
    }

    if($_FILES["upload"] != "") {
        $msg .= '<b> Upload : </b>' . $upload . '<br/>';
    }

    $msg .= '</p>';
    $msg .= '</body></html>';
    var_dump($msg);

    $filename = $_FILES["upload"]["name"];
    //$content = file_get_contents( $_FILES['upload']['tmp_name'] );

    if(!empty($_FILES['upload']['tmp_name']) && file_exists($_FILES['upload']['tmp_name'])) {
        $content= addslashes(file_get_contents($_FILES['upload']['tmp_name']));
    }
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));


    $replyto = 'test';
    $headers = "From: ".$subject." <".'[email protected]'.">\r\n";
    $headers .= "Reply-To: ".$replyto."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

    $msg = "--".$uid."\r\n";
    $msg .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $msg .= $msg."\r\n\r\n";
    $msg .= "--".$uid."\r\n";
    $msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
    $msg .= "Content-Transfer-Encoding: base64\r\n";
    $msg .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $msg .= $content."\r\n\r\n";
    $msg .= "--".$uid."--";

    if( mail( $mail_to, $subject, $msg, $headers )) {
        echo "Thank You!";
    } else {
        die("Error!");
    }
   }

 ?>`

Upvotes: 0

Views: 1093

Answers (4)

Ususipse
Ususipse

Reputation: 321

There are several issues here. The primary issue is that this code is a security nightmare and should never be deployed anywhere.

The second issue is checking if your arguments are populated before trying to use them to instantiate other variables. When you assign $upload and then make a check for $_POST data and only then see if the $_POST argument was even set in the first place, it suggests you need to re-read the isset() documentation and even the fundamentals of conditional statements.

Upvotes: 0

ivion
ivion

Reputation: 567

I hope it is just a typo. You are using the var $msg twice. Once for Your html-message and for the body of the message.

<?php

....

    // Message body

    $msg = '<html><body><p>';

    $msg .= '<b> Request Sent From : </b>' . $reqBy . '<br/>';

    $msg .= '<b> Name : </b>' . $name . '<br/>';

    if($_POST["phone"] != "") {
       $msg .= '<b> Phone : </b>' . $phone . '<br/>';
    }

    if($_POST["company"] != "") {
       $msg .= '<b> Company : </b>' . $company . '<br/>';
    }

    if($_POST["message"] != "") {
        $msg .= '<b> Message : </b>' . $message . '<br/>';
    }

    if($_POST["industry"] != "") {
        $msg .= '<b> Industry : </b>' . $industry . '<br/>';
    }

    if($_POST["job"] != "") {
        $msg .= '<b> Job Role : </b>' . $job . '<br/>';
    }

    if($_FILES["upload"] != "") {
        $msg .= '<b> Upload : </b>' . $upload . '<br/>';
    }

    $msg .= '</p>';
    $msg .= '</body></html>';
    var_dump($msg);

    $filename = $_FILES["upload"]["name"];
    //$content = file_get_contents( $_FILES['upload']['tmp_name'] );

    if(!empty($_FILES['upload']['tmp_name']) && file_exists($_FILES['upload']['tmp_name'])) {
        $content= file_get_contents($_FILES['upload']['tmp_name']); // No add_slashes()
    }
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));


    $replyto = 'test';
    $headers = "From: ".$subject." <".'[email protected]'.">\r\n";
    $headers .= "Reply-To: ".$replyto."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

Rename the var here and change Content-type and transfer-encoding as well:

    $msgBody = "--".$uid."\r\n";
    $msgBody .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $msgBody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
    $msgBody .= $msg."\r\n\r\n";

    if(isset($_Files['upload'])) // Only add attachment if uploaded
         $msgBody .= "--".$uid."\r\n";
         $msgBody .= "Content-Type: ".mime_content_type ( $_FILES['upload']['tmp_name'] )."; name=\"".$filename."\"\r\n";
         $msgBody .= "Content-Transfer-Encoding: base64\r\n";
         $msgBody .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
         $msgBody .= $content."\r\n\r\n";
    }
    $msgBody .= "--".$uid."--";

    if( mail( $mail_to, $subject, $msgBody, $headers )) {
        echo "Thank You!";
    } else {
        die("Error!");
    }
   }

 ?>

Upvotes: 1

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

update your form opening tag with

<form class="test" action="contactMail.php" method="POST" enctype="multipart/form-data">

add below code before starting for if condition

if ($_FILES['upload']['error'] == 0) {
   $filename = $_FILES["upload"]["name"];
   $content = file_get_contents( $_FILES['upload']['tmp_name'] );
   $content = chunk_split(base64_encode($content));
   $uid = md5(uniqid(time()));
}

Upvotes: 0

user2342558
user2342558

Reputation: 6693

To better handle emails with PHP you can use this library:

https://github.com/PHPMailer/PHPMailer

There is also the availability to make the minimal installation:

At the very least you will need src/PHPMailer.php. If you're using SMTP, you'll need src/SMTP.php, and if you're using POP-before SMTP, you'll need src/POP3.php.

Upvotes: 0

Related Questions