将文件附加到电子邮件

I have this form to send an email and the visitor can send an attachment with his email as will everything gos well the email sent but the attachment comes to the email like this

if he uploaded any C.V the C.V will be attached to this Email --_1_c4fe3315ccb7d6076c71d64ec5265ecc Content-Type: multipart/alternative; boundary="_2_c4fe3315ccb7d6076c71d64ec5265ecc"

--_2_c4fe3315ccb7d6076c71d64ec5265ecc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit

--_2_c4fe3315ccb7d6076c71d64ec5265ecc-- --_1_c4fe3315ccb7d6076c71d64ec5265ecc Content-Type: application/octet-stream; name="Yousef.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment

--_1_c4fe3315ccb7d6076c71d64ec5265ecc--

I can see my attached file name but no attachment come with email

the HTML form

<form id="sendYourCV" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data"
                  class="padding_16px">

                <div class="padding_16 clear_both">
                    <p class="f_right complaints_form"><label for="cSector">القطاع :</label></p>
                    <select name="cSector" id="cSector" class="styled-select f_right">
                        <?php
                        $getSections = $db->prepare("SELECT * FROM career_selection_sec_arabic");
                        $getSections->execute();
                        $res = $getSections->get_result();
                        while ($s = $res->fetch_object()) {
                            echo "<option value='$s->sector'>$s->sector</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="padding_16 clear_both">
                    <p class="f_right complaints_form"><label for="cAdministration">الادارة :</label></p>
                    <select name="cAdministration" id="cAdministration" class="styled-select f_right">
                        <?php
                        $getSections = $db->prepare("SELECT * FROM career_selection_admin_arabic");
                        $getSections->execute();
                        $res = $getSections->get_result();
                        while ($a = $res->fetch_object()) {
                            echo "<option value='$a->admin'>$a->admin</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="padding_16 clear_both">
                    <p class="f_right complaints_form"><label for="cBranch">الفرع :</label></p>
                    <select name="cBranch" id="cBranch" class="styled-select f_right">
                        <?php
                        $getSections = $db->prepare("SELECT * FROM career_selection_bran_arabic");
                        $getSections->execute();
                        $res = $getSections->get_result();
                        while ($s = $res->fetch_object()) {
                            echo "<option value='$s->branch'>$s->branch</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="padding_16 clear_both">
                    <p class="f_right complaints_form"><label for="cCareer">الوظيفة :</label></p>
                    <select name="cCareer" id="cCareer" class="styled-select f_right">
                        <?php
                        $getSections = $db->prepare("SELECT * FROM career_selection_car_arabic");
                        $getSections->execute();
                        $res = $getSections->get_result();
                        while ($s = $res->fetch_object()) {
                            echo "<option value='$s->career'>$s->career</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="padding_16 clear_both m_top_10px">
                    <p class="f_right complaints_form">الاسم :</p>
                    <input name="cName" type="text" class="form_textarea_rec f_right" placeholder="اسمك"/>
                </div>
                <div class="padding_16 clear_both m_top_10px">
                    <p class="f_right complaints_form">التليفون :</p>
                    <input name="cTelephone" type="text" class="form_textarea_rec f_right" placeholder="تليفونك"/>
                </div>
                <div class="padding_16 clear_both m_top_10px">
                    <p class="f_right complaints_form">الايميل :</p>
                    <input name="cEmail" type="text" class="form_textarea_rec f_right" placeholder="بريدك الالكتروني"/>
                </div>
                <div class="padding_16 clear_both m_top_10px">
                    <p class="f_right complaints_form">الرسالة :</p>
                <span class="margin_49px">
                  <textarea name="cMessage" class="form_textarea_vertical_2 f_right m_left_6px" placeholder="رسالتك"
                            type="text"></textarea>
                </span>
                </div>
                <div class="padding_16 clear_both m_top_10px">
                    <p class="f_right complaints_form"></p>
                </div>
                <div class="padding_16 clear_both m_top_10px">
                    <p class="f_right complaints_form">سيرتك الذاتية :</p>
                    <input type="file" name="upFile" id="upFile" class="f_right" style="direction:rtl;"/>
                </div>

                <input type="submit" name="submit" id="submit" class="btn_main m_bottom_18px m_left_286px"
                       value="ارسل"/>
            </form>

this is my form code that I used

<?php
error_reporting(E_ALL | E_STRICT);
if (isset($_POST['submit'])) {
    $sector = $_POST['cSector'];
    $cAdministration = $_POST['cAdministration'];
    $cBranch = $_POST['cBranch'];
    $cCareer = $_POST['cCareer'];
    $name = $_POST['cName'];
    $telephone = $_POST['cTelephone'];
    $cEmail = $_POST['cEmail'];
    $cMessage = $_POST['cMessage'];

    $recipient = 'info@test.com';
    $subject = "Someone apply for career";

    $myCv = $_FILES['upFile']['name'];
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['upFile']['tmp_name'])));
    $boundary = md5(date('r', time()));

    $content = "This information is for someone who apply for your new career

    Sector Applied For:" . $sector . ",

    Administration Applied For:" . $cAdministration . ",

    Branch Applied For:" . $cBranch . ",

    Career Applied For:" . $cCareer . ",

    His Name Is: " . $name . ",

    His Phone Number: " . $telephone . ",

    His Message: " . $cMessage . ",

    His Email: " . $cEmail . ",
 if he uploaded any C.V the C.V will be attached to this Email
    --_1_$boundary
    Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

    --_2_$boundary
    Content-Type: text/plain; charset=\"UTF-8\"
    Content-Transfer-Encoding: 7bit

    --_2_$boundary--
    --_1_$boundary
    Content-Type: application/octet-stream; name=\"$myCv\"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment

    $attachment
    --_1_$boundary--";

    $headers = "From:info@test.com
Reply-To:info@test.com";
    $headers .= "
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"_1_$boundary\"";

    $sent = mail($recipient, $subject, $content, $headers);
    if ($sent) {
        header("Location:index.php?pid=17&carr=your message has been sent, we will contact you soon.");
    } else {
        echo "Error";
    }
}
?>

I copied this code from some online tutorial but can't understand why it does not go will with the attachment

thanks in advance

Hay @Mikky you just need to adjust you code try this

    <?php
error_reporting(E_ALL | E_STRICT);
if (isset($_POST['submit'])) {
    $position = $c;
    $name = $_POST['cName'];
    $telephone = $_POST['cTelephone'];
    $cEmail = $_POST['cEmail'];
    $myCv = $_FILES['upFile']['name'];

    $strTo = 'info@test.com';
    $strSubject = "Someone apply for career";
    $strMessage = nl2br("This information is for someone who apply for your new career

    Position Applied For:" . $position . ",

    His Name Is: " . $name . ",

    His Phone Number: " . $telephone . ",

    His Email: " . $cEmail . " 
");

//*** Uniqid Session ***//
    $strSid = md5(uniqid(time()));

    $strHeader = "";
    $strHeader .= "From:info@test.com
Reply-To:info@test.com";

    $strHeader .= "MIME-Version: 1.0
";
    $strHeader .= "Content-Type: multipart/mixed; boundary=\"" . $strSid . "\"

";
    $strHeader .= "This is a multi-part message in MIME format.
";

    $strHeader .= "--" . $strSid . "
";
    $strHeader .= "Content-type: text/html; charset=utf-8
";
    $strHeader .= "Content-Transfer-Encoding: 7bit

";
    $strHeader .= $strMessage . "

";

//*** Attachment ***//
    if ($_FILES['upFile']['name'] != "") {
        $strFilesName = $_FILES["upFile"]["name"];
        $strContent = chunk_split(base64_encode(file_get_contents($_FILES["upFile"]["tmp_name"])));
        $strHeader .= "--" . $strSid . "
";
        $strHeader .= "Content-Type: application/octet-stream; name=\"" . $strFilesName . "\"
";
        $strHeader .= "Content-Transfer-Encoding: base64
";
        $strHeader .= "Content-Disposition: attachment; filename=\"" . $strFilesName . "\"

";
        $strHeader .= $strContent . "

";
    }


    $flgSend = @mail($strTo, $strSubject, null, $strHeader); // @ = No Show Error //

    if ($flgSend) {
        header("Location:index.php?pid=17&msg=done");
    } else {
        echo "Cannot send mail.";
    }
}
?>

let me know if it's working now with you

function mailWithAttachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $totalmailsent)
{
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">
";
    $header .= "Reply-To: ".$replyto."
";
    $header .= "MIME-Version: 1.0
";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"

";
    $header .= "This is a multi-part message in MIME format.
";
    $header .= "--".$uid."
";
    $header .= "Content-type:text/html; charset=iso-8859-1
";
    $header .= $message."

";
    $header .= "--".$uid."
";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"
"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64
";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"

";
    $header .= $content."

";
    $header .= "--".$uid."--";
    if(mail($mailto, $subject, "", $header))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

You give this a try and let me if fixed, since I'm using this with native php mail

I used the following code along with swiftmailer to do the job.

function send_email_attachment($email, $name, $filename) {

//return $email; //exit;
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('username')
  ->setPassword('password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

$the_message = "Hi {$name},<br/> Your booking is CONFIRMED and the invoice pertaining to the booking is attached with this mail. <br/> ";

// Create a message
$message = Swift_Message::newInstance('Subject')
  ->setFrom(array('email' => 'name'))
  ->setTo(array($email => $name))
  ->setBody($the_message, 'text/html')
  ->attach(Swift_Attachment::fromPath($filename))
  ;

// Send the message
$result = $mailer->send($message);
//if ($result){ return 'DONE';}else{    return 'SORRY';}
return;
}

?>

This worked well when I used it for one of the sites which I helped in developing and I hope it will work for you too.

N. B.: You will need to get swiftmailer to make the code work.

Perhaps consider using a simplified library such as PHPMailer? It handles most of the parts that take you excessive time for you, you wouldn't need to add headers and all that - it's done for you.

Example:

    $mail=new PHPMailer();
    $mail->isSMTP();

    //host through which the mail should be sent
    $mail->Host="smtp.mydomain.com";              

    //address where the email is going to be sent from
    $mail->From="me@mydomain.com";       

    //address where the email is going         
    $mail->AddAddress("person@otherdomain.com");

    //subject for the email  
    $mail->Subject="Example Email";   

    //Your email will be displayed as HTML                   
    $mail->IsHTML(true);               

    // you can use HTML here                  
    $mail->Body="Hi, This <strong>is</strong> an email"; 

    //base64 encoding is applied here by default
    $mail->AddAttachment("path/to/file"); 

    //send the email               
    if ($mail->Send())
    {
         echo "Mail Sent";
    }
    else
    {
         echo "Error sending mail: " . $mail->ErrorInfo;
    }

I usually use code very similar to this without any problems. I hope this helps, and makes your life much simpler. Just remember to include the PHP mailer library in the script, see the tutorial here for info on that.

Problem: I guess you are putting extra space for content-type declaration code in His mail:. See my code there is no need for spaces before that if you will give space it will produce error.

Now the working code with correct formatting is given below:

<?php
error_reporting(E_ALL | E_STRICT);
if (isset($_POST['submit'])) {
    $sector = $_POST['cSector'];
    $cAdministration = $_POST['cAdministration'];
    $cBranch = $_POST['cBranch'];
    $cCareer = $_POST['cCareer'];
    $name = $_POST['cName'];
    $telephone = $_POST['cTelephone'];
    $cEmail = $_POST['cEmail'];
    $cMessage = $_POST['cMessage'];

    $recipient = 'info@test.com';
    $subject = "Someone apply for career";

    $myCv = $_FILES['upFile']['name'];
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['upFile']['tmp_name'])));
    $boundary = md5(date('r', time()));

    $content = "This information is for someone who apply for your new career

    Sector Applied For:" . $sector . ",

    Administration Applied For:" . $cAdministration . ",

    Branch Applied For:" . $cBranch . ",

    Career Applied For:" . $cCareer . ",

    His Name Is: " . $name . ",

    His Phone Number: " . $telephone . ",

    His Message: " . $cMessage . ",

    His Email: " . $cEmail . ",
 if he uploaded any C.V the C.V will be attached to this Email
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"UTF-8\"
Content-Transfer-Encoding: 7bit

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$myCv\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--_1_$boundary--";

    $headers = "From:info@test.com
Reply-To:info@test.com";
    $headers .= "
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"_1_$boundary\"";

    $sent = mail($recipient, $subject, $content, $headers);
    if ($sent) {
        header("Location:index.php?pid=17&carr=your message has been sent, we will contact you soon.");
    } else {
        echo "Error";
    }
}
?>

I hope this will solve your problem. Since it worked for me with your HTML code.

Happy Coding!!!