I need to create a very simple script with php. Just a simple form with an input field for the users name and another for the users email address. They click submit, and the script sends an email to that address, while attaching a PDF file that I specify in the script.

I've found many scripts out there that encompassed the user attaching a file and submitting but not the opposite where the user just gives their name & desired email address and the email & attachment is sent to them.

As a bonus I was hoping to either (A) have the script log the users name & email and email it to me or (B) blanket carbon copy me on the email it sends out to the user so I can capture the users information.

Thank you in advance!

Is your simple mail working on server.

I am not sure what you mean. When I referenced "simple" I meant a less complex method. But either way, everything I have been able to find only had the user filling out the form and including an attachment where what I want to do is have the user fill in the form with their email and have the script send them the email with an attachment.

oh, and you can cc yourself but I don't recommend that.
you can send mail to more then one person so first send the user an email check for errors and if its good then send it to yourself.
I would save the data to a DB then access the data in the back-end, build yourself a nice archive.

I appreciate the responses so far but the first script doesn't have a user key in their name & email address so the script can respond with the email & attachment.

The second script (being over 1000+ lines) is hardly "simple" to interpret. I was looking for something similar to what else I have seen out there, just user form > script response versus user form with file > script to me with a file attachment.

The script I posted is a framework for sending email. If you want to respond with the script also. You need to create a script that save the users message into a database, the another that will populate the data based on the first scripts input and pass it to geekmail. It's relatively simple, but a process. A little html, php and mysql you can have exactly what you want.

not exactly sure what you mean? the script I sent:
http://www.webcheatsheet.com/PHP/sen...php#attachment
is a fairly simple tutorial on sending a file attachment but the script has some issues which are addressed in the comments section.
the script none the less is a good start: "the first script doesn't have a user key in their name & email address".
this is whats throwing me off, are you talking about the user info from the form? I hope not. Anyway I can whip up a function or maybe use the one at the bottom of that page with an example of how it works, if it will help. it won't be done until this weekend though. are you new to php or just the idea of sending attachments? it sounds like you need the form and script for sending the email, is that correct?

try this, its a nice class from kavoir.com
the test file shows its usage.

Here's my two cents

php mailer is pretty good for sending mail with attachments and other cool stuff


http://phpmailer.worxware.com/index.php?pg=phpmailer

Additionally, here's something I wrote as a wrapper class of the mail function to add attachments

<?php

class xmail{
    
    protected $body = array(
        'doctype'   =>  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
        'html'      =>  '<html xmlns="http://www.w3.org/1999/xhtml">',
        'meta'      =>  array(
            '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
        ),
        'reset'     =>  true,
        'styles'    =>  null,
        'title'     =>  '',
        'content'   =>  ''
    );
    
    protected $config = array();
    
    protected $attachment = array();
    
    protected $boundaryHash = '';
    
    public $log = array();
    
    protected static $DEBUG = true;
    
    public function __construct( array $args = array() ){
        
        $defaults = array(
            'to'        =>  array(),
            'subject'   =>  'mail',
            'headers'   =>  array(
                'From'          =>  'admin@example.com',
                'Content-Type'  =>  'text/html; charset=iso-8859-1',
                'MIME-Version'  =>  '1.0',
                'Reply-To'      =>  'noreply',
                'X-Mailer'      =>  'PHP/'.phpversion(),
                'Cc'            =>  null,
                'Bcc'           =>  null
            ),
            'type'      =>  'plain',
            'allowHtml' =>  false
        );
        
        if( isset($args['headers']) ){
            $defaults['headers'] = array_merge( $defaults['headers'], $args['headers'] );
            unset($args['headers']);
        }
        
        $this->config = array_merge( $defaults, $args );
        
        $this->boundaryHash = md5(date('r', time()));
        
    }
    
    public function setContentType( $type, $charset = 'charset=iso-8859-1' ){
        $this->config[ 'headers' ][ 'Content-Type' ] = "$type; $charset";
    }
    
    public function addAttachment( $file ){
        
        $defaults = array(
            'path'      =>  '',
            'name'      =>  '',
            'encoding'  =>  'base64',
            'type'      =>  'application/octet-stream'
        );
        
        if( is_array($file) ){
            
            $file = array_merge($defaults, $file);
            
        }else{
            
            $defaults[ 'path' ] = dirname($file);
            
            $defaults[ 'name' ] = basename($file);
            
            $file = $defaults;
            
        }
        
        $fullFile = self::addDS( $file[ 'path' ], $file[ 'name' ] );
        
        if( file_exists( $fullFile ) && is_file( $fullFile ) ){
            
            $this->attachment[] = $file;
            
            //$this->config[ 'type' ] = 'attachment';
            
        }
        
    }
    
