如何在codeigniter中向两个不同的电子邮件地址发送电子邮件

时间:2016-04-20 03:51:32

标签: php codeigniter email

我正在尝试使用codeigniter电子邮件库向两个不同的电子邮件地址发送电子邮件。

private static void ExecuteQuery(string connectionString, string query)
{
    SqlConnection connection = new SqlConnection(connectionString);
    DataTable output = null;

    while output is null
    {
        output = getDataFromDB(query);
    }

    if(output is DataTable && output.Rows.Count > 0)
    {
        Console.WriteLine("There are " + output.Rows.Count + " clusters within the capacity analysis.");
    }
}

private DataTable getDataFromDB(string query)
{
    DataTable oDTResult = null;

    try
    {
        //create new SqlAdataAdapter
        SqlDataAdapter command = new SqlDataAdapter {SelectCommand = new SqlCommand(query, connection)};

        //connect to Sqldb
        connection.Open();

        //validate connection to database before executing query
        if (connection.State != ConnectionState.Open) return;
        Console.WriteLine("Connection successful\nExecuting query...");

        //set connection timeout
        command.SelectCommand.CommandTimeout = 200;

        //create new dataSet in order to input output of query to
        DataSet dataSet = new DataSet();

        //fill the dataSet
        command.Fill(dataSet, "capacity");
        DataTable dtTable1 = dataSet.Tables["capacity"];            

        oDTResult = dtTable1;
    }
    catch (Exception e)
    {
        Console.WriteLine("Unable to execute capacity (all records) query due to {0}", e.Message);
    }
    finally
    {
        connection.Close();

        Declarations.NumOfClusters = output.Rows.Count;
        Declarations.finalIssues = Issues(output, 2m, 20, true);

        Console.WriteLine("\n---------------Successfully Created Capacity DataSet---------------\n");
    }

    return oDTResult;
}

我创建了一个包含两个电子邮件地址的数组。

这里的问题是邮件只发送到一个电子邮件地址,即$ email_address而不是另一个。

你能帮助我吗?

4 个答案:

答案 0 :(得分:1)

看起来你的代码很好,但是当to方法接受array或逗号分隔的字符串时,请尝试更改为逗号分隔的字符串:

$this->email->to("$email_address, example@yahoo.com");

注意:让您使用真实电子邮件而不是example@yahoo.com,如果您遗漏了任何内容,请查看documentation

答案 1 :(得分:0)

请尝试以下代码:

$mail_array = array($email_address, 'example@yahoo.com');
foreach($mail_array as $email) {
    $this->email->to($email);   
}

答案 2 :(得分:0)

您可以向CC添加第二封电子邮件,

$this->email->cc('email@email.com');

答案 3 :(得分:0)

CodeIgniter 中,发送电子邮件不仅简单,而且您可以动态配置或在配置文件中设置首选项。

  

<强> $这 - &GT;的电子邮件 - &gt;至()

     

设置收件人的电子邮件地址。可以是一封电子邮件,   以逗号分隔的列表或数组:

     

$这 - &GT;的电子邮件 - &gt;到(&#39; someone@example.com');

     

$ this-&gt; email-&gt; to(&#39; one @ example.com,two @ example.com,three @ example.com&#39;);

     

$这 - &GT;的电子邮件 - &gt;至(           数组(&#39; one@example.com',&#39; two@example.com',&#39; three@example.com'));

有关详情,请参阅此链接 - http://www.codeigniter.com/user_guide/libraries/email.html#class-reference

希望这会对你有所帮助!!