首页 > 解决方案 > 函数在 PHP 邮件正文中不起作用

问题描述

我用它来发送一个基本的电子邮件:

// Email details
$name = 'Davo';
$recipient = 'info@davo.com';
$from = 'sender@example.com'
$subject = 'Testing';

// All plugins
function the_plugins() {
    $the_plugs = get_option('active_plugins'); 
    foreach($the_plugs as $key => $value) { 
        $string = explode('/',$value); print $string[0] . '<br />';
    }
}

// Message
$body = '<p>Hello' . $name . ',</p>';
$body .= '<p>Your website has these plugins:</p>';
$body .= the_plugins();
$body .= '<p>Have a nice day.</p>';

$headers[]  = 'Content-type:text/html;charset=UTF-8';
$headers[]  = 'From' . $name. ' <' . $from . '>';
$headers[]  = 'Reply-To: ' . $from;
$headers[]  = 'MIME-Version: 1.0';
wp_mail($recipient, $subject, $body, $headers);

唯一不起作用的是the_plugins()功能不会在到达的电子邮件中显示任何内容。相反,它在我希望看到插件列表的行中只是空的,看起来像这样:

Hello Davo,
Your website has these plugins:

Have a nice day.

仅供参考the_plugins()功能有效。我能够echo the_plugins(); exit;在函数之后立即返回插件列表,因此函数本身不是问题。

关于如何解决这个问题的任何建议?

标签: phpwordpressfunctionemail

解决方案


尝试这个,

function the_plugins() {
    $the_plugs = get_option('active_plugins'); 
    $plugins = '';
    foreach($the_plugs as $key => $value) { 
        $string = explode('/',$value); 
        $plugins .= $string[0] . '<br />';
    }
   return $plugins;
}

推荐阅读