    public function setBody( $message, $key = 'content' ){
        
        if( is_array($message) ){
            $this->body = array_merge( $this->body, $message ); 
        }else{
            $this->body[ $key ] = $message;
        }
        
    }
    
    public function setCSS( array $styles ){
        
        //$style['styles'] = $styles;
        $this->body[ 'styles' ] = $styles;
        //$this->setBody{ $style };
    }
    
    public function addAddress( $address, $name = null ){
        //<user@example.com>, Another User <anotheruser@example.com>
        if( !is_null($name) ){
            $email = $name . ' <' . self::isValidEmail($address) . '>';
        }else{
            $email = '<'.self::isValidEmail($address).'>';
        }
        
        $this->config['to'][] = $email;
        
    }
    
    protected function buildBody(){
        
        if( $this->config['allowHtml'] === false ){
            return self::stripHtml($this->body['content']);
        }
        
        $reset = '';
        
        if( $this->body['reset'] === true ){
            
            $reset = "
                #outlook a{padding:0;}
                body{width:100% !important;} .ReadMsgBody{width:100%;} .ExternalClass{width:100%;}
                body{-webkit-text-size-adjust:none; -ms-text-size-adjust:none;}
                body{margin:0; padding:0;}
                img{height:auto; line-height:100%; outline:none; text-decoration:none;}
                #backgroundTable{height:100% !important; margin:0; padding:0; width:100% !important;}
                p{margin: 1em 0;}
                h1, h2, h3, h4, h5, h6{color: black !important; line-height: 100% !important;}
                h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {color: blue !important;}
                h1 a:active, h2 a:active,  h3 a:active, h4 a:active, h5 a:active, h6 a:active{color: red !important;}
                h1 a:visited, h2 a:visited,  h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited{
                    color: purple !important;
                }
                .yshortcuts, .yshortcuts a, .yshortcuts a:link,.yshortcuts a:visited,
                .yshortcuts a:hover, .yshortcuts a span{
                    text-decoration: none !important; border-bottom: none !important; background: none !important;
                }
                table td{
                    border-collapse:collapse;
                }
            ";
            
        }
        
        ob_start();
            
            ?>
            <?php echo $this->body['doctype']; ?>
            <?php echo $this->body['html']; ?>
                
                <?php
                    foreach($this->body['meta'] as $meta){
                        echo $meta . "\n";
                    }
                ?>
                <title><?php echo $this->body['title']; ?></title>
                <style type="text/css">
                <!--
                    <?php echo $reset ?>
                    <?php
                        if(!is_null($this->body['styles']))  :
                        foreach($this->body['styles'] as $selector => $styles)    :
                            
                            $style = $selector . '{';
                            
                            if(is_array($styles)){
                                $style .= implode('; ', $styles);
                            }else{
                                $style .= "$styles;";
                            }
                            
                            $style .= "}\n";
                            
                            echo $style;
                            
                        endforeach; endif;
                    ?>
                    
                -->
                </style>
                <body>
                    <?php echo $this->body['content']; ?>
                </body>
            </html>
                
            <?php
            
        return ob_get_clean();
    }
    
    protected function buildHeader(){
        
        $headers = $this->config[ 'headers' ];
        
        $header = sprintf("From: %s", $headers['From']);
        $header .= sprintf("\r\nReply-To: %s", $headers['Reply-To']);
        
        
        if( !is_null($headers['Cc']) ){
            $header .= sprintf("\r\nCc: %s", $headers['Cc']);
        }
        
        if( !is_null($headers['Bcc']) ){
            $header .= sprintf("\r\nCc: %s", $headers['Bcc']);
        }
        
        $header .= sprintf("\r\nX-Mailer: %s", $headers['X-Mailer']);
        $header .= sprintf("\r\nMIME-Version: %s", $headers['MIME-Version']);
        
        return $header;
        
    }
    
