0

我做了一个简单的应用程序,在一个用户注册后,一封欢迎邮件会发送到他的地址。一切正常。但是,当我尝试将文件附加到邮件(在本例中为徽标)时,页面开始加载并且......永远不会完成。并且不要发送邮件。你知道为什么会发生这种情况吗?

这是 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();
4

1 回答 1

0

你可能会发现这是因为你$this->options不需要$this->option。你可能还需要一个正斜杠之后你public_path();喜欢这样:

$this->options = function($message)
{
 $message->attach(public_path().'/img/logo.gif');
};
于 2013-11-15T17:19:27.440 回答