83 Posts
modernmagic
8 years ago
18
Topic

Hello,

Is there a way to auto email the user that created the category if a new article is published to that category?

Regards,

Jeff

Get a VIP membership
8 years ago
4
Level 1

Hi Modernmagic,

Yes, you can used a code field (after save) to get the author of the category used by your current article then to get the email of this author then to send an email to this author email.

Thanks

83 Posts
modernmagic
8 years ago
3
Level 2

Hi Sébastien,

1) Are you referring to "Code Pack (5 plug-ins): Add Code After Store"  ?

2) Just to make sure, do I add this to the Article Form or the Category Form?

Thanks,

Jeff

8 years ago
2
Level 3

1) yes

2) your purpose is to add an action when a new article is submitted. So on article form.

Thanks.

83 Posts
modernmagic
8 years ago
1
Level 4

Hi Sébastien,

I have added the Code-After Store to the article form.  

Hopefully there is someone that could help me with the php code to send an email to the category creator.  I'm still trying to learn php....

Thanks,

Jeff

83 Posts
modernmagic
8 years ago
0
Level 5

Hello,

I have posted on joomla forum and joomla linkedin groups for help on "php code to send an email to the category creator", but no one has replied.  Do you have any advice and how I would learn how to write this php code?

Regards,

Jeff

8 years ago
2
Level 1

Hello modernmagic,
I'll try to help you with a simple example.


The purpose here is that after the submission of your form, you must request the database to get the email of the category author and then send the email, so:

  • get the category submited
  • get the author of the category
  • get the email of the author
  • send an email to this author email

For that we will use the plugin field Code "Afterstore".

In this kind of field, we can read the array "$fields" which is the array of all fields in your form.

Getting the category

$catid = $fields['name_of_category_field']->value;


Getting the category author

$cat_author = JCckDatabase::loadResult( 'SELECT created_user_id FROM #__categories WHERE id ='.$catid );

Getting his email

$author_email = JCckDatabase::loadResult( 'SELECT email FROM #__users WHERE id ='.$cat_author );

and finaly, send the email to the author

For that, you can use the Joomla! framework, and specifically the sendMail function:

	$app        =   JFactory::getApplication();
	$attach     =   array();
        $from       =   $app->getCfg( 'mailfrom' );
        $fromName   =   $app->getCfg( 'fromname' );
        $dest       =    array( $author_email );
        $subject    =   'The subject';   // you can get the subjetc from a field $fields['xxx']->value;
        $body       =   'The body';     // you can get the subjetc from a field $fields['xxx']->value;
	$bool       =    JFactory::getMailer()->sendMail( $from, $fromName, $dest, $subject, $body, true, NULL, NULL, $attach, NULL, NULL );


Regards.

Lionel

83 Posts
modernmagic
8 years ago
1
Level 2

Dear Lionel,

WOW, thanks!!

Here is what I put in the AfterStore of the article form and wanted to confirm that I understood.

<?php $catid = $fields['cat_title']->value; $cat_author = JCckDatabase::loadResult( 'SELECT created_user_id FROM #__categories WHERE id ='.$catid ); $author_email = JCckDatabase::loadResult( 'SELECT email FROM #__users WHERE id ='.$cat_author ); $app = JFactory::getApplication(); $attach = array(); $from = $app->getCfg( 'mailfrom' ); $fromName = $app->getCfg( 'fromname' ); $dest = array( $author_email ); $subject = $fields['cat_title']->value; // you can get the subject from a field $fields['xxx']->value; $body = $fields['art_introtext']->value; // you can get the subject from a field $fields['xxx']->value; $bool = JFactory::getMailer()->sendMail( $from, $fromName, $dest, $subject, $body, true, NULL, NULL, $attach, NULL, NULL ); ?>


JeffRegards,

83 Posts
modernmagic
8 years ago
0
Level 3

Sorry I didn't see aneasy way to past in code into this forum.  

