Skip to content

Multi sending #75

Description

@WinterSilence

Example:

$mailer = Email::forge();
$body = View::forge('email/template', $data);
$mailer->html_body($body);
foreach ($mailing_list as $user) {
   $body->set('user', $user);
   $mailer->to($user['email'])->send();
}

Code like this throw error or work slow because multi sending not supported.
Rework:

  1. body/alt_body typed in Email_Driver::send(), not in setters
  2. Add method Email_Driver::before_send() called in Email_Driver::send() init properties only once
  3. Parse attachments from body is bad solution.
    Also:
    $cid = 'cid:'.md5(pathinfo($image_url, PATHINFO_BASENAME));

    'second/img.jpg' rewrite 'firtst/img.jpg'

Email\View_Body would solve it:

namespace Email;
use Fuel\Core\View;
class View_Body extends View
{
    protected $email;
    protected $attachments = [];
    
    public function __construct(Email_Driver $email, $file = null, $data = [], $filter = null)
    {
        $this->email = $email;
        $this->bind('email', $this->email);
        parent::__construct($file, $data, $filter);
    }
    
    public function set_filename($file, $reverse = false)
    {
        // TODO: add in config `'base_dir' => 'emails'` - directory with mail templates
        if ($dir = $this->email->get_config('email.defaults.base_dir'))
        {
            $file = $dir.DIRECTORY_SEPARATOR.$file;
        }
        
        return parent::set_filename($file, $reverse);
    }
    public function render($file = null)
    {
        // TODO: update method `clear_attachments` for optional clear
        $this->email->clear_attachments('inline');
        $this->attachments = [];
        
        return parent::render($file);
    }
    /**
     * Template helper.
     * @example `<img src="<?=$this->attach_inline($url)?>">`
     * @param string $path Path/URL to file
     * @return string CID
     */
    protected function attach_inline($path)
    {
        $id = 'cid:'.md5($path);
        if (!in_array($id, $this->attachments))
        {
            $this->email->attach($path, true, $id);
            $this->attachments[] = $id;
        }
        return $id;
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions