peppeocchi / php-cron-scheduler

PHP cron job scheduler
MIT License
811 stars 144 forks source link

Change subject per schedule #83

Closed TwinMist closed 5 years ago

TwinMist commented 5 years ago

Hi Is it possible to change the email subject per schedule?

thanks

peppeocchi commented 5 years ago

Hi @TwinMist yes it is possible, please have a look here, this is the example you're looking for

$scheduler = new Scheduler();

$scheduler->php('myscript.php')->configure([
    'email' => [
        'subject' => 'Visitors count',
    ]
]);

$scheduler->php('my_other_script.php')->configure([
    'email' => [
        'subject' => 'Page views count',
    ]
]);
TwinMist commented 5 years ago

hi Sorry can not get it to work, where iam in going wrong

// Create the Transport the call setUsername() and setPassword() $transport = (new Swift_SmtpTransport('xxxxxx', 587, 'TLS')) ->setUsername('xxxxx') ->setPassword("xxxxx") ->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false)));

$scheduler = new Scheduler([ 'email' => [

    'from' => 'noreply@notifications.xxxxxx.com',
    'body' => 'This is the email body',
    'transport' => $transport
]

]);

// ... configure the scheduled jobs (see below) ... $scheduler->php( '/scripts/test.php', // The script to execute '/usr/local/bin/php', // The PHP bin [ '--account' => '1' ], 'IdScheduler' ) ->everyMinute() ->output('/scripts/file.txt') ->email(['andy.xxxxxx@xxxxxx.com', 'subject' => 'Visitors count']);

just does not send the email

many thanks

peppeocchi commented 5 years ago

@TwinMist I think you missed my comment, the subject shouldn't go in the emails method, but to the configure method. So, your code should look like

$scheduler->php(
  '/scripts/test.php', // The script to execute
  '/usr/local/bin/php', // The PHP bin
  [
    '--account' => '1'
  ],
  'IdScheduler'
)
->configure([
    'email' => [
        'subject' => 'Visitors count',
    ]
])
->everyMinute()
->output('/scripts/file.txt')
->email(['andy.xxxxxx@xxxxxx.com']);

after this, if the emails doesn't get sent, you should debug your mail transport and make sure the configuration is correct

TwinMist commented 5 years ago

Hi so using your code, it looks like ->configure([ 'email' => [ 'subject' => 'Visitors count', ] ]) overwrites

$scheduler = new Scheduler([ 'email' => [ 'from' => 'noreply@notifications.xxxxx.com', 'body' => 'This is the email body', 'transport' => $transport ] ]);

was not until i added the from address it sent the email ->configure([ 'email' => [ 'subject' => 'Visitors count', 'from' => 'noreply@notifications.xxx.com', ] ])

where am i going wrong, many thanks for you help