Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:32:07+00:00 2026-06-02T06:32:07+00:00

My website sends an email out to my user’s whenever someone PM’s (Private Message)

  • 0

My website sends an email out to my user’s whenever someone PM’s (Private Message) them. It says something along the lines of “Someone just sent you a message on SITE NAME, login to view it”.

At the moment my site isn’t very popular so my current way of doing this hasn’t been a problem performance wise:

    // code that sends pm here...

    // code that stores pm in messages table here...

    // notify user by emailing them right away
    mail($user_email, $subject, $message);

Basically every time a PM is sent, the mail function is executed and the message is sent right there on the spot. So for every 100 messages sent the mail() gets called 100 times.

I am anticipating my site getting more popular and with more users come more PM’s so I think my current way of doing it would become a performance nightmare. So I was thinking of doing it this way instead :

// get the last message that was accounted for by the last cron
$query = mysql_query(" SELECT `last_checked_id` FROM `settings` ");
$last_checked_id = mysql_fetch_array($query);


$user_emails = array();

// get all messages that were added to this table since the last time this cron ran
$query = mysql_query(" SELECT `recipient_id`, `message_id` FROM `messages` WHERE `message_id` > '$last_checked_id' ");

$i = 0;
while($row = mysql_fetch_array($query)) {

    if($i == 0) {
        // set this as the last message processed
        mysql_query(" UPDATE `settings` WHERE `last_checked_id` = '". $row['message_id'] ."' ");
    }

    // only put this user in the to email list if he hasn't been added already (since there can be multiple messages for the same user but we need to notify him only once) 
    if(!in_array($row['recipient_id'], $user_emails)) {
        array_push($user_emails, $row['recipient_id']);     
    }
}
$i++;

// send email to all users at one
mail(implode(',', $user_emails), $subject, $message);

I can set this script up as a cron and have it run every hour. All the emails are sent in one go with the mail function being called only once. The only drawback is user’s aren’t notified right away when they recieve a PM, but once within the hour.
But that’s not a big deal and something I can live with.

My questions are:

  • is the cron method significantly better performance wise or is the increase negligable
  • is this the way most big sites do it? or is there a better way, some established library maybye?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:32:09+00:00Added an answer on June 2, 2026 at 6:32 am

    You can insert mails a mysql table (emailoutgoing)

    And with a cron job running every “3” minutes, you can get unsent (100?) emails from table, send and mark row as sent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My website sends out html emails but most of my recipients are receiving them
After user registration, website send activation code to email. something like that. www.domain.com/?activate=<code> I'm
I send out a newsletter email containing URLs to a https website that then
Our real estate website sends email notifications when properties come on the market matching
My website example1.com is hosted on host.example2.com and when I send an email message
I have a website hosted on a Linux server. If someone sends me an
I have a website where a user can send out emails from. I have
Our website sends e-mails to the e-mail address which a user enters on our
Suppose you want a box on your website that says Give us your email
i have downloaded Swift Mailer from their website and try to send simple email

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.