manar.t
(Manar T)
April 5, 2021, 4:23am
#1
Hi everyone!
Can you help us, we can’t get new link. Our task is to make.
User creates a new record.
To user will send a letter by mail with a link to confirmation. (We stopped in this part)
User follows the link, and confirms by pressing button Yes or No.
How can we do this by either?
Pinyazhin
(Roman Pinyazhin)
April 13, 2021, 3:25pm
#5
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:
Open: Application → Approve request;
Create entity instance;
Open: Administration → Email History
Select the last email and copy link from body message
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.
Pinyazhin
(Roman Pinyazhin)
April 14, 2021, 8:40am
#9
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 .