将BCC / CC添加到Contact Form 7电子邮件

huangapple go评论57阅读模式
英文:

Add BCC / CC to Contact Form 7 emails

问题

如何在WordPress中使用Contact Form 7和Contact Form 7钩子添加抄送/密送?

add_action( 'wpcf7_before_send_mail', 'dynamic_addcc' );


<details>
<summary>英文:</summary>

How do I add a CC/BCC to a WordPress using Contact Form 7 using the Contact Form 7 hook? 

add_action( 'wpcf7_before_send_mail', 'dynamic_addcc' );


</details>


# 答案1
**得分**: 1

如果您登录到您的WordPress管理界面,然后在仪表板下的最左侧点击“联系人”图标。

一旦您这样做了,如果您点击“联系表单”按钮,然后点击“邮件”选项卡。

在邮件选项卡下,您会看到一个“附加标头”框。在附加标头框中,您可以输入CC和BCC电子邮件地址。

关于此的详细信息:

https://michael-shore.uk/blog/add-cc-and-bcc-to-a-contact-form-7-form/

或者您可以尝试:

```php
add_action('wpcf7_before_send_mail','dynamic_addcc');

function dynamic_addcc($WPCF7_ContactForm){

    // 检查联系表单ID。
    if (12345 == $WPCF7_ContactForm->id()) {

        $currentformInstance  = WPCF7_ContactForm::get_current();
        $contactformsubmition = WPCF7_Submission::get_instance();

        if ($contactformsubmition) {

            $bcc_email = array('email1@example.com', 'email2@example.com', 'email3@example.com');

            // 用逗号分隔所有电子邮件。
            $cclist = implode(', ',$bcc_email);

            $data = $contactformsubmition->get_posted_data();

            if (empty($data))
                return;

            $mail = $currentformInstance->prop('mail');

            if(!empty($cclist)){
                $mail['additional_headers'] = "Bcc: $cclist";
            }

            // 保存电子邮件正文
            $currentformInstance->set_properties(array(
                "mail" => $mail
            ));

            // 返回当前cf7实例
            return $currentformInstance;
        }
    }
}

希望您找到这个有用。

英文:

If you login to your WordPress admin and click on the Contact icon on the far left, under the dashboard.

Once you've done that if you click on the Contact Form button, and then click on the Mail tab.

Under the mail tab you will see an "Additional headers" box. In the additional headers box, you can enter the CC and BCC e-mail addresses.

Details on this:

https://michael-shore.uk/blog/add-cc-and-bcc-to-a-contact-form-7-form/

Or you could try:

add_action(&#39;wpcf7_before_send_mail&#39;,&#39;dynamic_addcc&#39;);

function dynamic_addcc($WPCF7_ContactForm){

// Check contact form id.
if (12345 == $WPCF7_ContactForm-&gt;id()) {

    $currentformInstance  = WPCF7_ContactForm::get_current();
    $contactformsubmition = WPCF7_Submission::get_instance();

    if ($contactformsubmition) {

        $bcc_email = array(&#39;email1@example.com&#39;, &#39;email2@example.com&#39;, &#39;email3@example.com&#39;);

        // saparate all emails by comma.
        $cclist = implode(&#39;, &#39;,$bcc_email);

        $data = $contactformsubmition-&gt;get_posted_data();

        if (empty($data))
            return;

        $mail = $currentformInstance-&gt;prop(&#39;mail&#39;);

        if(!empty($cclist)){
            $mail[&#39;additional_headers&#39;] = &quot;Bcc: $cclist&quot;;
        }

        // Save the email body
        $currentformInstance-&gt;set_properties(array(
            &quot;mail&quot; =&gt; $mail
        ));

        // return current cf7 instance
        return $currentformInstance;
    }
}


}

Hope you find this useful.

答案2

得分: 0

/**
 * 为所有WordPress邮件添加抄送和密送
 * 作者:Archie M
 * 
 */
function dynamic_addcc($WPCF7_ContactForm){

	$currentformInstance  = WPCF7_ContactForm::get_current();
	$contactformsubmition = WPCF7_Submission::get_instance();

	$form_id = $currentformInstance->id;

	// 如果您不想在每个表单上都添加这个,请使用表单ID
	if ($contactformsubmition && ($form_id === 100) ) {

		$bcc_email = array('joe.doe@gmail.com');  // 抄送或密送

		// 如果有多个邮箱,请用逗号分隔
		$cclist = implode(', ',$bcc_email);

		$data = $contactformsubmition->get_posted_data();

		if (empty($data))
			return;

		$mail = $currentformInstance->prop('mail');

		if(!empty($cclist)){
			$mail['additional_headers'] = "Bcc: $cclist";
		}

		// 保存邮件正文
		$currentformInstance->set_properties(array(
			"mail" => $mail
		));

		// 返回当前cf7实例
		return $currentformInstance;
	}

}
add_action( 'wpcf7_before_send_mail', 'dynamic_addcc' );
英文:

This is how, I commented where possible.

/**
 * Add Cc &amp; Bcc to all WordPress mails
 * @author: Archie M
 * 
 */
function dynamic_addcc($WPCF7_ContactForm){

	$currentformInstance  = WPCF7_ContactForm::get_current();
	$contactformsubmition = WPCF7_Submission::get_instance();

	$form_id = $currentformInstance-&gt;id;

	// If you don&#39;t want this on every form, use the form ID instead
	if ($contactformsubmition &amp;&amp; ($form_id === 100) ) {

		$bcc_email = array(&#39;joe.doe@gmail.com&#39;);  // CC or BCC

		//separate all emails by comma if more than one
		$cclist = implode(&#39;, &#39;,$bcc_email);

		$data = $contactformsubmition-&gt;get_posted_data();

		if (empty($data))
			return;

		$mail = $currentformInstance-&gt;prop(&#39;mail&#39;);

		if(!empty($cclist)){
			$mail[&#39;additional_headers&#39;] = &quot;Bcc: $cclist&quot;;
		}

		// Save the email body
		$currentformInstance-&gt;set_properties(array(
			&quot;mail&quot; =&gt; $mail
		));

		// return current cf7 instance
		return $currentformInstance;
	}

}
add_action( &#39;wpcf7_before_send_mail&#39;, &#39;dynamic_addcc&#39; );

huangapple
  • 本文由 发表于 2023年5月30日 01:52:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定