public BodyBuilder CreateBody(WebContact webContact) { string pathToFile = Path.Combine( Environment.CurrentDirectory, "Templates", webContact.EmailTemplate + ".html" ); var builder = new BodyBuilder(); using (StreamReader SourceReader = File.OpenText(pathToFile)) { builder.HtmlBody = SourceReader.ReadToEnd(); } builder.HtmlBody = builder.HtmlBody.Replace("{webContact.Name}", webContact.Name); builder.HtmlBody = builder.HtmlBody.Replace("{webContact.Email}", webContact.Email); builder.HtmlBody = builder.HtmlBody.Replace("{webContact.Message}", webContact.Message); builder.HtmlBody = builder.HtmlBody.Replace("{webContact.SingleDetails.Ip}", webContact.SingleDetails.Ip); builder.HtmlBody = builder.HtmlBody.Replace("{webContact.SingleDetails.City}", webContact.SingleDetails.City); builder.HtmlBody = builder.HtmlBody.Replace("{webContact.SingleDetails.CountryName}", webContact.SingleDetails.CountryName); builder.TextBody = " Imię : <strong>" + webContact.Name + "</strong><br>" + " Email : " + webContact.Email + "<br>" + " Treść : " + webContact.Message + "<br><br>" + "Adres IP : <strong style=\"color:red\">" + webContact.SingleDetails.Ip + "</strong><br>" + " Kraj :" + webContact.SingleDetails.CountryName; return(builder); }
public async Task <IActionResult> Post([FromBody] WebContact webContact) { var ip = HttpContext.Connection.RemoteIpAddress.ToString(); IpStackClient client = new IpStackClient(_ipStackOptions.Key); webContact.SingleDetails = client.GetIpAddressDetails(ip); webContact.EmailTemplate = _emailOptions.Template; MailRequest request = new MailRequest { ToEmail = _emailOptions.ToAddress, Subject = _emailOptions.Subject, Body = _emailService.CreateBody(webContact) }; await _emailService.SendAsync(request); await _emailService.SendGridAsync(request); //https://www.pragimtech.com/blog/blazor/post-in-aspnet-core-rest-api/ return(StatusCode(StatusCodes.Status200OK, "Success")); }