Default PHP mail in Moodle

Default PHP mail in Moodle

by Siumin Ang -
Number of replies: 12

Hi, I have already set up a working SMTP server and tested it. All of my moodle mails including forum notification emails and message emails does send out. 

However, I have developed a php page of my own as a help desk.(please see attached) When the user clicks confirm, the (attached) php page will be executed, capturing the details and sending an email to a designated admin. 

However, i am unable to send emails via the Default PHP mail function. Is there any way to keep the SMTP server AS WELL AS set up settings for the default PHP mail function?? I have tried to no avail to send emails via moodle's 'email_to_user()' function.

Help is greatly appreciated!!

Average of ratings: -
In reply to Siumin Ang

Re: Default PHP mail in Moodle

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Siumin,

Here is some information that should help you get those email's working.

(from http://articlebin.michaelmilette.com/sending-custom-emails-in-moodle-using-the-email_to_user-function/)

You can send custom emails from within Moodle using PHP using your own forms using the email_to_user() function. By the way, did you know that Moodle actually uses the open source PHPMailer behind the scenes? This could be useful if for any reason you want to bypass Moodle's email function completely.

A typical call to Moodle's email_to_user() function would look like:

email_to_user($toUser, $fromUser, $subject, $messageText, $messageHtml, ", ", true);

This is a very simple version of a very powerful function. For more information about the email_to_user() function, take a look at the source code for function email_to_user in Moodle's /lib/moodlelib.php.

Tips for Using email_to_user()

  1. $toUser and $fromUser must be Moodle user objects, not email addresses. The stdClass object must contain a minimum of the following:
    •  $emailuser->email: Email address
    • $emailuser->firstname: You can put both first and last name in this field.
    • $emailuser->lastname
    • $emailuser->maildisplay = true;
    • $emailuser->mailformat = 1; // 0 (zero) text-only emails, 1 (one) for HTML/Text emails.
    • $emailuser->id: Moodle User ID. If it is for someone who is not a Moodle user, use an invalid ID like -99.
    • $emailuser->firstnamephonetic
    • $emailuser->lastnamephonetic
    • $emailuser->middlename
    • $emailuser->alternatename
    • Unless specified, the above fields can be set to blank if you don't have the information available.
  2. You can convert your HTML message into plain text using:
    $messagetext = html_to_text($messagehtml);
  3. You can get the configured support user information using the following two variables:
    • $CFG->supportemail
    • $CFG->supportname
  4. If you want the email to come from noreply@yourwebsite.com, set the true parameter to  false .

Be sure to always use Moodle filters to clean any user entered information before submitting it in an email including email addresses, subject, message or you will be just asking for trouble.

Enabling Email Debugging in Moodle

When developing email support in your Moodle plugin, you can make your life a whole lot easier by enabling email debugging however this should only be done in a development environment. The settings are all in the same place you would normally go to put Moodle debugging in DEVELOPER mode. Simply complete the following steps:

  1. Click Site Administration > Advanced Features > Development > Debugging
  2. Set Debug messages to DEVELOPER: extra Moodle debug messages for developers
  3. Check the Display debug messages box.
  4. Check the Debug email sending box.
  5. Click Save changes at the bottom of the page.

Don't forget to go back and disable these settings when you are done debugging.

Best regards,

Michael Milette

Average of ratings: Useful (2)
In reply to Michael Milette

Re: Default PHP mail in Moodle

by Siumin Ang -

Thanks very much Michael. smile

I got it to work successfully. But do you have any idea of how to send an attachment from a Php based form?

Can i send a file from 'Input Type=file' to Moodle's email_to_user?


So far, i've only seen attachments that are stored on the server can be sent through the email_to_user function.


In reply to Siumin Ang

Re: Default PHP mail in Moodle

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

I haven't tried to do attachments using the email_to_user function yet.

Here are a couple of examples on how you could do it:

I haven't really looked into these but they might provide you with some additional insight on how to do it within Moodle:
From what I can see, it looks like you typically need to upload the file to Moodle first and then send it as an attachment. If you want to do it directly, PHP Mail() or the PHPMailer class may be a better option for you, especially if these are people who are not Moodle users.

Hope something in all this helps.

Best regards,

Michael

In reply to Michael Milette

Re: Default PHP mail in Moodle

by Ramit Kumar -

How about if we want to add an attachment with the email and send?? 

i have been doing it and the emails deliver successfully, but i does not deliver the attachments with the email.  below are my codes! 


require_once('../config.php');

Global $USER,$DB,$COURSE;

$userid= $USER->id; // retrieves user id from login

$fName = $USER ->firstname;

$LName = $USER->lastname;


$UName = $fName.$LName ;


$name = $UName; // get from DB

$email_subject = optional_param('subject','',PARAM_RAW); 

$email_form = optional_param('Emailfrom', '', PARAM_RAW);

$message = optional_param('body', '', PARAM_RAW);


$emailTo = array();

    foreach ($_POST['emailto'] as $val)

    {

       $emailTo[] = $val;

    }

    $toManagaers = implode(';', $emailTo);


$to = new stdClass();

$to->firstname = '';

$to->lastname  = '';

$to->email     = $toManagaers;



$from = new stdClass();

$from->firstname = $name;

$from->lastname  = '';

$from->email     = $email_form;

$from->maildisplay = 1;


$emailsubject = $email_subject;

$emailmessage = $message;


$attachment->AddAttachment($CFG->wwwroot.'/tcpdf/examples/folder/BHIForm.pdf'); // attachments here

email_to_user($to, $from, $emailsubject, $emailmessage,$attachment);

In reply to Ramit Kumar

Re: Default PHP mail in Moodle

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

Please see the inline docs for the email_to_user() function. The $attachment argument is supposed to be a string (no idea what your AddAttachment() method is supposed to be)

@param string $attachment a file on the filesystem, either relative to $CFG->dataroot or a full path to a file in $CFG->tempdir

There are other issues with you code example though. E.g. accessing superglobals like $_POST is strongly discouraged. It might help to grep the Moodle code a bit to see how the function is typically used.

In reply to David Mudrák

Re: Default PHP mail in Moodle

by Ramit Kumar -

hi David,

i have tried to go through inline Docs for the email_to_user() function. well m not a big PHP geek. at the moment m learning and doing it same time.


i have tried various ways to attach a file with the email, but the attachment does not reach my targeted email address. could you guide me with attachment (sample code maybe!).


thanks

Ramit

In reply to Ramit Kumar

Re: Default PHP mail in Moodle

by Jaume Mila -

Please Ramit fix your code or remove it before anyone could use it in a production environment.

Think about what happens if a spammer submits a request formed by hundred 'emailto[]' fields to your code.

In reply to Jaume Mila

Re: Default PHP mail in Moodle

by Jaume Mila -

Well... after check the email_to_user function I could think that your code is not so usable as spammer engine, but continues being a DDOS vulnerability. Imagine that the evil user sends to your script a request with a very big list of 'emailto[]' emails on query, thousands, million,.... You could say that only Administrators have access to, ok, you are almost safe, but the code is not able to publish.

Please, at least, pay attention to http://php.net/manual/en/ref.filter.php or better if you could use the required_param/optional_param to sanitize the 'emailto' field. Limit the number of 'emailto' too.

In reply to Jaume Mila

Re: Default PHP mail in Moodle

by Ramit Kumar -

hi Jaume,

$emailTo = array();

    foreach ($_POST['emailto'] as $val)

    {

       $emailTo[] = $val;

    }

    $toManagaers = implode(';', $emailTo);


above comes from the checkbox, so its not a big deal as it has only 3 options to Check.(a bulk users wont get in). the concern was to send a email with the built-in Moodle eamil_to_user function, which apparently does not work in my case.

appreciate if you have something in mind to share about it.


thanks

In reply to Michael Milette

Re: Default PHP mail in Moodle

by Jona Turner -

Hi all, 

Is there a way of sending an email from a user that's not logged into moodle? From a contact form in a newly created php file for example?

I have attempted to use the email_to_user function but failed, mainly I believe because I'm trying to do this without being a user.

Any help would be much appreciated.

In reply to Jona Turner

Re: Default PHP mail in Moodle

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

You can fake a "from" user to use in the email_to_user() function. Have a look at /admin/process_email.php.