PHP表单附件

时间:2016-11-24 22:05:43

标签: php email attachment email-attachments

此表单会捕获一些潜在客户信息,然后通过电子邮件向客户发送带有潜在客户数据的电子邮件。我已将文件上传按钮添加到表单中,但我尝试过的其他内容似乎都没有附加到电子邮件中。我曾尝试过一些教程,但我是菜鸟,我在这里所拥有的东西似乎与其他方法不相符。任何帮助将不胜感激!

形式:

    <form id='email_form'>
            <table border="0" bordercolor="#FFFFFF" style="background-color:#FFFFFF" width="400" cellpadding="0" cellspacing="0">
    <tr>
        <td >First Name:<br><input style="" type='text' name='FName_req' value=''></td>
        <td></td>
    </tr>
        <tr>
        <td >Last Name:<br><input style="" type='text' name='LName_req' value=''></td>
        <td></td>
    </tr>
    <tr>
        <td>Phone:<br><input style="" type='text' name='Phone_req' value=''></td>
        <td></td>
    </tr>
        <tr>
        <td>E-Mail:<br><input style="" type='text' name='Email_req' value=''></td>
        <td></td>
    </tr>
        <tr>
        <td>Address:<br><input style="" type='text' name='Address1_req' value=''></td>
        <td></td>
    </tr>
            <tr>
        <td><input style="" type='text' name='Address2' value=''></td>
        <td></td>
    </tr>
                <tr>
        <td>Postal Code:<br><input style="" type='text' name='PostalCode_req' value=''></td>
        <td></td>
    </tr>
    </tr>
                <tr>
        <td>Image:<br><input type="file" name="fileField"></td>
        <td></td>
    </tr>
    <tr >
        <td style="padding-top:5px;"><input style="margin-top:15px;" type='checkbox' name='Communications_Agreement' value='***Permission Granted***'><br>Yes I would like to receive communications. </td>
        <td></td>
    </tr>

</table>

            <br>


<button id='email_submit'>Submit</button>
    <p id='form_msg'>

        <span id='form_done' style="color:red;">Thank you! Your information has been sent.</span>
        <span id='form_err_email'>Error: Problem sending email</span>
        <span id='form_err_info'>Error: Missing required information</span>
        <span id='form_working'><img src='/files/ajax-loader.gif' alt='working...' /></span>
    </p>

    <input type='hidden' name='prog_url' value='{$prog_url}' />
    <input type='hidden' name='period' value='{$period}' />

</form>

form_email.php

<?php

session_start();    // for captcha

// validate the fields
foreach( $_POST as $name => $val ) {
    if ( preg_match( '/.*_req/', $name) && !$val ) {
        die( 'info' );
    }
}

// validate captcha
if (array_key_exists( 'captcha_code', $_POST )) {   // ZZZZZZZZ testing via curl
    include_once 'securimage/securimage.php';
    $securimage = new Securimage();
    if ($securimage->check( $_POST[ 'captcha_code' ] ) == false ) {
        die( 'captcha' );
    }
}

///// prep email /////
// bootstrap libraries
define("PATH_BASE", dirname(__FILE__).'/' );
// Load config -- Use buffering to ensure bogus whitespace in config.php is ignored
ob_start(NULL, 2048);
include PATH_BASE . 'config.php';
include PATH_BASE . 'user_config.php';
include PATH_BIN . 'lib.php';
ob_end_clean();
include PATH_BIN.'inc.objs.php';

// get program, agent name, and "to" emails
$prog_url =  $_POST['prog_url'];
$period =  $_POST['period'];
param_set( 'period', $period );
param_set( 'prog_url', $prog_url );
if (!prog_init()) { error(ERR_PROGRAMS_NO_LOAD); }
if (!agent_init()) { error(ERR_AGENTS_NO_LOAD); }
if (!terr_init( make_dname( 'data', 'period' ) )) { error(ERR_TERRITORIES_NO_LOAD); }
$prog = prog_find_by_url( $prog_url );
if (!$prog) { error( "$prog_url is not a statistics page.", 404 ); }
extract( $prog );
$agent = agent_find( $prog_agentid );
if (!$agent) { error( "Could not locate agent $prog_agentid.", 404 ); }
//~ trace_var( '$agent', $agent );
extract( $agent );
$agent_name = $agent_name2 ? "$agent_name1 and $agent_name2" : $agent_name1;

// compose message
$recipients = array(
    'to' => $prog_form_email_to,
    'bcc' => FORM_EMAIL_TO_ALSO
);

$logo_url = $_SERVER[ 'HTTP_ORIGIN' ] . dirname( $_SERVER[ 'REQUEST_URI' ] ) . MR_LOGO_URL;

// in case the template embeds a trace
$trace = '';
//~ $trace = print_r( $_SERVER , true);

// compose form field text
$SKIP = array( 'captcha_code', 'prog_url', 'period' );
$res = array();
foreach( $_POST as $name => $val ) {
    if (in_array( $name, $SKIP )) { continue; }
    if ( !$val ) { continue; }
    if ( preg_match( '/(.*)_req/', $name, $m) ) { $name = $m[1]; }
    $res[] = "<tr><th>$name:</th><td>$val</td></tr>";
}
$form_email_info = '<table>'.join($res).'</table>';

// render it here to capture all vars!!
ob_start(NULL, 2048);
include FORM_EMAIL_TEMPLATE;
$body = ob_get_contents();
ob_end_clean();

//~ die($body);

// send email
mr_email( FORM_EMAIL_FROM, $recipients, FORM_EMAIL_SUBJECT, $body, true );

die( 'done' );

?>

电子邮件模板:

<?php echo <<<END

<p> Dear ${agent_name}, </p> {$trace} <p> A user from "{$prog_title}"
submitted the following information: </p>

{$form_email_info}


<p> Sincerely, </p>

<img alt="Logo" src="$logo_url" /> <p><i>This is an automated
email</i></p>

END;

?>

0 个答案:

没有答案