    public function send(){
        
        $header = $this->buildHeader();
        
        if( count($this->attachment) == 0 ){
            
            $header .= sprintf("\r\nContent-Type: %s", $this->config[ 'headers' ]['Content-Type']);
            
            $body = $this->buildBody();
            
            
        }else{
            
            $mime_boundary = "==Multipart_Boundary_x{".$this->boundaryHash."}x";
            
            //$alt_boundary = "==Multipart_Alt_x{".$this->boundaryHash."}x";
            
            $header .= "\r\nContent-Type: multipart/mixed;\n boundary=\"{$mime_boundary}\"";
            
            $body = "--{$mime_boundary}";
            
            //$body .= "\nContent-Type: multipart/alternative; boundary=\"{$alt_boundary}\"";
            //
            //$body .= "\n\n--{$alt_boundary}";
            
            $body .= "\nContent-Type: ".$this->config[ 'headers' ]['Content-Type'];
            
            $body .= "\nContent-Transfer-Encoding: 7bit";
            
            $body .= "\n\n".$this->buildBody()."\n";
            
            $body .= "\n".$this->buildAttachment()."\n";
            
        }
        
        //var_dump($header);
        //var_dump($body);
        
        //try{
            
            $mailStatus = mail(
                implode( ', ', $this->config['to'] ),
                $this->config['subject'],
                $body,
                $header
            );
            
        //}catch(Exception $e){
        //    
        //    if( self::$DEBUG ){
        //        $this->log[] = 'On mail send: ' . $e;
        //    }
        //    
        //}
        
        //var_dump($this->config);
        return $mailStatus;
        
    }
    
    protected function buildAttachment(){
        
        if( count($this->attachment) == 0 || !is_array($this->attachment) ){
            return false;
        }
        
        $ath = '';
        
        $mime_boundary = "==Multipart_Boundary_x{".$this->boundaryHash."}x";
        
        foreach( $this->attachment as $attachment ){
                
            $fileName = self::addDS( $attachment[ 'path' ], $attachment[ 'name' ] );
            
            $handle = fopen($fileName, "rb");
            
            $data = fread( $handle,filesize($fileName) );
            
            fclose($handle);
            
            //just base64 for now
            switch( $attachment['encoding'] ){
                case 'base64' :
                    $data = chunk_split(base64_encode($data));
                    break;
            }
            
            //$attachment['type']
            $ath .= "--{$mime_boundary}\n";
            
            $ath .= "Content-Type: {$attachment['type']}; name=\"{$attachment[ 'name' ]}\"\n";
            
            $ath .= "Content-Disposition: attachment;\n" . " filename=\"{$attachment[ 'name' ]}\"; size=".filesize($fileName).";\n";
            
            $ath .= "Content-Transfer-Encoding: {$attachment['encoding']}\n\n" . $data . "\n\n";
            
        }
        
        $ath .= "--{$mime_boundary}--";
        
        return $ath;
        
    }
    
    protected function stripHtml( $content ){
        
        
        return filter_var( $content, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_HIGH );
        
       
        
    }
    
    public static function isValidEmail( $email ){
        
        if( is_array( $email ) ){
            
            if( count($email) > 1 ){
                
                $newEmail = array();
                
                foreach( $email as $key => $address ){
                    $check = self::isValidEmail( $address );
                    
                    if( $check === false ){
                        return false;
                    }
                    
                    $newEmail[$key] = $check;
                }
                
                return $newEmail;
                
            }else{
                $email = isset($email[0])?$email[0]:reset($email);
            }
            
        }
        
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
            return false;
        }
        
