Skip to content

Commit 3f60321

Browse files
committed
Version 3.3
1 parent 7bd21c9 commit 3f60321

File tree

7 files changed

+87
-64
lines changed

7 files changed

+87
-64
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ This is where solutions like Emailqueue come in handy: Emailqueue is not an SMTP
2222
* Inject any number of emails super-fast and inmediately free your app to do other things. Let Emailqueue do the job in the background.
2323
* Prioritize emails: Specify a priority when injecting an email and it will be sent before any other queued emails with lower priorities. E.g: You can inject 100k emails for a newsletter with priority 10 (they will take a while to be sent), and still inject an important email (like a password reminder message) with priority 1 to be sent ASAP even before the huge newsletter has been sent.
2424
* Schedule emails: Inject now an email and specify a future date/time for a scheduled delivery.
25-
* The code is quite naive, built in the early 2000s. But boy, it's been tested! This means it will be very easy for you if you decide to branch/fork it and improve it. Emailqueue is a funny grown man.
25+
* The code is quite naive, built in the early 2000s. But boy, it's been tested! This means it will be very easy for you if you decide to branch/fork it and improve it. Emailqueue is a funny grown old man.
2626

2727

2828
# Changelog #
29+
* **Version 3.3**
30+
* Support for Emoji.
31+
* Errors are now thrown using standard PHP Exceptions and are easily catchable.
32+
2933
* **Version 3.2**
3034
* Switched to MIT license, now Emailqueue can be used in commercial, non GNU-GPL projects.
3135
* emailqueue_inject::inject method is now called differently, see the "How to use" section or example.php for for info.
3236
* "is_send_now" emailqueue_inject::inject parameter sends the email without waiting for the next queue processing event, perfect for those cases where you can't even wait a minute to have your email delivered.
3337
* Now uses composer for library dependencies.
3438
* Using standard <?php instead of the shorthand <? version for improved compatibility.
3539
* No need to configure LIB_DIR or APP_DIR anymore.
36-
* "is_inmediate" parameter typo solve, now it's called "is_immediate"
40+
* "is_inmediate" parameter typo solve876543 , now it's called "is_immediate"
3741

3842
* **Version 3.1**
3943
* Finally Emailqueue supports attachments! See the "Hints" section for an interesting idea with this.
@@ -49,6 +53,7 @@ This is where solutions like Emailqueue come in handy: Emailqueue is not an SMTP
4953
* Recode the file logging system.
5054
* Recode it to modern standards.
5155
* A way to provide secured hard-links to view individual emails, so that a hard link can be included within the email to the user with a link like "Can't see this message? click here to see it in your browser"
56+
* A better back-end to explore the queue in realtime, plus some beautiful statistical graphs.
5257

5358
# How to install #
5459
* Clone the emailqueue repository wherever you want it.
@@ -95,21 +100,19 @@ This is where solutions like Emailqueue come in handy: Emailqueue is not an SMTP
95100
* Send emails using the provided emailqueue_inject PHP class found in scripts/emailqueue_inject.class.php. See an example on how to use this class in example.php
96101
* You can also inject messages to the queue by manually inserting them on the database via SQL (Insert in the "emails" table, read the field comments for detailed explanations)
97102

103+
# Migrate from Version 3.2 to Version 3.3 #
104+
* Run `$ git pull` in your emailqueue directory to get the latest version.
105+
* Using your database manager, select your emailqueue database and run the install/migrate_from_v3.2_to_v3.2.sql SQL file.
98106

99-
# Migrate to Version 3.2 #
107+
# Migrate from Version 3.1 to Version 3.2 #
100108
* Install this new 3.2 version following the regular Install process.
101109
* Set up the same database connection parameters as your existing Emailqueue database. Note that some minor things have changed in db.config.inc.php and application.config.inc.php, so you cannot use the old ones.
102110
* Using your database manager, select your emailqueue database and run the install/migrate_from_v3.1_to_v3.2.sql SQL file.
103-
* run `$ composer update` on the installation dir to get the latest phpmailer libraries.
111+
* Run `$ composer update` on the installation dir to get the latest phpmailer libraries.
104112

