我做了一个简单的应用程序,在一个用户注册后,一封欢迎邮件会发送到他的地址。一切正常。但是,当我尝试将文件附加到邮件(在本例中为徽标)时,页面开始加载并且......永远不会完成。并且不要发送邮件。你知道为什么会发生这种情况吗?
这是 Mailer 抽象类:
<?php
namespace Mailers;
class InvalidContactInfoException extends \Exception {}
abstract class Mailer {
public $to;
public $email;
public $subject;
public $view;
public $data;
public $options;
public function __construct($user)
{
if(!is_object($user))
{
throw new InvalidContactInfoException('Please pass a valid user object');
}
$this->data = $user->toArray();
$this->to = $user->username;
$this->email = $user->email;
}
public function deliver()
{
$self = $this;
return \Mail::send($this->view, $this->data, function($message) use($self)
{
$message->to($self->email, $self->to)->subject($self->subject);
if(is_callable($self->option))
{
call_user_func($self->option, $message);
}
});
}
}
这是 userMailer 类:
<?php
namespace Mailers;
class UserMailer extends Mailer {
public function welcomeMail()
{
$this->subject = 'Welcome in my website';
$this->view = 'emails.user.welcome2';
$this->option = function($message) // -- HERE IS WHERE I ATTACH THE FILE --
{
$message->attach(public_path().'img/logo.gif');
};
return $this;
}
}
我从 PostRegister 方法内部调用 Class,就在将用户保存到数据库中之前:
$mailer = new Mailers\UserMailer($user);
// var_dump($mailer);
$mailer->welcomeMail()->deliver();
$user->save();