我做了一个简单的应用程序,在一个用户注册后,一个欢迎邮件被发送到他的地址。 一切正常。 但是当我试图将文件附加到邮件(在这种情况下是徽标)时,页面开始加载并且......永远不会完成。并且不要发送邮件。 你知道为什么会这样吗?
这是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();
答案 0 :(得分:0)
您可能会发现这是因为您需要$this->options
而不是$this->option
。在public_path();
之后你可能还需要一个正斜杠:
$this->options = function($message)
{
$message->attach(public_path().'/img/logo.gif');
};