105113

106114
# Migrating from versions older than v3.1 #
107-
If you have a version of emailqueue older than v3.1 (released on december 26th, 2015), and want to upgrade to v.3.1 or above, execute the following SQL in your emailqueue database in order to migrate:
108-
109-
`ALTER TABLE emails ADD attachments TEXT NULL DEFAULT NULL;`
110-
`ALTER TABLE emails ADD is_embed_images TINYINT(1) NOT NULL DEFAULT 0;`
111-
112-
No other changes are needed for the migration.
115+
If you have a version of emailqueue older than v3.1 (released on december 26th, 2015), and want to upgrade to v.3.1 or above, using your database manager, select your emailqueue database and run the install/migrate_from_versions_older_than_v3.1.sql SQL file.
113116

114117
# How to use #
115118
The file example.php is a thoroughly documented example on how to send an email using emailqueue using the provided emailqueue_inject PHP class, which is the recommended method. Here's what to do, anyway:

common.inc.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Emailqueue;
77

8-
define("VERSION", "3.2");
8+
define("VERSION", "3.3");
99
define("OFFICIAL_PAGE_URL", "https://github.com/tin-cat/emailqueue");
1010

1111
require_once(dirname(__FILE__)."/config/db.config.inc.php");
@@ -24,13 +24,12 @@
2424
EMAILQUEUE_DB_DATABASE
2525
);
2626
if (!$db->connect()) {
27-
echo "Cannot connect to database";
27+
throw new EmailqueueException("Cannot connect to database");
2828
die;
2929
}
3030

3131
$db->query("set names UTF8");
3232

