Attachment not working in email_to_user()

Attachment not working in email_to_user()

by Amir Mustafa -
Number of replies: 7

Hello Friends,

I have managed to successfully send email to user. Now the issue is I have tried various ways to send email with attachment. 5th and 6th parameter sends the email attachment. But it is not working:


Code:

$trimval   = trim($val);

$sql       = "SELECT id FROM {user} WHERE email = '$trimval'"; 

$useridSql = $DB->get_record_sql($sql);

// $userid    = $useridSql->id;

$userid    = 15;


if($userid == '') { continue; }


/*$emailsubjectcancel = "Flight Plan";

$emailmessagecancel = "Hello, <br><br>";

$emailmessagecancel.= $flight_plan_message . PHP_EOL;

$emailmessagecancel.= "This flight plan was sent by <b> $generatedFor </b>.<br><br>";

$emailmessagecancel.= "<br><br><b>Get your Flight Plan <a href = '$attachment'>here</a></b>.";*/

$userobj            = get_complete_user_data('id',$userid);

// $targetlink         = "$CFG->wwwroot/user/profile.php?id=$userid"; // user profile in link

$targetlink         = "mailto:$userobj->email"; // mailto in link

// $targetlink         = "mailto:$userobj->email?subject=$emailsubjectcancel&body=$emailmessagecancel"; // mailto in link

$generatedFor       = '<a href = "'.$targetlink.'">' . $userobj->firstname . ' ' . $userobj->lastname . ' (' .$userobj->email . ')</a>';

$emailsubjectcancel = "Flight Plan";

$emailmessagecancel = "Hello, <br><br>";

$emailmessagecancel.= $flight_plan_message . PHP_EOL;

$emailmessagecancel.= "This flight plan was sent by <b> $generatedFor </b>.<br><br>";

$emailmessagecancel.= "<br><br><b>Get your Flight Plan <a href = '$attachment'>here</a></b>.";

// echo $emailmessagecancel;


// Trigger moodle email 

// email_to_user($userobj,  'bartilona.air@gmail.com', $emailsubjectcancel, $emailmessagecancel, $emailmessagecancel);

email_to_user($userobj,  'bartilona.air@gmail.com', $emailsubjectcancel, $emailmessagecancel, $emailmessagecancel, 'path1/path2', 'test.php');


Please guide me how to proceed. I am stuck in this attachments process. Please guide me I am stuck in this step.

Average of ratings: -
In reply to Amir Mustafa

Re: Attachment not working in email_to_user()

by Amir Mustafa -

In my case some extra /data path is coming. Don't know where to find this path.

path1 and path2  I have given in the moodle mail function:

email_to_user($userobj,  'bartilona.air@gmail.com', $emailsubjectcancel, $emailmessagecancel, $emailmessagecancel, 'path1/path2', 'test.php');

Please guide me how to fix this. sad sad

Attachment Screenshot_1.png
In reply to Amir Mustafa

Re: Attachment not working in email_to_user()

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Amir,

they've a discussions about it here :

https://moodle.org/mod/forum/discuss.php?d=260080

https://moodle.org/mod/forum/discuss.php?d=198409#p866272

https://articlebin.michaelmilette.com/sending-custom-emails-in-moodle-using-the-email_to_user-function/


if you scroll down or search on the page about attachment you'll find code link.

but in fact, it's not that obvious.


Your mistake it's not to pass the right path and filename.

Moodle paths are usually in the $CFG variable. Dump it, it will help you.


Dominique

Average of ratings: Useful (1)
In reply to Dominique Palumbo

Re: Attachment not working in email_to_user()

by Amir Mustafa -

Thank you Dominique smile smile

It was the great help.

In reply to Amir Mustafa

Re: Attachment not working in email_to_user()

by Amir Mustafa -

Some conclusions from my end how to send mail with and without attachments:


Way to send Email in Moodle:

// PREPARING THE DATA TO SEND IN MAIL

$senderid = $USER->id;

$senderobj= get_complete_user_data('id',$senderid); // This data is for the person whom we want to send the mail


$Subject = "Flight Plan";


$Message = "Hello, <br><br>";

$Message.= $flight_plan_message . PHP_EOL;

$Message.= "This flight plan was sent by <b> $generatedFor </b>.<br><br>";

$Message.= "<br><br><b>Get your Flight Plan <a href = '$attachment'>here</a></b>.";


// File to be sent as an attachment - if we hit this url in browser we get the attachment

$attachment = $CFG->wwwroot.'/local/flight_preparation/PDF/'.$file_name;


(A) Without Attachment:

// echo $emailmessagecancel;

1st para = to, 2nd Para = from, 3rd Para = Subject, 4th Para = Message(Plain text), 5th Para = Message(HTML format), 

email_to_user($senderobj,  'bartilona.air@gmail.com', $emailsubjectcancel, $emailmessagecancel, $emailmessagecancel);


(B) With Attachment:

1st para = to, 2nd Para = from, 3rd Para = Subject, 4th Para = Message(Plain text), 5th Para = Message(HTML format),6th Para = File path including filename, 7th Para = only file name 

email_to_user($userobj,  'bartilona.air@gmail.com', $emailsubjectcancel, $emailmessagecancel, $emailmessagecancel, 'temp/typo3temp/cs/csascii_utf-8.tbl', 'csascii_utf-8.tbl');


Note the attached path should be inside moodledata (i.e. your folder should be inside moodledata directory)


Hope this would be a help.

Thank You


Average of ratings: Useful (1)
In reply to Amir Mustafa

Re: Re: Attachment not working in email_to_user()

by Sandipa Mukherjee -
Picture of Particularly helpful Moodlers
I have a local plugin and some pdf file generated in a folder of the local plugin. I have sent the path and the file name to email_to_user function but it does not attach the attachment into the email. Any thoughts?
In reply to Sandipa Mukherjee

Re: Re: Re: Attachment not working in email_to_user()

by Sandipa Mukherjee -
Picture of Particularly helpful Moodlers

$touser = $DB->get_record('user', array('id' => 9));

$fromuser = $DB->get_record('user', array('id' => 2));

$subject = get_string('eamil_subject', 'local_myplugin');

$messagetext = $messagehtml = get_string('eamil_body', 'local_myplugin');

$loc = 'myfile.pdf'; // location should be moodle data folder

$fname = 'myfile.pdf';

 if(email_to_user($touser, $fromuser, $subject, $messagetext, $messagehtml, $loc, $fname, true)) {
 echo 'sent';
} else { echo 'no'; }


 hope this helps!
In reply to Sandipa Mukherjee

Re: Re: Re: Re: Attachment not working in email_to_user()

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
hi, you've to include the full path of your generated pdf. I don't think the PDF is generated in the root folder of the module. (except if you've changed the right after installation...) if it's the case you can use something like $loc = $CFG->dirroot . '/mod/mymoddirectorythatgeneratepdf/myfile.pdf'; But to use the moodle data folder use this variable $CFG->dataroot It's better to create a moodle data subfolder with the name of your module directory to save your pdf and give the rights. Hope it's helped. Dominique.