Smtp client - from and do not send

I have a smtp client build in C ++ where I can send emails to my mailtrap account. Headers are sent and the letter arrives just fine.

My problem is that the field that indicates from / to is empty - as shown in the screenshot

enter image description here

I am sending a header with

write_command("MAIL FROM: < foo@bar.de >"); write_command("RCPT TO: < foo@bar.de >"); 

I made the point with my full Smtp client code

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

I call 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 attracting the look

+5
source share
1 answer

I managed to create the fields by slightly modifying your sendmail function:

 void sendmail() { write_command("MAIL FROM: < foo@bar.de >"); write_command("RCPT TO: < foo@bar.de >"); write_command("DATA"); std::string data; data.append("MIME-Version: 1.0\r\n"); data.append("From: < foo@bar.de >\r\n"); data.append("To: < foo@bar.de >\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 all the DATA in a string and sent it to one record.

What (apparently) matters:

  • Do not start the data section with an empty string;
  • add a blank line before the message body.

I also edited your write_command , this does not apply to your problem, but I suggest you not copy the line to the buffer, but use the line 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()); 
+4
source

Source: https://habr.com/ru/post/1274139/


All Articles