Search in sources :

Example 11 with EmailAddress

use of com.helger.commons.email.EmailAddress in project peppol-practical by phax.

the class PagePublicContact method fillContent.

@Override
protected void fillContent(@Nonnull final WebPageExecutionContext aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final String sRecaptchWebKey = AppConfig.getRecaptchaWebKey();
    final String sRecaptchSecretKey = AppConfig.getRecaptchaSecretKey();
    final boolean bRecaptchaEnabled = StringHelper.hasText(sRecaptchWebKey) && StringHelper.hasText(sRecaptchSecretKey);
    aNodeList.addChild(p("If you have general questions concerning Peppol technology, you may contact me using the form below. Please be aware, that I run this page on a voluntary basis and that the answers you may receive are my personal answers and not official OpenPeppol answers."));
    boolean bShowForm = true;
    final FormErrorList aFormErrors = new FormErrorList();
    if (aWPEC.hasAction(CPageParam.ACTION_PERFORM)) {
        final String sName = StringHelper.trim(aWPEC.params().getAsString(FIELD_NAME));
        final String sEmail = StringHelper.trim(aWPEC.params().getAsString(FIELD_EMAIL));
        final String sTopic = aWPEC.params().getAsString(FIELD_TOPIC);
        final String sText = StringHelper.trim(aWPEC.params().getAsString(FIELD_TEXT));
        final String sReCaptchaResponse = StringHelper.trim(aWPEC.params().getAsString(HCReCaptchaV2.RESPONSE_PARAMETER_NAME));
        if (StringHelper.hasNoText(sName))
            aFormErrors.addFieldError(FIELD_NAME, "Your name must be provided.");
        if (StringHelper.hasNoText(sEmail))
            aFormErrors.addFieldError(FIELD_EMAIL, "Your email address must be provided.");
        else if (!EmailAddressHelper.isValid(sEmail))
            aFormErrors.addFieldError(FIELD_EMAIL, "The provided email address is invalid.");
        if (StringHelper.hasNoText(sText))
            aFormErrors.addFieldError(FIELD_TEXT, "A message text must be provided.");
        if (bRecaptchaEnabled)
            if (aFormErrors.isEmpty() || StringHelper.hasText(sReCaptchaResponse)) {
                if (!CaptchaStateSessionSingleton.getInstance().isChecked()) {
                    // Check only if no other errors occurred
                    if (ReCaptchaServerSideValidator.check(sRecaptchSecretKey, sReCaptchaResponse).isFailure())
                        aFormErrors.addFieldError(FIELD_CAPTCHA, "Please confirm you are not a robot!");
                    else
                        CaptchaStateSessionSingleton.getInstance().setChecked();
                }
            }
        if (aFormErrors.isEmpty()) {
            if (!_isSpamBody(sText)) {
                final EmailData aEmailData = new EmailData(EEmailType.TEXT);
                aEmailData.setFrom(new EmailAddress("peppol-practical@helger.com", "Peppol Practical"));
                aEmailData.to().add(new EmailAddress("philip@helger.com"));
                aEmailData.replyTo().add(new EmailAddress(sEmail, sName));
                aEmailData.setSubject("[" + AppHelper.getApplicationTitle() + "] Contact Form - " + sName);
                final StringBuilder aSB = new StringBuilder();
                aSB.append("Contact form from " + AppHelper.getApplicationTitle() + " was filled out.\n\n");
                aSB.append("Name: ").append(sName).append("\n");
                aSB.append("Email: ").append(sEmail).append("\n");
                aSB.append("Topic: ").append(sTopic).append("\n");
                aSB.append("Text:\n").append(sText).append("\n");
                aEmailData.setBody(aSB.toString());
                LOGGER.info("Sending contact form from '" + sName + "' and email '" + sEmail + "' with topic '" + sTopic + "'");
                ScopedMailAPI.getInstance().queueMail(InternalErrorSettings.getSMTPSettings(), aEmailData);
            } else
                LOGGER.info("Ignoring spam contact form from '" + sEmail + "' with topic '" + sTopic + "'");
            aNodeList.addChild(success("Thank you for your message. Please note that I run this page on a voluntary basis on my expenses - you may consider a donation."));
            bShowForm = false;
        }
    }
    if (bShowForm) {
        final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormSelf(aWPEC));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Your name").setCtrl(new HCEdit(new RequestField(FIELD_NAME))).setErrorList(aFormErrors.getListOfField(FIELD_NAME)));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Your email address").setCtrl(new HCEdit(new RequestField(FIELD_EMAIL))).setErrorList(aFormErrors.getListOfField(FIELD_EMAIL)));
        final HCExtSelect aSelect = new HCExtSelect(new RequestField(FIELD_TOPIC));
        aSelect.addOption("SML/SMK");
        aSelect.addOption("SMP");
        aSelect.addOption("AccessPoint (AP)/AS4");
        aSelect.addOption("Peppol Directory");
        aSelect.addOption("Validation");
        aSelect.addOption("CEF");
        aSelect.addOption("General question");
        aSelect.addOptionPleaseSelect(aDisplayLocale);
        aForm.addFormGroup(new BootstrapFormGroup().setLabel("Topic").setCtrl(aSelect).setErrorList(aFormErrors.getListOfField(FIELD_TOPIC)));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Your message").setCtrl(new HCTextAreaAutosize(new RequestField(FIELD_TEXT)).setRows(5)).setErrorList(aFormErrors.getListOfField(FIELD_TEXT)));
        if (bRecaptchaEnabled)
            if (!CaptchaStateSessionSingleton.getInstance().isChecked()) {
                // Add visible Captcha
                aForm.addFormGroup(new BootstrapFormGroup().setCtrl(HCReCaptchaV2.create(sRecaptchWebKey, aDisplayLocale)).setErrorList(aFormErrors.getListOfField(FIELD_CAPTCHA)));
            }
        aForm.addChild(new HCHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM));
        aForm.addChild(new BootstrapSubmitButton().addChild("Send message").setIcon(EFontAwesome4Icon.PAPER_PLANE));
    }
}
Also used : Locale(java.util.Locale) HCExtSelect(com.helger.photon.uicore.html.select.HCExtSelect) HCNodeList(com.helger.html.hc.impl.HCNodeList) HCHiddenField(com.helger.html.hc.html.forms.HCHiddenField) HCTextAreaAutosize(com.helger.photon.uictrls.autosize.HCTextAreaAutosize) FormErrorList(com.helger.photon.core.form.FormErrorList) HCEdit(com.helger.html.hc.html.forms.HCEdit) EmailData(com.helger.smtp.data.EmailData) EmailAddress(com.helger.commons.email.EmailAddress) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) BootstrapSubmitButton(com.helger.photon.bootstrap4.button.BootstrapSubmitButton) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) RequestField(com.helger.photon.core.form.RequestField)

