Skip to content

trejjam/Emailing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emailing

Library for

  • managing emails
  • their groups
  • send email to group(s)
  • save email to IMAP

Installation

The best way to install Trejjam/Emailing is using Composer:

$ composer require trejjam/emailing

Configuration RabbitMq

Look how to configure Kdyby/RabbitMq This extension need only 'rabbitmq.connection'

extensions:
	rabbitmq: Kdyby\RabbitMq\DI\RabbitMqExtension

rabbitmq:
	connection:
		default:
			host: localhost
			port: 5672
			user: 'guest'
			password: 'guest'
			vhost: '/'

Configuration

.neon

extensions:
	emailing: Trejjam\DI\EmailingExtension

emailing:
	tables   : 
		emails      : 
			table : send__emails
			id    : id
			email : email		
		groups      : 
			table    : send__groups
			id       : id
			parentId : parent_id
			name     : name
			type     : 
				name    : type
				options : [public, private]
		group_email : 
			table      : send__group_email
			id         : id
			groupId    : group_id
			emailId    : email_id
			newsletter : 
				name    : newsletter
				options : [enable, disable]
		senders     : 
			table      : send__sender
			id         : id
			email      : email
			configName : config_name
	connect  : 
		default : 
			smtp          : FALSE #TRUE - use SmtpMailer NULL - disable connection FALSE - use SendmailMailer
			host          : NULL
			smtp_port     : NULL
			username      : NULL
			password      : NULL
			secure        : ssl

			imap          : FALSE
			imap_host     : TRUE #TRUE -> use host
			imap_port     : 993
			imap_param    : /imap/ssl/validate-cert #Flags from http:#php.net/manual/en/function.imap-open.php
			imap_username : TRUE #TRUE -> use username
			imap_password : TRUE #TRUE -> use password
		other:
			...
	rabbitmq : 
		mailer    : 
			templateDir    : '/presenters/templates/'  
			defaultTemplate: 'emails/default.latte'
		imap      : 
			sendFolder : 'Sent.From web'        
		producers : 
			mailer : 
				name       : NULL #NULL -> use mailer 
				connection : default
				exchange   : NULL #NULL -> use mailer
			imap   : 
				name       : NULL #NULL -> use imap
				connection : default
				exchange   : NULL
		consumers : 
			mailer : 
				connection : default
				exchange   : NULL
				queue      : NULL            
			imap   : 
				connection : default
				exchange   : NULL
				queue      : NULL
	cache    :
		use     : TRUE
		name    : emailing
		timeout : 60 minutes

Config

The best way for configuration is using Kdyby/Console

$ composer require kdyby/console

Read how to install Kdyby/Console

php index.php

After successful installation display:

Available commands:
Emailing
	Emailing:imap       Basic IMAP task
	Emailing:groups     Edit groups
	Emailing:install    Install default tables
	help                Displays help for a command
	list                Lists commands

Config database

Create default tables:

php index.php Emailing:install

Config imap

List folders:

php index.php Emailing:imap [connectionName=default] -l

Add folder:

php index.php Emailing:imap [connectionName=default] -c folderName [-l]

Config groups

Add group:

php index.php Emailing:groups groupName [parentName] -c [-t type] [-l]

Move group:

php index.php Emailing:groups groupName [parentName] -m [-l]

Edit group:

php index.php Emailing:groups groupName -t type [-l]

Delete group:

php index.php Emailing:groups groupName -r [-l]

List all groups:

php index.php Emailing:groups -l

Config senders

Add sender:

php index.php Emailing:senders senderEmail [connection=default] -c [-l]

Change connection:

php index.php Emailing:senders senderEmail [connection=default] [-l]

Delete sender:

php index.php Emailing:senders senderEmail -r [-l]

List all senders:

php index.php Emailing:senders -l

Usage

Presenter/Model:

	/**
	* @var \Trejjam\Emailing\RabbitMq\Mailer @inject
	*/
	public $rabbitMailer;
	/**
	 * @var \Trejjam\Emailing\Emails @inject
	 */
	public $emails;
	/**
	 * @var \Trejjam\Emailing\Groups @inject
	 */
	public $groups;
	/**
	 * @var \Trejjam\Emailing\Senders @inject
	 */
	public $senders;
	
	function renderDefault() {
		$this->senders->addSender("email@from.ltd");
		$this->senders->addSender("email2@from.ltd", "other");
	
		$message=$this->rabbitMailer->createEmail();
		$message->setSender("email@from.ltd"); //set from and connection
		$message->setImapSave(TRUE);
		
		$message->setTo("email@to.ltd");
		$this->rabbitMailer->send($message);
		//send email generated by defaultTemplate, send using default connection, save email to IMAP
		
		$message->setSender("email2@from.ltd");
		$message->setTemplate("newsletter.latte");
		$message->setImapFolder("Sent.newsletter");
		$message->setImapConnection("default");
		$message->setUnsubscribeEmail("unsubscribe-1@from.ltd");
		$message->setTo("email2@to.ltd");
		$this->rabbitMailer->send($message);
		//send email generated by newsletter.latte, send using 'other' connection, save email to IMAP using 'default'
				
				
		
		$email = $this->emails->addEmail("email@to.ltd");
		$email2 = $this->emails->addEmail("email2@to.ltd");
		$all=$this->groups->addGroup("all");
		$group = $this->groups->addGroup("newsletter", "all");
		
		$this->emails->addEmailToGroup($email, $group);
		$this->emails->addEmailToGroup($email2, $all);
		
		foreach ($all->getEmailsRecursive() as $v) {
			dump($v->getEmail()); //email2@to.ltd, email@to.ltd
		}
		foreach ($all->getEmails() as $v) {
			dump($v->getEmail()); //email2@to.ltd
		}
		foreach ($group->getEmails() as $v) {
			dump($v->getEmail()); //email@to.ltd
		}
		
		dump($this->groups->getGroup("all")->containEmail($email)); //FALSE
		dump($this->groups->getGroup("newsletter")->containEmail($email)); //TRUE
		$this->emails->removeEmail("email@to.ltd", TRUE);
		$this->emails->removeEmail("email2@to.ltd", TRUE);
	}