php app邮件功能

时间:2017-09-21 08:59:11

标签: php email email-integration

我在从envato购买的php ecomm应用程序中使用邮件功能时遇到问题。开发人员具有邮件程序功能,用于从网站发送电子邮件。 功能:

/*  Send mail with custom templates:$template : E-mail template.$array : Variables for email template. $subject : E-mail Subject.$to : E-mail receiver.*/

function mailing($template,$array,$subject,$to) {
    $cfg = DB::select('SELECT * FROM config WHERE id = 1')[0];
    $array['url'] = url('');
    $array['name'] = $cfg->name;
    $array['address'] = nl2br($cfg->address);
    $array['phone'] = $cfg->phone;
    $array['email'] = $cfg->email;
    // Get the template from the database
    $message = DB::select("SELECT template FROM templates WHERE code = '".$template."'")[0]->template;
    foreach ($array as $ind => $val) {
        $message = str_replace("{{$ind}}",$val,$message);
    }   
    $message = preg_replace('/\{\{(.*?)\}\}/is','',$message);
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: '.$cfg->name.' <'.$cfg->email.'>'."\r\n";
    mail($to,$subject,$message,$headers);
    return true;
}

电子邮件模板是从数据库中提取的。

作为API.php文件中结帐流程的一部分,邮件程序功能用于通过以下代码将订单通过电子邮件发送给用户:

// Send an email to customer
    mailing(
            'order',
            array(  'buyer_name'=>$data['name'],
                    'buyer_email'=>$data['email'],
                    'buyer_fields'=>$email_fields,
                    'name'=>$this->cfg->name,
                    'address'=>$this->cfg->address,
                    'phone'=>$this->cfg->phone,
                    'products'=>$email_products,
                    'total'=>$total
                ),
            'Order Confirmation #'.$order,
            $data['email']
        );

这很好,但是,我试图让自己发送订单的电子邮件,而不是必须检查管理员ux,因为它目前的工作方式。有人能指出我正确的方向吗?

非常感谢提前。

2 个答案:

答案 0 :(得分:0)

只需添加另一个

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

$to替换为您的电子邮件。那就是它。

答案 1 :(得分:0)

简单的答案是使用mail()功能中的电子邮件地址向$to添加mailing()替换function mailing($template,$array,$subject,$to, $toMe=false) { // new parameter ^^^^^^^^^^^ $cfg = DB::select('SELECT * FROM config WHERE id = 1')[0]; $array['url'] = url(''); $array['name'] = $cfg->name; $array['address'] = nl2br($cfg->address); $array['phone'] = $cfg->phone; $array['email'] = $cfg->email; // Get the template from the database $message = DB::select("SELECT template FROM templates WHERE code = '".$template."'")[0]->template; foreach ($array as $ind => $val) { $message = str_replace("{{$ind}}",$val,$message); } $message = preg_replace('/\{\{(.*?)\}\}/is','',$message); $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: '.$cfg->name.' <'.$cfg->email.'>'."\r\n"; mail($to,$subject,$message,$headers); // now mail yourself if $toMe is true if ( $toMe !== false ) { mail($toMe,$subject,$message,$headers); } return true; }

但由于这可能在应用程序的多个位置使用,因此向该函数添加一个参数可能会很高兴,该参数告诉它通过电子邮件发送给您以及预期的收件人

mailing()

然后在应用程序中发送邮件的任何地方都可以添加请求以获取电子邮件的副本。但是,如果您不添加新参数,// Send an email to customer mailing( 'order', array( 'buyer_name'=>$data['name'], 'buyer_email'=>$data['email'], 'buyer_fields'=>$email_fields, 'name'=>$this->cfg->name, 'address'=>$this->cfg->address, 'phone'=>$this->cfg->phone, 'products'=>$email_products, 'total'=>$total ), 'Order Confirmation #'.$order, $data['email'], 'me@mymail.com' // new parameter added ); 函数将像以前一样运行。

mailing

现在对[{1}}的现有调用都不会生效,因为你在函数原型中使用了一个默认值,除非你将额外的参数添加到mailing的调用中,但是你们中的任何一个都是想要改变可以轻松完成,当您想要获得副本时指定地址,您可以使用任何电子邮件地址。