Created
May 16, 2013 15:20
-
-
Save stavrossk/5592507 to your computer and use it in GitHub Desktop.
Sending e-mail with attachments using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title> | |
Sending attachment using PHP | |
</title> | |
</head> | |
<body> | |
<?php | |
$to = "xyz@somedomain.com"; | |
$subject = "This is subject"; | |
$message = "This is test message."; | |
# Open a file | |
$file = fopen( "/tmp/test.txt", "r" ); | |
if( $file == false ) | |
{ | |
echo "Error in opening file"; | |
exit(); | |
} | |
# Read the file into a variable | |
$size = filesize("/tmp/test.txt"); | |
$content = fread( $file, $size); | |
# encode the data for safe transit | |
# and insert \r\n 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:xyz@somedomain.com\r\n"; | |
$header .= "MIME-Version: 1.0\r\n"; | |
$header .= "Content-Type: multipart/mixed; "; | |
$header .= "boundary=$num\r\n"; | |
$header .= "--$num\r\n"; | |
# Define the message section | |
$header .= "Content-Type: text/plain\r\n"; | |
$header .= "Content-Transfer-Encoding:8bit\r\n\n"; | |
$header .= "$message\r\n"; | |
$header .= "--$num\r\n"; | |
# Define the attachment section | |
$header .= "Content-Type: multipart/mixed; "; | |
$header .= "name=\"test.txt\"\r\n"; | |
$header .= "Content-Transfer-Encoding:base64\r\n"; | |
$header .= "Content-Disposition:attachment; "; | |
$header .= "filename=\"test.txt\"\r\n\n"; | |
$header .= "$encoded_content\r\n"; | |
$header .= "--$num--"; | |
# Send email now | |
$retval = mail ( $to, $subject, "", $header ); | |
if( $retval == true ) | |
{ | |
echo "Message sent successfully..."; | |
} | |
else | |
{ | |
echo "Message could not be sent..."; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pathetic. The send process doesn't even initiate. Did you even bother testing this or is this just theoretical code?