<?php
$catid = $fields['cat_title']->value;
$cat_author = JCckDatabase::loadResult( 'SELECT created_user_id FROM #__categories WHERE id ='.$catid );
$author_email = JCckDatabase::loadResult( 'SELECT email FROM #__users WHERE id ='.$cat_author );
$app        =   JFactory::getApplication();
	$attach     =   array();
        $from       =   $app->getCfg( 'mailfrom' );
        $fromName   =   $app->getCfg( 'fromname' );
        $dest       =    array( $author_email );
        $subject    =   $fields['cat_title']->value;   // you can get the subject from a field $fields['xxx']->value;
        $body       =   $fields['art_introtext']->value;    // you can get the subject from a field $fields['xxx']->value;
	$bool       =    JFactory::getMailer()->sendMail( $from, $fromName, $dest, $subject, $body, true, NULL, NULL, $attach, NULL, NULL );
?>
8 years ago
0
Level 1

Hello Jeff,

I see 1 things:

you wrote :

$catid = $fields['cat_title']->value;
...
$subject    =   $fields['cat_title']->value;

So I think you want to get the title of the category in the subject.

For that, you must use the Text property:

$subject    =   $fields['cat_title']->text;<br>


Regards.

Lionel

83 Posts
modernmagic
8 years ago
0
Level 1

Dear Lionel,

After some tests, no emails were received.

Please review the screenshot:  https://www.dropbox.com/s/iyl5ivlzlghihjl/Screenshot%202015-08-06%2009.05.37.png?dl=0

Many thanks for all your help,

Jeff

8 years ago
6
Level 1

Jeff,

don't put the PHP tag <?php and ?>.

Only the code.

Regards.

Lionel

83 Posts
modernmagic
8 years ago
0
Level 2

Dear Lionel,

When I did that I get this error: 

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 SQL=SELECT created_user_id FROM le9n4_categories WHERE id =

https://www.dropbox.com/s/owabx7hhnyxd0df/Screenshot%202015-08-06%2009.16.57.png?dl=0

https://www.dropbox.com/s/wkk4h9zvh1ocnmu/Screenshot%202015-08-06%2009.17.23.png?dl=0

Regards,

Jeff

83 Posts
modernmagic
8 years ago
4
Level 2

Dear Lionel,

I'm still trying to figure out the issue, but I am still getting a 1064 error.

Please help,

Jeff

8 years ago
3
Level 3

Hello Jeff,

sorry I was in vacation.

What you can see is that the id in the request is empty.

you must get the ID of the category, so your code is probably not good:

$catid = $fields['cat_title']->value;

and must be more:

$catid = $fields['cat_id']->value;

but be sure to have the field Category ID in your form.


Best regards.

Lionel

83 Posts
modernmagic
8 years ago
0
Level 4

Dear Lionel,

Glad you had a vacation.  I sure need one....

*

I do have a hidden art_catid field in this same form.  https://www.dropbox.com/s/0u8dwdbz5ncdisa/Screenshot%202015-08-19%2010.05.53.png?dl=0


Is that what you mean?


Regards,

Jeff

83 Posts
modernmagic
8 years ago
1
Level 4

Dear Lionel,

I have added a hidden cat_id field to this article form.

https://www.dropbox.com/s/gdt5o035ormtgyf/Screenshot%202015-08-20%2000.31.50.png?dl=0

I am still getting the error:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 SQL=SELECT created_user_id FROM le9n4_categories WHERE id =

Thank you for your help,

Jeff

8 years ago
0
Level 5

Hi Jeff,

it is difficult to debug PHP applications remotely.

At this stage, it is necessary to dump on different variables, to be sure of the storage of each field,....

So what I advise if you don't find why your cat_id is empty and to not loose too many time, it's to have a look on our support package.


Be free to contact Octopoos


Regards.

Lionel

83 Posts
modernmagic
8 years ago
0
Level 1

Dear Lionel, 

Just checking it to see if you have any time to review my reply.

Thank you,

Jeff

Get a VIP membership