33-
3433
require("classes/out.class.php");
3534
global $output;
3635
$output = new \Emailqueue\output;
@@ -154,8 +153,9 @@ function deliver_emails(&$mail, $emails, $isOutputVerbose = false) {
154153

155154
// Check if maximum delivery timeout have been reached
156155
if ((mktime() - $timecontrol_start) > MAXIMUM_DELIVERY_TIMEOUT) {
157-
if ($isOutputVerbose)
156+
if ($isOutputVerbose) {
158157
echo "Delivery proccess automatically stopped before it finished because of too many time spent on delivering. Time spent: ".(mktime() - $timecontrol_start)." seconds. Maximum time allowed: ".MAXIMUM_DELIVERY_TIMEOUT." seconds\n";
158+
}
159159
$logger->add_log_incidence(
160160
array(
161161
0,

example.php

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,41 @@
1111

1212
$emailqueue_inject = new Emailqueue\emailqueue_inject(EMAILQUEUE_DB_HOST, EMAILQUEUE_DB_UID, EMAILQUEUE_DB_PWD, EMAILQUEUE_DB_DATABASE); // Creates an emailqueue_inject object. Needs the database connection information.
1313

14-
$result = $emailqueue_inject->inject([
15-
"foreign_id_a" => false,
16-
"foreign_id_b" => false,
17-
"priority" => 10,
18-
"is_immediate" => true,
19-
"date_queued" => false,
20-
"is_html" => true,
21-
"from" => "from@email.com",
22-
"from_name" => "From name",
23-
"to" => "lorenzo@tin.cat",
24-
"replyto" => "replyto@email.com",
25-
"replyto_name" => "Replyto email",
26-
"subject" => "Test email from emailqueue",
27-
"content" => "<html><body>Test <b><i>HTML</i></b> message.",
28-
"content_nonhtml" => false,
29-
"list_unsubscribe_url" => false,
30-
"attachments" => [
31-
[
32-
"path" => __DIR__."/frontend/gfx/img/logo_small.png", // Required. PHP must have permissions enough to read this file.
33-
"fileName" => "logo_small.png", // Optional. Emailqueue will extract the filename from the path if not specified.
34-
"encoding" => "base64", // Optional. Defaults to "base64"
35-
"type" => "image/png" // Optional. Emailqueue will try to determine the type
36-
],
37-
[
38-
"path" => __DIR__."/frontend/gfx/img/item.gif"
39-
]
40-
],
41-
"is_embed_images" => true,
42-
"custom_headers" => false,
43-
"is_send_now" => false
44-
]);
14+
try {
15+
$result = $emailqueue_inject->inject([
16+
"foreign_id_a" => false,
17+
"foreign_id_b" => false,
18+
"priority" => 10,
19+
"is_immediate" => true,
20+
"date_queued" => false,
21+
"is_html" => true,
22+
"from" => "from@email.com",
23+
"from_name" => "From name",
24+
"to" => "lorenzo@tin.cat",
25+
"replyto" => "replyto@email.com",
26+
"replyto_name" => "Replyto email",
27+
"subject" => "Test email from emailqueue",
28+
"content" => "<html><body>Test <b><i>HTML</i></b> message with some emoji 🚀",
29+
"content_nonhtml" => false,
30+
"list_unsubscribe_url" => false,
31+
"attachments" => [
32+
[
33+
"path" => __DIR__."/frontend/gfx/img/logo_small.png", // Required. PHP must have permissions enough to read this file.
34+
"fileName" => "logo_small.png", // Optional. Emailqueue will extract the filename from the path if not specified.
35+
"encoding" => "base64", // Optional. Defaults to "base64"
36+
"type" => "image/png" // Optional. Emailqueue will try to determine the type
37+
],
38+
[
39+
"path" => __DIR__."/frontend/gfx/img/item.gif"
40+
]
41+
],
42+
"is_embed_images" => true,
43+
"custom_headers" => false,
44+
"is_send_now" => false
45+
]);
46+
} catch (Exception $e) {
47+
echo "Emailqueue error: ".$e->getMessage()."<br>";
48+
}
4549

4650
if($result)
4751
echo "Message correctly injected.<br>";

install/emailqueue.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ CREATE TABLE `emails` (
1515
`date_sent` datetime DEFAULT NULL COMMENT 'The date/time this email was delivered. Do not specify this field when manually inserting.',
1616
`is_html` tinyint(1) UNSIGNED DEFAULT NULL COMMENT 'Whether this email''s content is HTML.',
1717
`from` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The email address of the sender.',
18-
`from_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Optional. The name of the sender.',
18+
`from_name` varchar(255) CHARACTER SET utf8 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'Optional. The name of the sender.',
1919
`to` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The email address of the recipient.',
2020
`replyto` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The email addess where replies to this message will be sent by default.',
21-
`replyto_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'The name where this email will be replied by default.',
22-
`subject` tinytext CHARACTER SET utf8 COLLATE utf8_unicode_ci COMMENT 'The email''s subject',
23-
`content` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci COMMENT 'The content of the email. If HTML, be sure to set the is_html field to 1',
24-
`content_nonhtml` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci COMMENT 'Optional. A plain-text version of the email for old clients.',
21+
`replyto_name` varchar(255) CHARACTER SET utf8 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'The name where this email will be replied by default.',
22+
`subject` tinytext CHARACTER SET utf8 COLLATE utf8mb4_bin COMMENT 'The email''s subject',
23+
`content` longtext CHARACTER SET utf8 COLLATE utf8mb4_bin COMMENT 'The content of the email. If HTML, be sure to set the is_html field to 1',
24+
`content_nonhtml` longtext CHARACTER SET utf8 COLLATE utf8mb4_bin COMMENT 'Optional. A plain-text version of the email for old clients.',
2525
`list_unsubscribe_url` varchar(255) DEFAULT NULL COMMENT 'Optional. The URL where users can unsubscribe from the newsletter. Highly recommended.',
2626
`attachments` text CHARACTER SET utf8 COLLATE utf8_unicode_ci COMMENT 'A serialized array of hash arrays specifying the files to be attached to this email. See example.php on how to build this array.',
2727
`is_embed_images` tinyint(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Whether to automatically convert <IMG ... /> tags found on the email content to embedded images that are transferred along with the email itself instead of being referenced to external URLs. Can bring you some interesting benefits, but also hugely increases the data transfer.',

install/migrate_from_v3.2_to_v3.3.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE `emails` CHANGE `from_name` `from_name` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL;
2+
ALTER TABLE `emails` CHANGE `replyto_name` `replyto_name` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL;
3+
ALTER TABLE `emails` CHANGE `subject` `subject` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL;
4+
ALTER TABLE `emails` CHANGE `content` `content` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL;
5+
ALTER TABLE `emails` CHANGE `content_nonhtml` `content_nonhtml` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE emails ADD attachments TEXT NULL DEFAULT NULL;
2+
ALTER TABLE emails ADD is_embed_images TINYINT(1) NOT NULL DEFAULT 0;

scripts/emailqueue_inject.class.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ function __construct($db_host, $db_user, $db_password, $db_name, $avoidpersisten
3232

3333
function db_connect() {
3434
if(!$this->connectionid = mysqli_connect($this->db_host, $this->db_user, $this->db_password)) {
35-
trigger_error("Emailqueue Inject class component: Cannot connect to database");
35+
$this->error("Cannot connect to database on ".$this->db_host." for user ".$this->db_user);
3636
die;
3737
}
38-
mysqli_select_db($this->connectionid, $this->db_name);
39-
mysqli_query($this->connectionid, "set names utf8");
38+
if (!mysqli_select_db($this->connectionid, $this->db_name)) {
39+
$this->error("Cannot select database ".$this->db_name);
40+
die;
41+
}
42+
if (!mysqli_query($this->connectionid, "set names utf8mb4")) {
43+
$this->error("Cannot set names to UTF8");
44+
die;
45+
}
4046
}
4147

4248
function db_disconnect() {
@@ -93,24 +99,24 @@ function inject($p) {
9399
// Prepare and check attachments array
94100
if ($attachments) {
95101
if (!is_array($attachments)) {
96-
trigger_error("Emailqueue inject error: attachments parameter must be an array.");
102+
$this->error("Attachments parameter must be an array.");
97103
return false;
98104
}
99105
foreach ($attachments as $attachment) {
100106
if (!is_array($attachment)) {
101-
trigger_error("Emailqueue inject error: Each attachment specified on the attachments array must be a hash array.");
107+
$this->error("Each attachment specified on the attachments array must be a hash array.");
102108
return false;
103109
}
104110
if (!file_exists($attachment["path"])) {
105-
trigger_error("Emailqueue inject error: Can't open attached file for reading.");
111+
$this->error("Can't open attached file for reading.");
106112
return false;
107113
}
108114
}
109115
}
110116

111117
if ($custom_headers) {
112118
if (!is_array($custom_headers)) {
113-
trigger_error("Emailqueue inject error: custom headers parameter must be an array.");
119+
$this->error("Custom headers parameter must be an array.");
114120
return false;
115121
}
116122
}
@@ -178,11 +184,16 @@ function inject($p) {
178184
)
179185
"
180186
);
187+
188+
if (!$result) {
189+
$this->error("Error inserting message in the queue DB");
190+
die;
191+
}
181192

182193
if ($is_send_now) {
183194
$email_id = mysqli_insert_id($this->connectionid);
184195
if (!$result = mysqli_query($this->connectionid, "select * from emails where id = ".$email_id)) {
185-
trigger_error("Emailqueue inject error: Couldn't retrieve the recently inserted email for 'send now' delivery.");
196+
$this->error("Couldn't retrieve the recently inserted email for 'send now' delivery.");
186197
return false;
187198
}
188199
$email = $result->fetch_assoc();
@@ -252,16 +263,14 @@ function empty_queued() {
252263
*/
253264
function empty_all() {
254265
$this->db_connect();
255-
$result = mysqli_query($this->connectionid, "
256-
delete from
257-
emails
258-
");
266+
$result = mysqli_query($this->connectionid, "delete from emails");
259267
$this->db_disconnect();
260268
return $result ? true : false;
261269
}
262270

263-
// Deprecated method, left for compatibility purposes. Will likely be removed on future versions.
264-
function destroy() {}
271+
function error($description) {
272+
throw new \Exception($description);
273+
}
265274
}
266275

267276
?>

0 commit comments

Comments
 (0)