Matt P
Matt P

Reputation: 593

How do I configure Mailgun for Moodle?

What is the proper configuration for Mailgun to be used with Moodle? I am using the Bitnami Moodle image on Google Cloud.

I receive this error message.

Error sending password change confirmation email

More information about this error

Debug info: Error code: cannotmailconfirm Stack trace: line 495 of /lib/setuplib.php: moodle_exception thrown line 110 of /login/lib.php: call to print_error() line 81 of /login/forgot_password.php: call to core_login_process_password_reset_request() Output buffer:

 2016-11-18 14:21:25    Connection: opening to smtp.mailgun.org:25, timeout=300, options=array ( ) 2016-11-18 14:23:32  SMTP ERROR: Failed to connect to server: Connection timed out (110) 2016-11-18 14:23:32 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 

Upvotes: 1

Views: 1751

Answers (3)

Flavio Barros
Flavio Barros

Reputation: 1046

I just did that in a server on digital ocean. So here the solution:

  • Configure your DNS and check it on mailgun app;
  • In the credentials get your smtp password;
  • SMTP host: smtp.mailgun.org:587
  • SMTP security: tls
  • SMTP Auth Type: Login
  • SMTP Username: poastmaster@
  • SMTP Password:

The trick here is port 587. I tried everything but just this worked. As a bonus, if you have ssh access to your server you can send emails form terminal with:

./swaks --auth \
    --server smtp.mailgun.org:587 \
    --au postmaster@<yourdomain> \
    --ap <smtpcredential> \
    --to [email protected] \
    --h-Subject: "Teste Moodle" \
    --body 'Testing some Mailgun awesomness!'

And, as you are running moodle, if you want to do the same with php:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mailgun.org:587';                 // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';     // SMTP username
$mail->Password = '<your credetials>';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, only 'tls' is accepted

$mail->From = '[email protected]';
$mail->FromName = 'You';
$mail->addAddress('[email protected]');                 // Add a recipient

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters

$mail->Subject = 'PHP subject';
$mail->Body    = 'Email from php!';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

Upvotes: 0

Tom Davies
Tom Davies

Reputation: 899

Google Compute Engine does not allow outbound connections on ports 25, 465, and 587. By default, these outbound SMTP ports are blocked because of the large amount of abuse these ports are susceptible to.

Port 25 outbound is blocked on Google Cloud. Mailgun mirrors 587, on port 2525 - so try using port 2525.

As a quick test you could also try telnetting from the command line to check connectivity, like this:

> telnet smtp.mailgun.org 2525

Upvotes: 1

HelloSpeakman
HelloSpeakman

Reputation: 830

You should attempt to use port 465 instead of port 25 as well as setting encryption to SSL.

I haven't used moodle personally but I believe you can do it like this: UPDATE mdl_config SET value='ssl://smtp.mailgun.org:465' WHERE name='smtphosts';

Upvotes: 0

Related Questions