简体   繁体   English

为什么我收到一条错误,指出“mail()期望参数1是字符串”?

[英]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.

相关问题 为什么我得到copy()期望参数2是PHP中的有效路径 - Why I am getting copy() expects parameter 2 to be a valid path in PHP 警告:mail()期望参数4是字符串 - warning: mail() expects parameter 4 to be string 为什么我在我的网站顶部收到“警告:array_filter() 期望参数 1 是数组,给定布尔值”? - Why am I getting "Warning: array_filter() expects parameter 1 to be array, bool given" at the top of my website? 使用 WordPress 上的复制器插件安装克隆站点时,为什么会出现安装错误,指出“wp-config.php 为空”? - When using the duplicator plugin on WordPress to install a cloned site, why am I getting an install error stating 'wp-config.php is empty'? 为什么我收到“mysql_num_rows() 期望参数 1 为资源,给出字符串”这个警告? - Why I'm getting 'mysql_num_rows() expects parameter 1 to be resource, string given' this warning? 我不断收到此错误警告:mysql_query() 期望参数 1 为字符串 - i keep getting this error Warning: mysql_query() expects parameter 1 to be string 为什么我可能会收到“mysqli_stat() 期望参数 1 为 mysqli”错误? - Why might I be getting "mysqli_stat() expects parameter 1 to be mysqli" error? Yii mail() 期望参数 1 是字符串,给定数组 - Yii mail() expects parameter 1 to be string, array given 由于htmlentities()期望参数1为字符串,给定数组,因此出现错误 - Getting Error as htmlentities() expects parameter 1 to be string, array given 我收到一个错误“警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在 C:\\xampp\\htdocs\\....” - I am getting an error "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\...."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM