MODX3 Processor Problem with modPHPMailer

Hello,

I try to send email within a function in Processor.
Tried to use adapted code from Bob


	private function mailnow(){

    	$email_content  = $this->getProperty('emlContent');
        $email_to       = $this->getProperty('email');


        if (!$this->modx->services->has('mail')) {
            $this->modx->services->add('mail', new modPHPMailer($this->modx));
        }
        $mail = $this->modx->services->get('mail');

		$mail->set(modMail::MAIL_BODY, $email_content);
		$mail->set(modMail::MAIL_FROM, $this->modx->getOption('pp_emailFromService'));
		$mail->set(modMail::MAIL_FROM_NAME, $this->modx->getOption('pp_emailFromServiceName'));
		$mail->set(modMail::MAIL_SUBJECT, $this->modx->lexicon('eml_subject'));
		$mail->address('to', $email_to);
		$mail->setHTML(true);

		if (!$mail->send()) {
			$this->modx->log(1,'An error occurred while trying to send the email: ' . $mail->mailer->ErrorInfo);
			return $this->modx->error->failure($this->modx->lexicon('eml_error'));
		}else{	
			$mail->reset();
			return true;
		}		

	}

But gets error
Fatal error: Uncaught Error: Class “customer\processors\customers\modMail” not found in

I used class via

use MODX\Revolution\Mail\modPHPMailer;

What is wrong?

Thank you in advance.

bye
Chris

Also add modMail with a use block:

use MODX\Revolution\Mail\modPHPMailer;
use MODX\Revolution\Mail\modMail;

Whenever you’re accessing a class that’s outside your own namespace, you need to either add a use statement for it or use it’s fully qualified name.

1 Like

thank you Mark.

omg, so easy…

:pray:

Is there any way to add these statements globally so that we don’t have to add them to every single snippet? I tried doing that with a plugin but couldn’t get it to work. I use a LOT of snippets in my sites, so it would be great to be able to install the various USE statements once instead of everywhere.

That would defeat the purpose of using namespaces. It’s standard in PHP development that you’ll get used to after working with it some more. :wink:

Proper IDEs like PhpStorm will also automatically add them for you when auto completing classnames.

+1 for Mark’s PhpStorm suggestion.

It not only autocompletes namespaces, it highlights invalid ones in a classname, a use statement, or a namespace line. This feature alone has saved me many hours.

It will also flag just about any PHP error you could make, so it’s even more valuable for beginners.

Is it support Visual Studio Code

I’m a PHPStorm user, so I haven’t tried, but it looks like this VSCode plugin should do it: