1

我有一个 PHP 函数来发送电子邮件,

function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc)
    {
        if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
        {
            require_once "/usr/local/lib/php/Mail.php";

            $from = $email_from;
            $to = $email_to;
            $subject = $email_subject;
            $body = $email_body;

            $host = "mail.domain.co.uk";
            $username = "sending@domain.co.uk";
            $password = "********";

            $headers = array ('From' => $from,
              'To' => $to,
              'Cc' => $cc,
              'Subject' => $subject,
              'Content-type' => 'text/html');
            $smtp = Mail::factory('smtp',
              array ('host' => $host,
             'auth' => true,
             'username' => $username,
             'password' => $password));

             $rec = $to.', '.$cc;

            $mail = $smtp->send($rec, $headers, $body, $cc);
        }
    }

当我调用该函数时,有时没有$cc变量,所以我收到警告说Missing argument 6 for sendemail(),

如果 $cc 无效,停止警告的最佳方法是什么?

4

4 回答 4

7

如果您编写了该函数,则可以将第 6 个参数设为可选:

function sendemail($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc = null) {
    if ($cc !== null) {
        // add cc headers, e.g.
        // $headers['Cc'] = $cc;
    }
}

然后,您可以选择省略此参数:

sendemail("to@example.com", "from@example.com", "subject", "body", "replyto@example.com");
sendemail("to@example.com", "from@example.com", "subject", "body", "replyto@example.com", "cc@example.com");
于 2013-09-05T10:17:36.507 回答
1

用这个

function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc = "")
于 2013-09-05T10:16:51.993 回答
0

Try this,

function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc=NULL)

put $cc = NULL. So you will not get warning if there is no $cc .

于 2013-09-05T10:18:54.530 回答
0

If you are able to change the send email function:

function sendemail ($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc=null) { }

Just make sure that the function body itself will not have problem with a null $cc.

于 2013-09-05T10:19:44.853 回答