[英]Why am I getting an error stating that “mail() expects parameter 1 to be string”?
So basically, I am trying to create a bulk email application. 基本上,我正在尝试创建批量电子邮件应用程序。
I am allowing the upload of a CSV file with a few emails, and once I click send they will all be processed and sent. 我允许上传包含几封电子邮件的CSV文件,一旦我点击发送,他们就会被处理和发送。
I parse the CSV file into an array, and I am using a foreach
loop to handle each email address and send it. 我将CSV文件解析成一个数组,我使用foreach
循环来处理每个电子邮件地址并发送它。
For example, this is called after the csv
array has been populated: 例如,在填充csv
数组之后调用它:
foreach($csv as $email_address)
{
$sendHeaders = "headers here";
$sendSubject = "subject here";
$sendText = "long email text here";
mail($email_address,$sendSubject,$sendText,$sendHeaders);
}
Although, I seem to get the error of: 虽然,我似乎得到错误:
Warning: mail() expects parameter 1 to be string
It states that the error is on line 54, which is the line where my mail()
function is being called. 它声明错误在第54行,这是我的mail()
函数被调用的行。
Does anyone have any idea to why this is happening? 有谁知道为什么会这样?
EDIT: 编辑:
Here is a var_dump of my array if anyone is after it: 这是我的数组的var_dump,如果有人在它之后:
Array
(
[0] => Array
(
[0] => email1@foo.com
[1] => email2@foo.com
[2] => email3@foo.com
)
)
Change your loop to: 将你的循环改为:
foreach($csv as $email_addresses) {
foreach($email_addresses as $email_address) {
$sendHeaders = "headers here";
$sendSubject = "subject here";
$sendText = "long email text here";
mail($email_address,$sendSubject,$sendText,$sendHeaders);
}
}
In fact the problem was that your $csv
is a two-level array, hence its elements are not strings, you must iterate on its elements to get the actual email addresses. 实际上问题是你的$csv
是一个两级数组,因此它的元素不是字符串,你必须迭代它的元素才能得到实际的电子邮件地址。
Also, if $sendHeaders
and/or $sendSubject
are always the same, you could move them before the loop so you do not set them each time. 此外,如果$sendHeaders
和/或$sendSubject
始终相同,您可以在循环之前移动它们,这样您就不会每次都设置它们。
Lastly, as @Dagon suggested, if $csv
always has only one element, you could simply do: 最后,正如@Dagon建议的那样,如果$csv
总是只有一个元素,你可以简单地做:
foreach($csv[0] as $email_address) {
$sendHeaders = "headers here";
$sendSubject = "subject here";
$sendText = "long email text here";
mail($email_address,$sendSubject,$sendText,$sendHeaders);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.