send_to_email() for CC?

send_to_email() for CC?

از Mark B در
Number of replies: 7

Hi there,

I have recently been using the send_to_email command and have used this to good effect in order to send students an e-mail through php code.  My questions is, if i want to CC the e-mail to a member of staffs e-mail how do i go about doing this instead of having to fire off a second e-mail to them seperately?


The code i am currently using is as follows:

require_once("lib/moodlelib.php");

$from = $DB->get_record('user', array('username'=>"username_goes_here"));
#$from = $return->email;

$user = new object();
$user->firstname = "First name";
$user->lastname = "Last name";
$user->email = "*****@****.com";

$subject = "dummy subject";
$message = "dummy message goes here";
$messagehtml = "No";

email_to_user($user, $from, $subject, $message, $messagehtml);

 

Ok so when i run that with the proper values submitted it works a charm and the e-mail gets sent, how do i make it send to more than one user in the same e-mail or will i need to write out two email_to_user functions with the second one going to the person who is copied in?


Many thanks,

Mark

میانگین امتیازات: -
In reply to Mark B

Re: send_to_email() for CC?

از Matteo Scaramuccia در
تصویر Core developers تصویر Peer reviewers تصویر Plugin developers

Hi Mark,
email_to_user() sends a mail to a single user. AFAIK you need to use low level methods, accessing directly to the mailer class in Moodle:

$mail = get_mailer();

and (part of) the low level methods are:

  • AddAddress
  • AddCC
  • AddBCC
  • Subject
  • Send
  • ...

FYI: MDL-21835, in the next days I'll look at possibile issues in 2.x.

HTH,
Matteo

In reply to Matteo Scaramuccia

Re: send_to_email() for CC?

از ramesh prajapati در

i have to send htmlbody using email_to_user.

How i can send the html message using these method

 

In reply to Mark B

Re: send_to_email() for CC?

از Siumin Ang در

Hi,

Would it be possible for you to share more examples?

I followed your codes exactly but unfortunately was unable to get email_to_user() to work. ناراحت


I need the function for a customized script. 


Many thanks!!

In reply to Siumin Ang

Re: send_to_email() for CC?

از Davo Smith در
تصویر Core developers تصویر Particularly helpful Moodlers تصویر Peer reviewers تصویر Plugin developers

If you're using Linux, or similar, then this command will get you a good list of examples of using email_to_user (if run in the code directory for your Moodle install):

grep -R "email_to_user"

I'm sure there are similar search commands in other OS.

You could also try explaining what didn't work. Is debugging on? Was there an error message? Have you set up a debugger and stepped through the code line by line? Have you tried adding echo statements to the email_to_user function to figure out where it is going wrong?

In reply to Mark B

Re: send_to_email() for CC?

از Suraj Kumar در

Dear Mark,

I faced the same  issue. I was not able to send mails to parent in cc along with site_alert_email(in my case - this is the mail that was receiving mail of certain type in cc).

What I was tried first, I set customheaders for $from i,e. $form->customheaders and set cc header  here.

$form->customheaders = 'Cc: xyz@abcd.com';

And called email_to_user in the same way.

email_to_user($user, $from, $subject, $message, $messagehtml); Sending cc through Cc header was not working well.

I realized that the mails-ids passed through Cc headers were not coming in the mailer's object all_recipients array but I passed those email-ids through addCC() and addBCC() methods of mailer, it worked & also those mail ids were in the list of all_recipients lists.

What I changed then,

function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $attachment = '', $attachname = '',
                       $usetrueaddress = true, $replyto = '', $replytoname = '', $wordwrapwidth = 79, $cclist=array(), $bcclist=array()) {

added two additional parameters to email_to_user().

These array should be in format of

$cclist = array( array('email'=>'abcd@xyz.com', 'name'=>'Suraj Kumar'), .....);

'name' in the array might be empty or may be the name of associated user.

Added code, just above this line in the function emal_to_user

Line no. 5868 (moodle 2.7.7+)  


+++++++++++++++++++++++++++++++

//set cc and bcc if specified in the argument
   
    //add cc
    if(!empty($cclist) && is_array($cclist)){
        foreach($cclist as $cc){
            if(is_array($cc)){
                $email = isset($cc['email'])?$cc['email']:'';
                $name = isset($cc['name'])?$cc['name']:'';
                if(!empty($email)){
                    $mail->addCC($email, $name);
                }               
            }
        }       
    }
    //add bcc
    if(!empty($bcclist) && is_array($bcclist)){
        foreach($bcclist as $bcc){
            if(is_array($bcc)){
                $email = isset($bcc['email'])?$bcc['email']:'';
                $name = isset($bcc['name'])?$bcc['name']:'';
                if(!empty($email)){
                    $mail->addBCC($email, $name);
                }               
            }
        }       
    }
   
+++++++++++++++++++++++++++

Line no. 5868 if (!empty($from->customheaders)) {


It worked for me.