BigToach.com

PHP Snippets

sendmail

Information

(PHP Version >= 3)
Functions used in this snippet: trim, preg_math, rand, strip_tags, preg_replace, chunk_split, base64_encode, is_array, file_exists, pathinfo, file_get_contents, mail, implode.

Description

boolean sendmail( string to, string subject, string body, mixed file, string from, string additional )

Simply put, this function sends mail. It checks to see if the body is in html format, if so it will send it with the correct headers to be received as html. It also sends a plain text alternative for older browsers.

If the optional file is supplied, the function will send this file (or files if file is of type array) as an attachment.

You can also specify who the email should be from as well as the reply-to address.

If you need additional settings, you can pass them as mail headers in the additional parameter.

Snippet

<?php
function sendmail($to$subject$body$file=NULL$from='noreply@yourdomain.com'$additional='')
{
    
$boundary 'SPROUT-19_16_18_15_21_20';
    
$headers = array();
    
$headers[] = 'From: ' $from;
    
$headers[] = 'Reply-To: ' $from;
    if(
trim($additional) !== '')
    {
        
$headers[] = $additional;
    }
    
$headers[] = 'MIME-Version: 1.0';
    
$headers[] = 'Content-Type: multipart/mixed; boundary="' $boundary '"';
    
$headers[] = 'Content-Transfer-Encoding: 7bit' "\r\n";
    
$headers[] = "> This is a multi-part message in MIME format.\r\n";
    
$headers[] = '--' $boundary;
    
// if there is html in the message, send it with the right settings and an alternative for crappy browsers
    
if(preg_match('/\<(html|body)/ims'$body))
    {
        
$htmlbound 'SPROUT-' rand(100,9999);
        
$headers[] = 'Content-Type: multipart/alternative; boundary="' $htmlbound '"' "\r\n";
        
$headers[] = '--' $htmlbound;
        
$headers[] = 'Content-Type: text/plain; charset="iso-8859-1"';
        
$headers[] = 'Content-Transfer-Encoding: 7bit' "\r\n";
        
$headers[] = strip_tags(preg_replace('/\<head\>(.+?)\<\/head\>/ims'''$body)) . "\r\n\r\n";
        
$headers[] = '--' $htmlbound;
        
$headers[] = 'Content-Type: text/html; charset="iso-8859-1"';
        
$headers[] = 'Content-Transfer-Encoding: base64' "\r\n";
        
$headers[] = chunk_split(base64_encode($body));
        
$headers[] = "\r\n\r\n--" $htmlbound "--\r\n\r\n--" $boundary;
    }
    else 
    {
        
$headers[] = 'Content-Type: text/plain; charset="iso-8859-1"';
        
$headers[] = 'Content-Transfer-Encoding: 7bit' "\r\n";
        
$headers[] = $body "\r\n\r\n";
        
$headers[] = '--' $boundary;
    }
    
    
// attach files if they are there
    
if($file !== NULL)
    {
        
$files = (!is_array($file)) ? array($file) : $file;
            
        foreach(
$files as $attachment)
        {
            if(!
file_exists($attachment))
                continue;
            
$path pathinfo($attachment);
            
$headers[] = 'Content-Type: application/octet-stream; name="' $path['basename'] . '"';
            
$headers[] = 'Content-Disposition: attachment; filename="' $path['basename'] . '"';
            
$headers[] = "Content-Transfer-Encoding: base64\r\n";
            
$attach chunk_split(base64_encode(file_get_contents($attachment)));
            
$headers[] = $attach;
            
$headers[] = '--' $boundary;
        }
    }
    return 
mail($to$subject''implode("\r\n"$headers) . '--');
}
?>

Example

sendmail Can be used in the following way:

<?php

// Send an html file
sendmail('someone@domain.ext''Title''HTML CONTENT HERE');

// Send an email with an attachment
sendmail('someone@domain.ext''Title''Check out this picture''image.jpg');

// Send an html style email with multiple attachments
sendmail('someone@domain.ext''Title''HTML GOES HERE', array('product.jpg''productdetails.pdf'));

// Send a plaintext email from joeschmoe@domain.ext
sendmail('someone@domain.ext''Title''PLAIN TEXT GOES HERE'NULL'joeschmoe@domain.ext');

?>

Added on Nov 16th at 7 pm by Scott - 1 Comment
Updated on Feb 22nd at 7 pm.

1 Response to sendmail

Scott commented on Feb 22nd at 7 pm
Fixed a type which had preg_math instead of preg_match

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: