PhpMailer从表单捕获电子邮件地址并添加到变量

时间:2017-11-24 12:13:46

标签: php forms email variables phpmailer

我正在一个网站上工作,该网站上有一个表格捕获名称,电子邮件等。一切正常,但我想将CCed电子邮件添加到填写表单的用户。

我想将提供的电子邮件地址添加到varable中,然后使用AddCC向他们发送电子邮件,如下所示:

$mail->AddCC 'test@test.com';

这样可以正常使用,但如何将电子邮件地址变为可变数据?

这个PHP脚本和表单如下。 任何帮助都会很棒, 谢谢, 詹姆斯

//PHP

    public function SendEmail( $email_address_array, $email_data, $subject, $template, &$send_message ) {

    $smartyObj = Configurator::getInstance()->smarty;   
    $send_message = '';
    $send_result = 0;

    try { 

        $mail = new PHPMailer( true );      
        $mail->IsSMTP( true );  // telling the class to use SMTP
        $mail->SMTPDebug = false;
        $mail->IsHTML( true );
        $mail->Host = Configurator::getInstance()->getSettings( "phpmailer_smtp" ); 

        $mail->ClearAddresses();
        for( $x = 0;$x < sizeof($email_address_array);$x++ ){
            $mail->AddAddress( trim($email_address_array[$x]));
        }           

        $smartyObj->assign( 'email_data', $email_data );
        $mail->SetFrom( 'info@forexchange.co.uk', 'Forexchange Currency Order');


         //  NEW TEST
        //$mail->AddCC ($usersemail);


        $mail->Subject  = $subject;
        $mail->Body     = $smartyObj->fetch( $template );           
        if(!$mail->Send()) {
        } else {
            $send_result = 1;
        }
    } catch (phpmailerException $e) {
        $send_message = $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        $send_message = $e->getMessage(); //Boring error messages from anything else!
    }
    //echo $send_result;
    //exit;
    return $send_result;
}       

// FORM

<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">ID Number</td>
    <td>{$email_data.id_order|escape:"html"}</td>
</tr> 
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Submission Date</td>
    <td>{$email_data.cdate|escape:"html"}</td>
</tr> 
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Name</td>
    <td>{$email_data.name|escape:"html"}</td>
</tr> 
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Email Address</td>
    <td>{$email_data.email|escape:"html"}</td>
</tr>
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Phone</td>
    <td>{$email_data.phone|escape:"html"}</td>
</tr>
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Currency</td>
    <td>{$email_data.currency_code|escape:"html"}</td>
</tr> 
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Amount</td>
    <td>{$email_data.amount|escape:"html"}</td>
</tr>
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Rate</td>
    <td>{$email_data.rate|escape:"html"}</td>
</tr>    
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Pickup Point</td>
    <td>{$email_data.pickup|escape:"html"}</td>
</tr>
<tr>
    <td style="font-weight:bold; padding-right:15px; text-align:right;">Purchace Buy Back Product</td>
    <td>{if $email_data.purchase_buy_back_status eq '1'}Yes{else}No{/if}</td>
</tr>                

1 个答案:

答案 0 :(得分:0)

在您的示例中,您只需在AddCC中使用for loop发送电子邮件。

for( $x = 0;$x < sizeof($email_address_array);$x++ ){
    // Email to User
    $mail->AddAddress(trim($email_address_array[$x]));
    // Email to Admin
    $mail->AddCC(trim($email_address_array[$x]));
}

并且,如果您使用Post methodemail[]之类的形式传递电子邮件,则以下代码将有效。

foreach($_POST['email'] as $key => $val){
    // Check if Email coming
    echo $_POST['email'][$key];
    // Send Email Script
    $mail->AddCC(trim($_POST['email'][$key]));
}    

以下代码将从包含电子邮件内容的$ email_data变量中提取电子邮件。

$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i'‌​; 
preg_match_all($pattern, $email_data, $matches); 
$email = $matches[0];