Get link page for sending email confirm

Hi everyone!

Can you help us, we can’t get new link. Our task is to make.

  1. User creates a new record.
  2. To user will send a letter by mail with a link to confirmation. (We stopped in this part)
  3. User follows the link, and confirms by pressing button Yes or No.

How can we do this by either?

Hello!

Take a look at this guide about sending email after entity creation: 3.9.2.4. Email Sending Guide.
For sending emails you can try to use EmailService. For instance in StandardEditor:

@Inject
protected EmailService emailService;
@Inject
private GlobalConfig globalConfig;

private boolean justCreated;

@Subscribe
public void onInitEntity(InitEntityEvent<ApproveRequest> event) {
    justCreated = true;
}

@Subscribe(target = Target.DATA_CONTEXT)
public void onPostCommit(DataContext.PostCommitEvent event) {
    if (justCreated) {
        sendByEmail();
    }
}

private void sendByEmail() {
    ApproveRequest approveRequest = getEditedEntity();
    EmailInfo emailInfo = EmailInfoBuilder.create()
            .setAddresses("test@my_domain.com")
            .setCaption(approveRequest.getName())
            .setFrom(null)
            .setBody("Approve link: " + globalConfig.getWebAppUrl() + "/#main/approve-dialog?request="
                    + UrlIdSerializer.serializeId(approveRequest.getId()))
            .build();
    emailService.sendEmailAsync(emailInfo);
}

The link in the body leads to a screen in the application. How to configure URL navigation you can find in the article: 3.5.13.3. Using URL History and Navigation API.

I’ve attached a small demo project: fsmtp.zip (95.9 KB)
To see how it works follow these steps:

  1. Open: Application → Approve request;
  2. Create entity instance;
  3. Open: Administration → Email History
  4. Select the last email and copy link from body message
  5. Go to the link → will be opened screen with approving dialog.

Also, see application properties that configure SMTP server.

Upd. Sorry, I didn’t notice that the post about Jmix. The solution above only for CUBA.

In Jmix you should add email dependencies in the build.gradle:

implementation 'io.jmix.email:jmix-email-starter'
implementation 'io.jmix.email:jmix-email-ui-starter'

For sending emails you can use Emailer bean. Actually, the code looks almost the same:

@Autowired
private Emailer emailer;

@Autowired
private Environment environment;

private boolean justCreated;

@Subscribe
public void onInitEntity(InitEntityEvent<ApproveRequest> event) {
    justCreated = true;
}

@Subscribe(target = Target.DATA_CONTEXT)
public void onPostCommit(DataContext.PostCommitEvent event) {
    if (justCreated) {
        sendByEmail();
    }
}

private void sendByEmail() {
    ApproveRequest approveRequest = getEditedEntity();
    EmailInfo emailInfo = EmailInfoBuilder.create()
            .setAddresses("test@my_domain.com")
            .setSubject(approveRequest.getName())
            .setFrom(null)
            .setBody("Approve link: http://"
                    + environment.getProperty("server.address") + ":" + environment.getProperty("server.port")
                    + "/#/approve-dialog?request="
                    + UrlIdSerializer.serializeId(approveRequest.getId()))
            .build();
    emailer.sendEmailAsync(emailInfo);
}

See demo project for more details: demo.zip (89.0 KB)

The reproduce scenario the same as for demo project above. Also, see the list of Email properties: EmailerProperties.