2
votes

I'm developing an application which will send out notification emails and I'd like to perform end-to-end testing on the email before I deploy the capability.

To this end, I've got a dummy SMTP service running on my development machine that accepts any incoming mail and just stores it in a single box that a POP client can access, no matter who the sender and receiver are.

I've used this with other applications to make sure that emails get sent, that they're readable by various clients, and so on. I see that, by default, there is a com.google.appengine.api.mail.MailService that my application code can call into to send messages and, in the real environment, it actually sends email.

However, in the dev environment, the mail seems to get dropped on the floor.

I see log messages from worker threads like this:

Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO: MailService.send
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   From: myapp <[email protected]>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   To: Recipient <[email protected]>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Reply-to: myapp <[email protected]>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Subject: Email Update
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Body:
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:     Content-type: text/plain
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:     Data length: 948

But there doesn't seem to be an obvious way for me to see the actual message. I did find an example in the Google documentation that led me to write this bit of code:

    Properties mailProps = new Properties();
    AbstractConfiguration config = ConfigurationManager.getConfig();
    String smtpHost = config.getString("email.smtp.host");
    __l.debug("Sending email via SMTP connection to host "+smtpHost);
    mailProps.setProperty("mail.smtp.host", smtpHost);
    mailProps.setProperty("mail.smtp.port", config.getString("email.smtp.port", "25"));
    mailProps.setProperty("mail.smtp.connectiontimeout", config.getString("email.smtp.connectiontimeout", "1000"));
    mailProps.setProperty("mail.smtp.timeout", config.getString("email.smtp.timeout", "1000"));
    Session session = Session.getDefaultInstance(mailProps, null);
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(config.getString("email.sender.address"), config.getString("email.sender.name")));
        msg.addRecipient(Message.RecipientType.TO,
                         new InternetAddress(toAddress, toName));
        msg.setSubject(title);
        msg.setText(messageText);
        Transport.send(msg);
        __l.info("message has been sent to " + toAddress);
    } catch (Exception e) {
        __l.warn("Exception attempting to send email to "+toAddress+" about "+title, e);
    }

However, when I run this in my dev server, it looks like I'm still using the built in MailService, and my local dummy SMTP service never actually gets contacted.

Is there any way to achieve my goal of being able to view the app-generated email in an email client, or do I just get to debug every new email template in "the big lab"?

1

1 Answers

1
votes

The behaviour is expected if you didn't configure your development server to use sendmail or your local SMTP server. From Mail and the development server:

The development server can be configured to send email messages directly from your computer when you test a feature of your app that sends messages. You can configure the development server to use an SMTP server of your choice. Alternatively, you can tell the development server to use Sendmail, if Sendmail is installed on your computer and set up for sending email.

If you do not configure an SMTP server or enable Sendmail, when your app calls the Mail service, the development server will log the contents of the message. The message will not actually be sent.

The options for configuring your local development server to use sendmail or a specific SMTP server are documented in Local Development Server Options:

--enable_sendmail=yes|no
    Uses the local computer's Sendmail installation for sending email messages.

...

--smtp_host=...
    The hostname of the SMTP server to use for sending email messages.
--smtp_port=...
    The port number of the SMTP server to use for sending email messages.
--smtp_user=...
    The username to use with the SMTP server for sending email messages.
--smtp_password=...
    The password to use with the SMTP server for sending email messages.

Update:

As @Stephen B noted the above only applies to the python sandbox, the java Mail and the development server is just:

When an application running in the development server calls the Mail service to send an email message, the message is printed to the application logs. The development server does not send the email message.