Function bulild email HTML message & attachment

Hi - I have an email form I have been working on and I want to recieve a HTML formatted email rather than just text. - Also, I want to add an attachment feature which I have been tried but failed - below is the code:

<?php

$my_email = "name@domain.com";

$continue = "/";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Attachment

function email_attachment($my_email, $file_location, $default_filetype='application/zip') {

$fileatt = $file_location;
    if(function_exists(mime_content_type)){
        $fileatttype = mime_content_type($file_location); 
    }else{
        $fileatttype = $default_filetype;;
    }
    $fileattname =$_REQUEST['attachment']($file_location);
    //prepare attachment
    $file = fopen( $fileatt, 'rb' ); 
    $data = fread( $file, filesize( $fileatt ) ); 
    fclose( $file );
    $data = chunk_split( base64_encode( $data ) );
    //create mime boundary
    $semi_rand = md5( time() ); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = stripslashes($message);

$message = "This is a multi-part message in MIME format.\
\
" . 
"--{$mime_boundary}\
" . 
"Content-type: text/html; charset=us-ascii\
" . 
"Content-Transfer-Encoding: 7bit\
\
" .

//create attachment section
$message .= "--{$mime_boundary}\
" . 
"Content-Type: {$fileatttype};\
" . 
" name=\\"{$fileattname}\\"\
" . 
"Content-Disposition: attachment;\
" . 
" filename=\\"{$fileattname}\\"\
" . 
"Content-Transfer-Encoding: base64\
\
" . 
$data . "\
\
" . 
"--{$mime_boundary}--\
";

$subject = "General Enquiry";

$subject = stripslashes($subject);

$headers .= "Date: ".date("r")."\
";

$headers .= "MIME-Version: 1.0\
" . 
				"Content-Type: multipart/mixed;\
" . 
					" boundary=\\"{$mime_boundary}\\"";

$from_Name = "";

if(isset($_REQUEST['Name']) && !empty($_REQUEST['Name'])){$from_Name = stripslashes($_REQUEST['Name']);}

$headers = "From: {$from_Name} <{$_REQUEST['Email']}>";

mail($my_email,$subject,$message,$headers);

$message = "Thank you for your enquiry. We will be in touch with you as soon as possible.

Regards,

Company Name
http://www.domain.com";

$subject = "General Enquiry";

$headers = "From: Company Name <noreply@domain.com>";

mail($_REQUEST['Email'],$subject,$message,$headers); }

?>

You can see where the attempted code to add the attachment feature is, and when I test the form the page loads through but I don’t return any errors so it makes it hard for me to spot the mistake.

If anyone can see any mistakes I would greatly appreciate it.

Cheers,

Paul

Ok cool thanks - I will take a look into this.

The code I posted is for a simple enquiry form where a user will input their data in the required fields and then they will receive a text email (which I want to make HTML) - is this straight forward to do?

Cheers,

Paul

If you want to send attachments and HTML mail, then I’d suggest you look into using a library such as phpmailer or my favourite library, [URL=“http://swiftmailer.org/”]Swift - it’ll save you hours and hours of development time.

Yeah, very.

I haven’t looked at your code extensively (i.e. not at all :wink: ), but sending an email with Swift would look something like:


// get a mailer
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());

// create a message
$message = Swift_Message::newInstance('Wonderful Subject');
$message->setFrom(array('john@doe.com' => 'John Doe'));
$message->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'));

// add html content
$message->setBody('My <em>amazing</em> body', 'text/html');

// add plain text content
$message->addPart('My amazing body in plain text', 'text/plain')

// add an attachment
$message->attach(Swift_Attachment::fromPath('/my/attachment/file.zip'));

// send the message
$result = $mailer->send($message);