[SOLVED] Trouble using variable for Blynk.email address parameter

I am trying to send an email from my ESP8266 hardware using Blynk.email(address, subject, body). The problem is that I’m unable to use a String variable for the address field. I can use a String variable for the subject and body, just not the email address. I get an Arduino compile error if I try to use a variable for the address.

    String emailAddress = "xyz@gmail.com";
    String subject = "mySubject";
    String body = "Email body";

      //This compiles.
      // Blynk.email("xyz@gmail.com", subject, body); 
      
      // This doesn't compile. Only difference is 1st parameter.  
      Blynk.email(emailAddress, subject, body);       

The compiler throws the following error:

no matching function for call to 'BlynkWifi::email(String&, String&, String&)'

Have any of you had success using a variable, instead of a literal string for the address parameter of Blynk.email?

After some poking around I found these email parameters in Blynk.Api.h

/**
     * Sends an email message
     *
     * @param email   Email to send to
     * @param subject Subject of message
     * @param msg     Text of the message
     */
    template <typename T1, typename T2>
    void email(const char* email, const T1& subject, const T2& msg) {
        char mem[BLYNK_MAX_SENDBYTES];
        BlynkParam cmd(mem, 0, sizeof(mem));
        cmd.add(email);
        cmd.add(subject);
        cmd.add(msg);
        static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_EMAIL, 0, cmd.getBuffer(), cmd.getLength()-1);
    }

So I tested with const char* instead of String for the email address and it works!

  const char* emailAddress = "myemail@gmail.com";
  String subject = "Mega Email Test";
  String body = "Test";

  Blynk.email(emailAddress, subject, body);

Thanks @Gunner. I will do some more testing. In my situation, the user will enter their own email address via the blynk terminal field, so I can’t use const char. I sure hope blynk doesn’t “require” a constant. That would be very limiting.

It doesn’t and you can convert most datatypes from one to another.
As posted elsewhere local server only works with Gmail.

You can apply a c_str() function to a String object to get a pointer to the char
https://www.arduino.cc/en/Reference/CStr

So, your code may look like this:

Blynk.email(emailAddress.c_str(), emailSubject.c_str(), emailMessage.c_str());
3 Likes

I was going to write this up as a bug, for consistency with the rest of Blynk calls, the email declaration should be overloaded to also allow String.