在php中使用require和include_once函数

时间:2018-01-06 02:06:08

标签: php

是否可以在函数中使用require和include_once。

我正在尝试使用sendGrid发送电子邮件。我创建了一个名为sendMail的函数。

这样的事情:

function sendEmail($to, $from, $subject, $message)
{

  // You need to install the sendgrid client library so run: composer require sendgrid/sendgrid
    require './vendor/autoload.php';
    // contains a variable called: $API_KEY that is the API Key.
    // You need this API_KEY created on the Sendgrid website.
    include_once('./credentials.php');

    $FROM_EMAIL = $from;
    $From_Name = $FROM_EMAIL;
    // they dont like when it comes from @gmail, prefers business emails
    $TO_EMAIL = $to;
    // Try to be nice. Take a look at the anti spam laws. In most cases, you must
    // have an unsubscribe. You also cannot be misleading.
    $subject = $subject;
    $from = new SendGrid\Email($From_Name, $FROM_EMAIL);
    $to = new SendGrid\Email(null, $TO_EMAIL);
    $htmlContent = $message;
    // Create Sendgrid content
    $content = new SendGrid\Content("text/html",$htmlContent);
    // Create a mail object
    $mail = new SendGrid\Mail($from, $subject, $to, $content);

    $sg = new \SendGrid($API_KEY);
    return $sg->client->mail()->send()->post($mail);
}

我使用composer将sendgrid库安装到我的libs文件夹中。这个文件夹也有functions.php文件,但是我收到以下错误:

Fatal error: require(): Failed opening required './vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\samplepoject\libs\functions.php on line 65 

只是想知道如何在函数内调用require和include_once?有人可以帮助我吗?

谢谢!

0 个答案:

没有答案