< Back to Code Samples
<?php
//
// This file uses the "Attachment class"
//
require_once("attachment.class");

define('FROM''donotreply@indiana.edu');
define('EOL'"\r\n");

/**
* mail_attachment() is a function used for sending a SINGLE FILE ATTACHMENT with
* an email. It would be trivial to allow it to send multiple attachments, but for
* what we needed, one attachment was enough.
*
* @param $username     One of the security measures we take to prevent relaying. 
*                Since we know that our recipients will ALWAYS be 
*                indiana.edu addresses, we simply take the username as the 
*                parameter and append the domain.
* @param $subject        The subject (a string)
* @param $message        The message (a string). This should be ready-to-go already.
* @param $attachment    The attachment (a single element from $_FILES)
* @param $feedback    Response feedback, optional.
* @see   Assuming everything goes right, you'll see the feedback message. 
*
*/

function mail_attachment($username$subject$message$attachment$feedback "Thank you. Your mail has been sent."){
    
// Prevent spammers from using this as a free relay. It would actually just create
    // a bounceback, since it would be their@target.com@indiana.edu, but I'd rather
    // just not even allow bouncebacks.

    
if (strpos($username"@") !== FALSE) { die("Sorry, that is not allowed."); }

    
// Prevent Meta / Header injections
    
$find "/(content-type|bcc:|cc:)/i";
    
$args = array($username$subject$message);
    foreach(
$args as $var => $val)
    { 
        if (
preg_match($find$val)) 
        {
          die(
"<h1>Error</h1><br />\r <p>No meta/header injections, please.</p>");
        }
    }


    
$headers "From: " FROM EOL
                
"Reply-To: " FROM EOL
                
"Return-Path: " FROM EOL
                
"Message-ID: <" time() . "-" FROM ">" EOL
                
"X-Mailer: PHP v" phpversion() . EOL;

    
$file = new Attachment($attachment);

    
$mime_boundary "==Multipart_Boundary_x" md5(time()) . "x"
    
    
$headers .= EOL "MIME-Version: 1.0" EOL 
            
"Content-Type: multipart/mixed;" EOL 
            
" boundary=\"{$mime_boundary}\""

    
$body "This is a multi-part message in MIME format." EOL EOL 
            
"--{$mime_boundary}" EOL 
            
"Content-Type:text/html; charset=\"utf-8\"" EOL 
            
"Content-Transfer-Encoding: 7bit" EOL EOL 
            
"*** Do not reply directly to this message ***<br />" EOL
            
$message "<br />" EOL
            
"Sent from: " $_SERVER['REMOTE_ADDR'] . EOL EOL;

    
$body .= $file->toString($mime_boundary);

    
$body .= $file->encode();

    if(@
mail($username "@indiana.edu"$subject$body$headers)) { 
        echo 
$feedback;
    } else { 
        echo 
"Sorry, but there has been some kind of error. Please email directly to this address: <a href=\"mailto:$username@indiana.edu\">$username@indiana.edu</a>";
        
trigger_error("[SEND_ERR]: Message send failure.",E_USER_NOTICE);
    } 
}

?>