无法通过自定义电子邮件发送Woocommerce数据

时间:2018-10-09 12:00:17

标签: php wordpress woocommerce wordpress-theming hook-woocommerce

我在我的functions.php中放置了以下代码。

我想从Woocommerce / Wordpress创建的新用户帐户中获取一些特定字段。

但是没有发送电子邮件(用于测试)吗? 有人知道为什么会这样吗? 我认为这与用于获取特定字段的类/函数有关。

我想通过电子邮件发送以下字段: 用户名,密码,公司名称,电子邮件地址,姓氏和名字

我之所以要通过电子邮件发送给我,是为了测试并查看所有野外工作是否有效。

当我看到通过接收电子邮件一切正常时,我想用对第三方API的发布请求替换电子邮件功能,以便我可以在Wordpress中未内置的第三方网站上创建完全相同的用户帐户

但是目前主要的问题是没有发送电子邮件。 这与课程有关吗?

class NewCustomerRegistration
{
    private $credentials;

    public function __construct()
    {
        // Use PHP_INT_MAX so that it is the last filter run on this data
        // in case there are other filter changing the password, for example
        add_filter(
            'woocommerce_new_customer_data',
            [$this, 'when_user_is_created'],
            1,
            PHP_INT_MAX
        );
        add_action('woocommerce_new_customer', [$this, 'when_customer_is_created']);
    }

    public function when_user_is_created($customerData)
    {
        $this->credentials = [
            'email'     => $customerData['user_email'],
            'password'  => $customerData['user_pass'],

        ];

        return $customerData;
    }

    public function when_customer_is_created($customerId)
    {
        $customer = get_userdata($customerId);

        /**
         * Perform the required actions with collected data
         *
         * Email: $this->credentials['email']
         * Password: $this->credentials['password']
         * First Name: $customer->first_name
         * Last Name: $customer->last_name
         */

    // Testing sending email function
    $subject = "Test Email with data";
    $email   = "info@mydomain.com";

    $message  = "Hello, {$customer->first_name}\n\n";
    $message .= "Here are your login details for {$customer->first_name} {$customer->last_name}\n\n";
    $message .= "Your company is: {$customer->company}\n\n";
    $message .= "Your username is: {$this->credentials['email']}\n\n";    
    $message .= "Your password is: {$this->credentials['password']}\n\n";
    $message .= "Your email is: {$this->credentials['email']}\n\n";
    $message .= "Your role is: {$customer->role}\n\n";

    $headers = array();

    add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';});
    $headers[] = 'From: My Wordpress Website <info@mydomain.com>'."\r\n";
    wp_mail( $email, $subject, $message, $headers);

   // Reset content-type to avoid conflicts
   remove_filter( 'wp_mail_content_type', 'set_html_content_type' );

    }
}

0 个答案:

没有答案