John Davidson

php - October CMS | Using a button to send email retrieved from a field

0 comments
Message:


I'm trying to dynamically find email address (from what has been inputted in a field), then sending out email to that address.


So far, I have been able to get the button to send emails but unable to retrieve the address from model.


Examples:


Controller


public function onSend()

{
// Retrieve email address from Machines model in email field
$this->vars['email'] = Machines::get('email');

// Send to email address using partial button
Mail::send('martin.maintenance::mail.maintenancereminder', [], function($message) {
$message->to($this->vars['email']);
});
}

Field (in Machines model)


email:
label: 'Email Address'
span: auto
type: text
tab: Details

Partial (button)


<button
type="submit"
data-request="onSend"
class="btn btn-primary">
Reminder
</button>

Error


Happy to provide any additional info. Thanks in advance!



My Answer:
It looks like you are trying to retrieve the email address from the Machines model using the `get` method, but the `get` method is used to retrieve a single record by its primary key. In your case, you need to retrieve the email address from the specific record that is being edited or viewed.

You can achieve this by passing the record ID to the `onSend` method and then retrieving the email address from the record using that ID. Here's an updated version of your code:

Controller
php
public function onSend($recordId)
{
// Retrieve email address from Machines model using the record ID
$machine = Machines::find($recordId);
$email = $machine->email;

// Send to email address using partial button
Mail::send('martin.maintenance::mail.maintenancereminder', [], function($message) use ($email) {
$message->to($email);
});
}


Partial (button)
html
type="submit"
data-request="onSend"
data-request-data="recordId: {{ record.id }}"
class="btn btn-primary">
Reminder



In the partial button, we are passing the `record.id` to the `onSend` method as `recordId`. This ID will be used to retrieve the specific record from the Machines model and get the email address from it.

Make sure to adjust the code according to your specific requirements and the structure of your Models. Let me know if you need further assistance!

Rate this post

4 of 5 based on 6485 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us