Aggregations

EmailAddress (com.helger.commons.email.EmailAddress)11 EmailData (com.helger.smtp.data.EmailData)5 Test (org.junit.Test)4 SMTPSettings (com.helger.smtp.settings.SMTPSettings)3 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 HCEdit (com.helger.html.hc.html.forms.HCEdit)2 HCHiddenField (com.helger.html.hc.html.forms.HCHiddenField)2 HCNodeList (com.helger.html.hc.impl.HCNodeList)2 BootstrapSubmitButton (com.helger.photon.bootstrap4.button.BootstrapSubmitButton)2 BootstrapForm (com.helger.photon.bootstrap4.form.BootstrapForm)2 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)2 FormErrorList (com.helger.photon.core.form.FormErrorList)2 RequestField (com.helger.photon.core.form.RequestField)2 CNamedSMTPSettings (com.helger.photon.core.smtp.CNamedSMTPSettings)2 NamedSMTPSettings (com.helger.photon.core.smtp.NamedSMTPSettings)2 HCExtSelect (com.helger.photon.uicore.html.select.HCExtSelect)2 HCTextAreaAutosize (com.helger.photon.uictrls.autosize.HCTextAreaAutosize)2 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)2 ISMTPSettings (com.helger.smtp.settings.ISMTPSettings)2