使用 laravel 发送电子邮件时出错

Error throwing while sending an email using laravel

将 Web 应用程序迁移到新服务器后,使用 laravel 发送电子邮件时抛出错误。它在以前的服务器上运行良好。

相关错误描述编辑自评论:

[2018-01-25 13:31:10] production.ERROR: ErrorException: mkdir(): No such file or directory in /var/www/html/jbservice/vendor/swiftmailer/swiftmailer/lib/c‌​lasses/Swift/KeyCach‌​e/Disk$ Stack trace: #0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleErro‌​r(2, 'mkdir(): No suc...', '/var/www/html/j...', 273, Array) #1 /var/www/html/jbservice/vendor/swiftmailer/swiftmailer/lib/c‌​lasses/Swift/KeyCach‌​e/DiskKeyCache.php(2‌​73): mkdir('/tmp/68d392a3e3...')

根据评论编辑的相关代码:

Mail::send('dashboard.emails.createticket', $data , function ($message) 
use ($data) { $message->subject('New Ticket: ' . $data['subject']) 
    ->to("atif@gmail.com") 
    ->from('HERE COMES THE SENDER EMAIL'); });

您可以查看您的日志。

如果它显示 SwiftMailer 正在尝试在默认 /tmp 文件夹中创建缓存:

要解决此问题,请更改 app/Providers/AppServiveProvider.php.

的 boot() 方法中的 TMPDIR 环境变量
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    /**
     * Somehow PHP is not able to write in default /tmp directory and SwiftMailer was failing.
     * To overcome this situation, we set the TMPDIR environment variable to a new value.
     */
    if (class_exists('Swift_Preferences')) {
        \Swift_Preferences::getInstance()->setTempDir(storage_path().'/tmp');
    } else {
        \Log::warning('Class Swift_Preferences does not exists');
    }
}

请确保新的“tmp”文件夹位置可由网络服务器写入。