        return filter_var($email, FILTER_SANITIZE_EMAIL);
        
    }
    
    public static function addDS( $path, $name ){
        return $path . DIRECTORY_SEPARATOR . $name;
    }
    
    public function setDebug( $bool = true ){
        
        self::$DEBUG = $bool;
        
    }
    
    public static function getMime( $key ){
        /****
        *   A list of mime types
        ****/
        $mime = array(
            'hqx'   =>  'application/mac-binhex40',
            'cpt'   =>  'application/mac-compactpro',
            'doc'   =>  'application/msword',
            'bin'   =>  'application/macbinary',
            'dms'   =>  'application/octet-stream',
            'lha'   =>  'application/octet-stream',
            'lzh'   =>  'application/octet-stream',
            'exe'   =>  'application/octet-stream',
            'class' =>  'application/octet-stream',
            'psd'   =>  'application/octet-stream',
            'so'    =>  'application/octet-stream',
            'sea'   =>  'application/octet-stream',
            'dll'   =>  'application/octet-stream',
            'oda'   =>  'application/oda',
            'pdf'   =>  'application/pdf',
            'ai'    =>  'application/postscript',
            'eps'   =>  'application/postscript',
            'ps'    =>  'application/postscript',
            'smi'   =>  'application/smil',
            'smil'  =>  'application/smil',
            'mif'   =>  'application/vnd.mif',
            'xls'   =>  'application/vnd.ms-excel',
            'ppt'   =>  'application/vnd.ms-powerpoint',
            'wbxml' =>  'application/vnd.wap.wbxml',
            'wmlc'  =>  'application/vnd.wap.wmlc',
            'dcr'   =>  'application/x-director',
            'dir'   =>  'application/x-director',
            'dxr'   =>  'application/x-director',
            'dvi'   =>  'application/x-dvi',
            'gtar'  =>  'application/x-gtar',
            'php'   =>  'application/x-httpd-php',
            'php4'  =>  'application/x-httpd-php',
            'php3'  =>  'application/x-httpd-php',
            'phtml' =>  'application/x-httpd-php',
            'phps'  =>  'application/x-httpd-php-source',
            'js'    =>  'application/x-javascript',
            'swf'   =>  'application/x-shockwave-flash',
            'sit'   =>  'application/x-stuffit',
            'tar'   =>  'application/x-tar',
            'tgz'   =>  'application/x-tar',
            'xhtml' =>  'application/xhtml+xml',
            'xht'   =>  'application/xhtml+xml',
            'zip'   =>  'application/zip',
            'mid'   =>  'audio/midi',
            'midi'  =>  'audio/midi',
            'mpga'  =>  'audio/mpeg',
            'mp2'   =>  'audio/mpeg',
            'mp3'   =>  'audio/mpeg',
            'aif'   =>  'audio/x-aiff',
            'aiff'  =>  'audio/x-aiff',
            'aifc'  =>  'audio/x-aiff',
            'ram'   =>  'audio/x-pn-realaudio',
            'rm'    =>  'audio/x-pn-realaudio',
            'rpm'   =>  'audio/x-pn-realaudio-plugin',
            'ra'    =>  'audio/x-realaudio',
            'rv'    =>  'video/vnd.rn-realvideo',
            'wav'   =>  'audio/x-wav',
            'bmp'   =>  'image/bmp',
            'gif'   =>  'image/gif',
            'jpeg'  =>  'image/jpeg',
            'jpg'   =>  'image/jpeg',
            'jpe'   =>  'image/jpeg',
            'png'   =>  'image/png',
            'tiff'  =>  'image/tiff',
            'tif'   =>  'image/tiff',
            'css'   =>  'text/css',
            'html'  =>  'text/html',
            'htm'   =>  'text/html',
            'shtml' =>  'text/html',
            'plain' =>  'text/plain',
            'txt'   =>  'text/plain',
            'text'  =>  'text/plain',
            'log'   =>  'text/plain',
            'rtx'   =>  'text/richtext',
            'rtf'   =>  'text/rtf',
            'xml'   =>  'text/xml',
            'xsl'   =>  'text/xml',
            'mpeg'  =>  'video/mpeg',
            'mpg'   =>  'video/mpeg',
            'mpe'   =>  'video/mpeg',
            'qt'    =>  'video/quicktime',
            'mov'   =>  'video/quicktime',
            'avi'   =>  'video/x-msvideo',
            'movie' =>  'video/x-sgi-movie',
            'doc'   =>  'application/msword',
            'word'  =>  'application/msword',
            'xl'    =>  'application/excel',
            'eml'   =>  'message/rfc822'
        );
        
        $key = strtolower( trim( $key ) );
        
        if( isset($mime[ $key ]) ){
            return $mime[ $key ];
        }
        
        if( self::$DEBUG ){
            $this->log[] = "Cannot find mime type [$key]";
        }
        
        return false;
        
    }
    
}

/*
ex usage:
$mail = new xmail(array(
            'headers'   =>  array('From'=>'who@ami.com'),
            'subject'   =>  'the subject',
            'allowHtml' =>  false
        ));
        
	$mail->setContentType(xmail::getMime('plain'));
	
        $mail->addAddress('to@example.com');
        
        $message = "
            Yep nope
        ";
	
        $mail->setBody($message);
        
	$mail->addAttachment('path/to/file');
	
	$mailStatus = $mail->send(); //returns mail
*/

?>

I would recommend using phpmailer instead since the class I wrote is still a work in progress.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.