General developer forum

Cannot send email via moodle_phpmailer()

 
Picture of Hittesh Ahuja
Cannot send email via moodle_phpmailer()
Plugin developersTesters

Hi ,

Just so I'm clear other email functionalities on the site works as expected. I am writing a Moodle plugin that would generate a csv, zip it and email it to the user.

here is the code snippet I am working with. I can confirm that we do not use SMTP credentials on any of our servers. 


global $CFG;
        require_once($CFG->libdir . '/phpmailer/moodle_phpmailer.php');
        $mail = new moodle_phpmailer();
        $mail->isMail();
        $mail->Subject = $subject;
        $mail->isHTML(true);
        $mail->Body = $body;
        $mail->From = $from->email;
        $mail->FromName = $from->firstname . ' ' . $from->lastname;
        $mail->addAddress($to);
        if ($bccself) {
            $mail->addBCC($from->email);
        }
        if ($attachment) {
            $mail->addAttachment($attachment);
        }

        if (!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            exit;
        }
        return true;

And this is the error I am getting : 

Mailer Error: SMTP Error: The following recipients failed: <email>: : Relay access denied@bath.ac.uk>

Thanks

Hittesh

 
Average of ratings: -
Picture of Matteo Scaramuccia
Re: Cannot send email via moodle_phpmailer()
Core developersParticularly helpful MoodlersPlugin developers

Hi Hittesh,
you should ask to the SMTP server administrator(s) what are the constraints about the SMTP relaying granted to the host you've configured in your Moodle instance, since it looks like that relay is granted on the domain of the recipients.

Probably, getting SMTP credentials will give you more grants including the possibility to relay to any recipient.

HTH,
Matteo

 
Average of ratings: -
Picture of Sam Chaffee
Re: Cannot send email via moodle_phpmailer()
Core developers

Hi Hittesh,

Is there a reason you need to use moodle_phpmailer directly? If not and if, as you mention, mail seems to working elsewhere within in Moodle then possibly using the email_to_user function would work around whatever issue you are running into.

Cheers,

Sam

 
Average of ratings: -
Picture of Hittesh Ahuja
Re: Cannot send email via moodle_phpmailer()
Plugin developersTesters

Apparently using isMail() seems to work now. I agree about the SMTP credentials and particularly wanted to avoid that. The reason I am using the library as I want to send email to a arbitary email address , not a moodle user smile

Thanks guys .


Hittesh

 
Average of ratings: -