Search in sources :

Example 1 with EmailAddress

use of com.helger.commons.email.EmailAddress in project phoss-directory 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 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 sReCaptcha = StringHelper.trim(aWPEC.params().getAsString("g-recaptcha-response"));
        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 (aFormErrors.isEmpty() || StringHelper.hasText(sReCaptcha)) {
            if (!CaptchaSessionSingleton.getInstance().isChecked()) {
                // Check only if no other errors occurred
                if (ReCaptchaServerSideValidator.check("6LfZFS0UAAAAAONDJHyDnuUUvMB_oNmJxz9Utxza", sReCaptcha).isFailure())
                    aFormErrors.addFieldError(FIELD_CAPTCHA, "Please confirm you are not a robot!");
                else
                    CaptchaSessionSingleton.getInstance().setChecked();
            }
        }
        if (aFormErrors.isEmpty()) {
            final EmailData aEmailData = new EmailData(EEmailType.TEXT);
            aEmailData.setFrom(CPDPublisher.EMAIL_SENDER);
            aEmailData.to().add(new EmailAddress("pd@helger.com"));
            aEmailData.replyTo().add(new EmailAddress(sEmail, sName));
            aEmailData.setSubject("[" + CPDPublisher.getApplication() + "] Contact Form - " + sName);
            final StringBuilder aSB = new StringBuilder();
            aSB.append("Contact form from " + CPDPublisher.getApplication() + " 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());
            ScopedMailAPI.getInstance().queueMail(InternalErrorSettings.getSMTPSettings(), aEmailData);
            aWPEC.postRedirectGetInternal(success("Thank you for your message. We will come back to you asap."));
        }
    }
    if (bShowForm) {
        aNodeList.addChild(info("Alternatively write an email to pd[at]helger.com - usually using the below form is more effective!"));
        aNodeList.addChild(warn("Please don't request any change of data via this contact form - contact your service provider instead. Thank you."));
        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("SMP integration");
        aSelect.addOption("Website");
        aSelect.addOption("REST service");
        aSelect.addOption("SMP Statement of use");
        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 (!CaptchaSessionSingleton.getInstance().isChecked()) {
            // Add visible Captcha
            aForm.addFormGroup(new BootstrapFormGroup().setCtrl(HCReCaptchaV2.create("6LfZFS0UAAAAAJaqpHJdFS_xxY7dqMQjXoBIQWOD", aDisplayLocale)).setErrorList(aFormErrors.getListOfField(FIELD_CAPTCHA)));
        }
        aForm.addChild(new HCHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM));
        aForm.addChild(new BootstrapSubmitButton().addChild("Send message").setIcon(EDefaultIcon.YES));
        aForm.addChild(new BootstrapButton().addChild("No thanks").setIcon(EDefaultIcon.CANCEL).setOnClick(aWPEC.getLinkToMenuItem(CMenuPublic.MENU_SEARCH_SIMPLE)));
    }
}
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) BootstrapButton(com.helger.photon.bootstrap4.button.BootstrapButton) BootstrapSubmitButton(com.helger.photon.bootstrap4.button.BootstrapSubmitButton) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) RequestField(com.helger.photon.core.form.RequestField)

Example 2 with EmailAddress

use of com.helger.commons.email.EmailAddress in project phoss-smp by phax.

the class SMPInternalErrorHandler method doSetup.

public static void doSetup() {
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    final String sSenderAddress = aCF.getAsString("smp.errorhandler.sender.email");
    final String sSenderName = aCF.getAsString("smp.errorhandler.sender.name", "SMP Internal Error Sender");
    final String sReceiverAddress = aCF.getAsString("smp.errorhandler.receiver.email");
    final String sReceiverName = aCF.getAsString("smp.errorhandler.receiver.name");
    final String sSMTPHostName = aCF.getAsString("smp.smtp.hostname");
    final int nSMTPPort = aCF.getAsInt("smp.smtp.port", -1);
    final String sSMTPUserName = aCF.getAsString("smp.smtp.username");
    final String sSMTPPassword = aCF.getAsString("smp.smtp.password");
    final boolean bSMTPSSLEnabled = aCF.getAsBoolean("smp.smtp.ssl", false);
    final boolean bSMTPSTARTTLSEnabled = aCF.getAsBoolean("smp.smtp.starttls", false);
    final long nSMTPConnectionTimeoutMS = aCF.getAsLong("smp.smtp.connectiontimeoutms", 10_000);
    final long nSMTPSocketTimeoutMS = aCF.getAsLong("smp.smtp.sockettimeoutms", 10_000);
    final boolean bSMTPDebug = aCF.getAsBoolean("smp.smtp.debug", false);
    final SMTPSettings aSMTPSettings = StringHelper.hasText(sSMTPHostName) ? new SMTPSettings(sSMTPHostName, nSMTPPort, sSMTPUserName, sSMTPPassword, StandardCharsets.UTF_8, bSMTPSSLEnabled, bSMTPSTARTTLSEnabled, nSMTPConnectionTimeoutMS, nSMTPSocketTimeoutMS, bSMTPDebug) : null;
    if (StringHelper.hasText(sSenderAddress) && StringHelper.hasText(sReceiverAddress) && aSMTPSettings != null && aSMTPSettings.areRequiredFieldsSet()) {
        // Set global internal error handlers
        new SMPInternalErrorHandler().install();
        InternalErrorSettings.setSMTPSenderAddress(new EmailAddress(sSenderAddress, sSenderName));
        InternalErrorSettings.setSMTPReceiverAddresses(new EmailAddress(sReceiverAddress, sReceiverName));
        InternalErrorSettings.setSMTPSettings(aSMTPSettings);
        InternalErrorSettings.setFallbackLocale(CSMPServer.DEFAULT_LOCALE);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Setup internal error handler to send emails on internal errors to " + sReceiverAddress);
    } else {
        LOGGER.info("No internal error handler configuration was found. So not sending emails in case of error.");
    }
}
Also used : SMTPSettings(com.helger.smtp.settings.SMTPSettings) ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) EmailAddress(com.helger.commons.email.EmailAddress)

Example 3 with EmailAddress

use of com.helger.commons.email.EmailAddress in project ph-web by phax.

the class TypeConverterRegistrar_ph_mail method registerTypeConverter.

public void registerTypeConverter(@Nonnull final ITypeConverterRegistry aRegistry) {
    aRegistry.registerTypeConverter(InternetAddress.class, EmailAddress.class, aSource -> new EmailAddress(aSource.getAddress(), aSource.getPersonal()));
    aRegistry.registerTypeConverter(EmailAddress.class, InternetAddress.class, aSource -> {
        try {
            return new InternetAddress(aSource.getAddress(), aSource.getPersonal(), StandardCharsets.UTF_8.name());
        } catch (final UnsupportedEncodingException ex) {
            throw new IllegalStateException("UTF-8 is unknown", ex);
        }
    });
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EmailAddress(com.helger.commons.email.EmailAddress)

Example 4 with EmailAddress

use of com.helger.commons.email.EmailAddress in project ph-web by phax.

the class TypeConverterFuncTest method testEmailAddress.

@Test
public void testEmailAddress() {
    final EmailAddress aEA = new EmailAddress("spam@helger.com", "Philip");
    final InternetAddress aIA = TypeConverter.convert(aEA, InternetAddress.class);
    assertNotNull(aIA);
    final EmailAddress aEA2 = TypeConverter.convert(aIA, EmailAddress.class);
    assertNotNull(aEA2);
    assertEquals(aEA, aEA2);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) EmailAddress(com.helger.commons.email.EmailAddress) Test(org.junit.Test)

Example 5 with EmailAddress

use of com.helger.commons.email.EmailAddress in project ph-web by phax.

the class FailedMailDataTest method _createEmailData.

@Nonnull
private static EmailData _createEmailData() {
    final EmailData aData = new EmailData(EEmailType.TEXT);
    aData.setFrom(new EmailAddress("from@example.org"));
    aData.replyTo().addAll(new EmailAddress("replyto1@example.org"), new EmailAddress("replyto2@example.org"), new EmailAddress("replyto3@example.org"));
    aData.to().addAll(new EmailAddress("to1@example.org"), new EmailAddress("to2@example.org"), new EmailAddress("to3@example.org"));
    aData.cc().addAll(new EmailAddress("cc1@example.org"), new EmailAddress("cc2@example.org"), new EmailAddress("cc3@example.org"));
    aData.bcc().addAll(new EmailAddress("bcc1@example.org"), new EmailAddress("bcc2@example.org"), new EmailAddress("bcc3@example.org"));
    // Ensure 3 fraction digits are present
    aData.setSentDateTime(PDTFactory.getCurrentLocalDateTimeMillisOnly().withNano(100 * (int) CGlobal.NANOSECONDS_PER_MILLISECOND));
    aData.setSubject("This is a test");
    aData.setBody("This is my mail that failed\nIt contains a lot of information.\nBla bla bla\nAll the best to you.");
    final EmailAttachmentList aAttachments = new EmailAttachmentList();
    aAttachments.addAttachment(new EmailAttachment("file1.txt", "Bla foo fasel".getBytes(StandardCharsets.UTF_8)));
    aAttachments.addAttachment(new EmailAttachment("file2.txt", "Bla foo fasel. Bla foo fasel.".getBytes(StandardCharsets.UTF_8)));
    aData.setAttachments(aAttachments);
    // Some custom attributes for fun
    aData.attrs().putIn("Attr1", "3.14");
    aData.attrs().putIn("Attr2", "Test attribute\nWe are multiline!");
    return aData;
}
Also used : EmailAttachment(com.helger.smtp.data.EmailAttachment) EmailData(com.helger.smtp.data.EmailData) EmailAttachmentList(com.helger.smtp.data.EmailAttachmentList) EmailAddress(com.helger.commons.email.EmailAddress) Nonnull(javax.annotation.Nonnull)

Aggregations

EmailAddress (com.helger.commons.email.EmailAddress)9 EmailData (com.helger.smtp.data.EmailData)4 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 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)2 InternetAddress (javax.mail.internet.InternetAddress)2 Ignore (org.junit.Ignore)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 HCEdit (com.helger.html.hc.html.forms.HCEdit)1 HCHiddenField (com.helger.html.hc.html.forms.HCHiddenField)1 HCNodeList (com.helger.html.hc.impl.HCNodeList)1 BootstrapButton (com.helger.photon.bootstrap4.button.BootstrapButton)1 BootstrapSubmitButton (com.helger.photon.bootstrap4.button.BootstrapSubmitButton)1 BootstrapForm (com.helger.photon.bootstrap4.form.BootstrapForm)1 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)1 FormErrorList (com.helger.photon.core.form.FormErrorList)1 RequestField (com.helger.photon.core.form.RequestField)1 CNamedSMTPSettings (com.helger.photon.core.smtp.CNamedSMTPSettings)1