abdoe
abdoe

Reputation: 459

Smtp client - from and to not send

I have an smtp client build in c++ where I can send emails to my testing mailtrap account. The headers are send and the email is arriving just fine.

My problem is, that the field which indicates from/to is empty - as displayed in the screenshot

enter image description here

I'm sending the header with

write_command("MAIL FROM: <[email protected]>");
write_command("RCPT TO: <[email protected]>");

I made a gist with my full code of the Smtp client

https://gist.github.com/anonymous/7bb13de7f044bcb5d07d0e6a9d991ea9

I'm calling it from my main() function

with

 Smtp smtp_client = Smtp();
 smtp_client.new_connection("smtp.mailtrap.io", 25);
 smtp_client.auth_login("username", "password");
 smtp_client.sendmail();
 smtp_client.close_connection();

thanks for tacking a look

Upvotes: 5

Views: 607

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10067

I managed to make the fields appear, editing your sendmail function a bit:

void sendmail()
{
    write_command("MAIL FROM: <[email protected]>");
    write_command("RCPT TO: <[email protected]>");

    write_command("DATA");

    std::string data;
    data.append("MIME-Version: 1.0\r\n");
    data.append("From: <[email protected]>\r\n");
    data.append("To: <[email protected]>\r\n");
    data.append("Subject: Welcome\r\n");
    data.append("Date: Fri, 29 Dec 2017 09:30:00 -0400\r\n");
    data.append("\r\n"); //this seems to matter
    data.append("This is a test");
    data.append("\r\n.");
    write_command(data);

    write_command("QUIT");
}

I put the whole DATA in a string and sent it in a single write.

What (apparently) matters:

  1. don't start the data section with an empty line;
  2. add an empty line before the message text.

I also edited your write_command, it doesn't relate to your issue but I suggest you don't copy the string to a buffer but directly use the string instead:

    //char command_buffer[255];
    //strcpy(command_buffer, command.c_str());
    //n = write(sockfd,command_buffer,strlen(command_buffer));
    n = write(sockfd,command.c_str(),command.length());

Upvotes: 4

Related Questions