PHP Mail: Send Mail with Attachment
To send a message with an attachment, you need to mention many header information which is used in the example given below.
<?php
$to = "easytolearning2@gmail.com";
$subject = "This is subject";
$message = "This is a text message.";
# Open a file
$file = fopen("your directory path/test.txt", "r" );//change your file location
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("/your directory path/test.txt");
$content = fread( $file, $size);
# encode the data for safe transit
# and insert
after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From:support@easytolearning.com
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num
";
$header .= "--$num
";
# Define the message section
$header .= "Content-Type: text/plain
";
$header .= "Content-Transfer-Encoding:8bit
";
$header .= "$message
";
$header .= "--$num
";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name="test.txt"
";
$header .= "Content-Transfer-Encoding:base64
";
$header .= "Content-Disposition:attachment; ";
$header .= "filename="test.txt"
";
$header .= "$encoded_content
";
$header .= "--$num--";
# Send email now
$result = mail ( $to, $subject, "", $header );
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning