0

I am trying to send email three days before the expired date, but I'm not sure how to?

Logic

  1. Retrieve all subscribers that are three days left to expire
  2. Send email to their users

Code

Table I need to check timestamps named subscribes.

$subscribes = Subscribe::all();

This table has a column named expires_at which I need to check this for finding 3 days left.

And my mailing

Mail::to($user->email)->send(new SubscribeExpire($user, $subscribe));

I'm confused with this Carbon calculation thing, anyone can help with that?

Update

based on answer below now I have this:

$subscribes = Subscribe::where('expires_at', Carbon::now()->subDays(3))->get();
        $user = [];
        $package = [];
        foreach($subscribes as $subscribe){
            $user = User::where('email', $subscribe->email);
            $package = Package::where('id', $subscribe->package_id);
        }

        Mail::to($user->email)->send(new SubscribeExpire($user, $package));

but when I run the command it get this error

ErrorException  : Trying to get property 'email' of non-object
8
  • $user = User::where('email', $subscribe->email) should be $user = User::where('email', $subscribe->email)->first(); Commented Dec 3, 2018 at 16:23
  • The same goes to Package. Commented Dec 3, 2018 at 16:23
  • then what if i have several subscribes for different users? it only gets first one right? Commented Dec 3, 2018 at 16:24
  • In such case use "->get()" instead of "->first()" and use foreach loop to iterate through these users and send an email to each of these users. Commented Dec 3, 2018 at 16:30
  • 1
    I just added it as an answer. It was just easier to put a code snippet there. Take a look if that will work for you. Commented Dec 3, 2018 at 16:37

2 Answers 2

1
  // here you get subscribes  
  // if you are going to send three days before the expiry date, this means we need to check if expires_at is in three days so probably need to add days. Or maybe even check if time left before expiration is more than three days and less than one day and run it once per day?
  $subscribes = Subscribe::whereDate('expires_at', Carbon::now()->addDays(3))->get();
    $user = [];
    $package = [];
    foreach($subscribes as $subscribe){
        // you probably have only one user with this email
        $user = User::where('email', $subscribe->email)->first();
        // you probably have one associated package
        $package = Package::where('id', $subscribe->package_id)->first();
    }
    // check if user and package are found
    if(is_object($user) && is_object($package)){
      Mail::to($user->email)->send(new SubscribeExpire($user, $package));
    }
Sign up to request clarification or add additional context in comments.

4 Comments

unfortunately it doesn't work, my best guess is it has problem finding the user because it always return []
I've updated the answer. My guess is that it is returning you your empty array you assigned to user. The issue might be in the first line.
life saver :D. Thank you so much it finally does send email
You're welcome! :)
1

Use subDays() method as shown below:

$subscribes = Subscribe::where('expires_at', Carbon::now()->subDays(3))->get();

8 Comments

Q1 do I need to loop this in order to get users for their emails? Q2 if i loop it the will it send email to all those users of just first one it finds? how should i define array of results in my Mail::...?
updated my question.
If expires_at is a timestamp, this won't work so well as you're looking at an exact comparison.
yes is timestamp, what yu suggest now?
@mafortis, you really shouldn't add additional questions once you have an answer. Read the "How to Ask" section under Help. Don't ask multiple questions in one, SO is a Q&A site, not a discussion forum where the answers can be broad and discussed.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.