php 将数据数组作为电子邮件发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10578531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:30:25  来源:igfitidea点击:

Sending array of data as email

phparraysemail

提问by Shin

I have a PHP array, and I need to test the content of that array via an email. I'm aware that we can see the entire array using var_dump(), but how can I send that output in an email?

我有一个 PHP 数组,我需要通过电子邮件测试该数组的内容。我知道我们可以使用 看到整个数组var_dump(),但是如何在电子邮件中发送该输出?

采纳答案by Moyed Ansari

try with following code, it will show the complete array clearly

尝试使用以下代码,它将清楚地显示完整的数组

echo "<pre>";
print_r($array);
echo "</pre>";

回答by Sampson

You can use print_r( $array, true )to get the output as a string. You can then pass this into your message body. The second paramter instructs the method to return the value rather than output it directly, permitting you to handle the results.

您可以使用print_r( $array, true )将输出作为字符串。然后,您可以将其传递到您的消息正文中。第二个参数指示方法返回值而不是直接输出它,允许您处理结果。

$message = "Results: " . print_r( $array, true );

回答by muni

First Convert your array string using foreach() or implode function. I am using foreach to convert array to string..

首先使用 foreach() 或 implode 函数转换数组字符串。我正在使用 foreach 将数组转换为字符串..

Where string will be key and value pairs.

其中 string 将是键和值对。

$data = '';
foreach ($array as $key=>$value){
    $data .= $key.'-------'.$value;
    $data.= "\n";
}

or use following code to convert array to string.

或使用以下代码将数组转换为字符串。

$data = implode("\n", $array);

Now send that using php mail function.

现在使用php邮件功能发送。

mail($recipient, $subject, $data, $headers);

回答by Frankey

mail('[email protected]', 'array data', '<pre>'.print_r($array, true).'</pre>');

http://php.net/manual/en/function.mail.php

http://php.net/manual/en/function.mail.php

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