How to attach file with PHP MAIL function

Sharing is caring!

270 Views -

The Php mail() function allows you to send emails directly from your script without using any authentication.

mail(to,subject,message,headers,parameters);

If you want to add some attachment to your mail without saving it, you can try like this –

 <?php
 $to = "somebodyelse@example.com";
 $content = "
        <html>
            <head>
                <title>HTML email</title>
            </head>
            <body>
                <p>This email contains HTML Tags!</p>
                <table>
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                    </tr>
                    <tr>
                        <td>John</td>
                        <td>Doe</td>
                    </tr>
                </table>
            </body>
        </html>
        ";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $myemail\r\n" . 
            "CC: test@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/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $content . "\n\n";


if(isset( $_FILES))
{
    //Get uploaded file data using $_FILES array 
    $tmp_name    = $_FILES['logo']['tmp_name']; // get the temporary file name of the file on the server 
    $file_name   = $_FILES['logo']['name'];  // get the name of the file 
    $size        = $_FILES['logo']['size'];  // get size of the file for size validation 
    $type        = $_FILES['logo']['type'];  // get type of the file 
    $error       = $_FILES['logo']['error']; // get the error (if any) 

    //validate form field for attaching the file 
    if($error > 0 && !empty($file_name)) 
    { 
        die('Upload error or No files uploaded'); 
    } 


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

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

    // close the file
    fclose($file);

    // now we encode it and split it into acceptable length lines
    $data = chunk_split(base64_encode($data));

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

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


$response = 	mail($to,"send mail",$message,$headers);

if($response )  
{ 
    echo "Mail Sent Successfully.";
    
} 
else
{ 
    die("Sorry but the email could not be sent. 
    Please go back and try again!"); 
}

 

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments