Email Notification System Using PHP MySQL - php

I am currently working on a project that requires sending email notifications to users. The way it works, users follow certain catergories of posts/group, whenever there is a new post or comment under the post category/group they get email message notifying them with a snippet of the post itself.
I am expecting to send upwards of 5000 emails daily. Is it effective to use cron job to fetch users and send the emails at intervals or is there a better means to push this email while avoiding blacklisting on my IP by email providers.
Below is my table structure
First Table
Notification
ID // autoincrement
message // the message sent to users
createDate // date created
Second Table
User_Notification
ID // auto increment
userid // Id of users
notification_id // id of notification from first table
status // 0 or 1
So whenever a post is added, I insert the notification in the first table (notification) and then fetch all followers of that group from another table (following) where i store userids and the group ids. I then insert for each user in the second table (user_notification).
My cron job fetchs like 20 records from user_notification table and also fetch the notification message from notification table. I also fetch user email from my user table.
$firstquery = $lnk->mysqli->query("select * from user_notification where status=0 order by id ASC limit 0, 20", MYSQLI_USE_RESULT);
$secondquery = $lnk->mysqli->query("select * from notification where id=$notification_id", MYSQLI_USE_RESULT);
$thirdquery = $lnk->mysqli->query("select email from user_table where id IN($userids)", MYSQLI_USE_RESULT);
for($no=0;$no<counter;$no++)// loop to send emails
{
$lnk->emailsender($subject,$emailbody,$usr_email[$no]);
}
Please is there any better way to do this?

Unless you have a very specific relationship with your ISP, it is extremly likely you will be blacklisted, or your e-mails will go straight to spam. It is difficult to establish mass e-mails as legitimate. You can use a mass mailing services API to do some of the dirty work for you, like mail chimp or constant contact.
So the code you have will most likely work, minus e-mailing bit which should be done via a mailing service and their associated API.

5000 emails a day is roughly 4 emails per minute, or one email every 15 seconds. If I were you I would write a cron routine to run every minute that will send the emails which are due to be sent. This will ensure you avoid being treated like a spammer for sending out 2500 emails quickly, for example. It also means your users get email notifications quickly.
You could also send out the emails as soon as a user gets a notification, bypassing the need for a cron routine however if your site hits a surge in activity for whatever reason you could find some emails don't get through.
You could also consider using a 3rd party service like MailChimp which already has good authority with email providers, however that comes at a price and for 4 emails a minute I wouldn't consider it worthwhile.

Related

Select all user accounts that have the same email from database

We have a database with around 900 accounts. A few clients have more than one account but they all have the same email. How can I effectively let the client select the account to view when logging in.
I have posted a screenshot here to help explain this. I am already logging in users with single account, the multiple account dont seem to know which user to login.
http://prntscr.com/ehttj4
Any help would be appretiated. I will post some code if needed.
SELECT SysID, emails, COUNT(emails)
FROM table_name
GROUP BY emails
HAVING COUNT(*) > 1
ORDER BY SysID DESC;
Use this query to get counts of duplicate email there in the table, in your SysID order.

Database design for group messaging

Just a question about what the best way is to send a message to a group (Like whatsapp group messaging).
I save the members per group into a separate database table and once a user sends a message to the group it retrieves the users from this table. I'm a bit stuck on part two:
As I see it there would be three methods.
Insert the message into a database for the entire group. Send the message back to the different users based on groups they are member of.
Insert the message in the database per user of the group. So loop the members and insert for every member. This has the advantage over the first method that downloaded, read and deleted status can be tracked.
Create a table for messages and one for the message-recipient link. But I'm not really sure how a would query in the most optimum way as to retrieve all the data (downloaded, read, etc) for a group of users. Is that possible in a single query?
Would anyone know what the best method would be? I can imagine that method 2 will fill up a database pretty fast, but method 1 doesn't have the ability of tracking status for a message.
At the moment I am using method 1, but I run into the problem that when a user deletes a message. It would still be returned on other devices because there would be no easy way of setting a certain deleted flag for group messages.
Does anyone know how apps like telegram, whatsapp and so on do this? And would method 2 give problems later on when millions of messages are sent?
Regards,
Ron
I would seperate it like this.
Messages
id, message, owner_id, deleted
Groups
id, name
Message_Group
message_id, group_id
User_Group
user_id, group_id
Then you can don't need to add a message to all users in that group. When you insert a new message you first inserts it into Messages, catch the id and inserts it into Message_Group with the message id and group id. To get the messages of a group just join Message_Group and Messages and you are pretty much done. Of course you also needs to join the user table to catch who wrote that messages.

display number of emails sent using php mail function

I am creating a real-estate website that displays properties. On each property listing I would like a button that says 'Express Interest', when clicked, this button should send an email to me containing the user details who expressed interest (the user will be logged in and so all their details will be taken from their account).
I have the email function working fine, HOWEVER, I also want to display the number of people who have 'Expressed Interest'. Is there a way to track how many times an email has been sent using this button and display "xx people have expressed interest in this property"
Store in the database before sending the mail and just before sending the mail include the number of people or the people who are interested, a simple query would suffice, or if you have a content management system you can include it there, in both cases you would need an intermediate table (connection between user id and real-estate id)
I would add another table to your database that stores a property_id and a user_id. If a user clicks expresses interest, you add another row to that table with the current property_id and the current user_id. To get the amount of links you simply count the rows
SELECT count(user_id) FROM interest_relation WHERE property_id = 'your property id'
This way you can also find out which user is interested in which property.

unique id in imap php

I am using imap_search to get a list of Emails FROM INBOX and SENT ITEMS.
I have a grid that showing the Mail headers first ,when user clicks on one row i want to show entire content,like body,attachment etc ..
to achieve this there should be a UNIQUE ID for each messages.
how can i get UNIQUE ID for messages , i know about the SE_UID option will return the UNIQUE ID'S but UNIQUE ID are only in one Mail box ,
I can see same ID'S on INBOX and SENT ITEMS.
Please help me.
Note : I am using Codeigniter with Peeker Library.
Thanks.
The completely unique ID is made up of the mailbox name (eg INBOX), the UIDVALIDITY returned when a mailbox is selected, and the message UID.
You need to select the relevant mailbox and provide the correct UID to fetch the right message. UIDs are not unique between different mailboxes.
You can use the UID of an email to do operations on any email in any mailbox such as INBOX and SENT.
You can find more information http://phpmaster.com/exploring-phps-imap-library-1/ in this article

User notification system via email

I would like to create a user notification system using emails.
In context it is for employees (user type 1) to log onto my system and enter their timesheet (hours they've worked). I would like a notification to be sent to their supervisors (also members of the 'users' table (user type 2) that they have entered the information and it is ready to be checked.
Currently I have a database with a table 'users' which stores information such as 'id' (PRIMARY) AUTO INCREMENTING, 'first_name', 'last_name', 'email', 'user_type', etc.
I know how to sent emails in PHP, and I have a fully functioning log in system however it is these notifications which I cannot get my head around with how to implement.
My current code is not Object-Oriented as it is beyond my skill level at this time.
I would like something simple, it doesn't have to be perfect. Any suggestions are greatly appreciated.
When an employee successfully enters a timesheet, can you not fetch the list of users and then send them all an e-mail? I'm guessing the timesheet is a web form, so just trigger some code when the form gets submitted.
For example:
<?php
foreach($supervisor_user as $su) {
mail(...);
}
?>
However, what might be nicer, is to periodically check (by using a cronjob) for new timesheets and then e-mail all the supervisors about any new time sheets.
I won't write the full PHP for this, it's more of a pseudocode outline:
<?php
$last_check = /* Fetch the time we last checked for timesheets */
$new_timesheets = /* Fetch all timesheets submitted after this time */
/* Construct the e-mail message */
$message = "Hello, here are the new timesheets. \n";
foreach($new_timesheets as $ts) {
$message .= "* $ts \n";
}
/* Send a message to all supervisors */
$supervisors = /* Fetch e-mails of all supervisors */
foreach($supervisors as $s) {
mail(...);
}
/* Finally, store the time we last checked for timesheets
so that we can use this on the next run. */
?>
You might then set that to run every day, meaning that supervisors would get a single summary e-mail every day, rather than being bombarded with an e-mail for every submitted timesheet.
to spacediver - what are some of those ready made systems that would be suitable in this instance? We have the same project but feel an off the shelf system might be better for us. Please explain!
Rick

Categories