diff --git a/.gitignore b/.gitignore index 31db540..06ab499 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **/bin -**/obj \ No newline at end of file +**/obj +**/appsettings.local.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c75fabf --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/test/ContactForm.Tests/bin/Debug/netcoreapp2.1/ContactForm.Tests.dll", + "args": [], + "cwd": "${workspaceFolder}/test/ContactForm.Tests", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0699c36 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/test/ContactForm.Tests/ContactForm.Tests.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 3fd847e..ebe966b 100644 --- a/README.md +++ b/README.md @@ -12,5 +12,5 @@ This function is based off of my [lamda-dotnet-console](https://github.com/hyett ``` cd src/ContactForm -dotnet lambda deploy-function +dotnet lambda deploy-function ContactForm ``` diff --git a/src/ContactForm/App.cs b/src/ContactForm/App.cs index d05c5a7..01f7757 100644 --- a/src/ContactForm/App.cs +++ b/src/ContactForm/App.cs @@ -1,4 +1,13 @@ using System; +using System.Text; +using System.Threading.Tasks; + +using ContactForm.Models; + +using MailKit.Net.Smtp; + +using MimeKit; +using MimeKit.Text; using Serilog; @@ -20,12 +29,37 @@ namespace ContactForm throw new ArgumentNullException(nameof(settings)); } - public string Run(string input) + public async Task Run(ContactRequest input) { - _logger.Information("Lambda Function Starting"); - var result = $"{_settings.Prefix}{input?.ToUpper()}"; - _logger.Information("Lambda Function Finished"); - return result; + var emailBody = new StringBuilder(); + emailBody.AppendLine($"Name: {input.Name}"); + emailBody.AppendLine($"Email: {input.Email}"); + emailBody.AppendLine($"Phone: {input.Phone}"); + emailBody.AppendLine($"Website: {input.Website}"); + emailBody.AppendLine($"Message: {input.Body}"); + + await SendEmail(emailBody.ToString()); + } + + public async Task SendEmail(string body) + { + var message = new MimeMessage(); + message.From.Add(new MailboxAddress("Contact Form", _settings.EmailFrom)); + message.To.Add(new MailboxAddress(_settings.EmailTo)); + message.Subject = "Contact Request"; + message.Body = new TextPart(TextFormat.Text) + { + Text = body + }; + + using(var client = new SmtpClient()) + { + client.Connect(_settings.Host, _settings.Port, _settings.Port == 465); + client.AuthenticationMechanisms.Remove("XOAUTH2"); + await client.AuthenticateAsync(_settings.Username, _settings.Password); + await client.SendAsync(message); + await client.DisconnectAsync(true); + } } } } \ No newline at end of file diff --git a/src/ContactForm/ContactForm.csproj b/src/ContactForm/ContactForm.csproj index b6d202d..e21403e 100644 --- a/src/ContactForm/ContactForm.csproj +++ b/src/ContactForm/ContactForm.csproj @@ -18,8 +18,11 @@ + + + \ No newline at end of file diff --git a/src/ContactForm/Function.cs b/src/ContactForm/Function.cs index 53bb713..dfc839e 100644 --- a/src/ContactForm/Function.cs +++ b/src/ContactForm/Function.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using Amazon.Lambda.Core; +using ContactForm.Models; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -29,11 +31,12 @@ namespace ContactForm /// /// /// string - public string FunctionHandler(string input, ILambdaContext context) + public string FunctionHandler(ContactRequest input, ILambdaContext context) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional : false, reloadOnChange : true) + .AddJsonFile("appsettings.local.json", optional : true, reloadOnChange : true) .AddEnvironmentVariables(prefix: "LAMBDA_") .Build(); @@ -50,9 +53,12 @@ namespace ContactForm ConfigureServices(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); - var result = serviceProvider.GetService().Run(input); + var appService = serviceProvider.GetService(); + + appService.Run(input).GetAwaiter().GetResult(); + Log.CloseAndFlush(); - return result; + return "OK"; } private void ConfigureServices(IServiceCollection serviceCollection) diff --git a/src/ContactForm/Models/ContactRequest.cs b/src/ContactForm/Models/ContactRequest.cs new file mode 100644 index 0000000..b11a71d --- /dev/null +++ b/src/ContactForm/Models/ContactRequest.cs @@ -0,0 +1,11 @@ +namespace ContactForm.Models +{ + public class ContactRequest + { + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + public string Website { get; set; } + public string Body { get; set; } + } +} \ No newline at end of file diff --git a/src/ContactForm/Settings/AppSettings.cs b/src/ContactForm/Settings/AppSettings.cs index 358f15b..acdd067 100644 --- a/src/ContactForm/Settings/AppSettings.cs +++ b/src/ContactForm/Settings/AppSettings.cs @@ -2,6 +2,11 @@ namespace ContactForm { public class AppSettings { - public string Prefix { get; set; } + public string EmailFrom { get; set; } + public string EmailTo { get; set; } + public string Host { get; set; } + public int Port { get; set; } + public string Username { get; set; } + public string Password { get; set; } } } \ No newline at end of file diff --git a/src/ContactForm/appsettings.json b/src/ContactForm/appsettings.json index 7a7dc0c..94900fa 100644 --- a/src/ContactForm/appsettings.json +++ b/src/ContactForm/appsettings.json @@ -1,6 +1,11 @@ { "App": { - "Prefix": "Test" + "EmailFrom": "sender@example.com", + "EmailTo": "hello@example.com", + "Host": "smtp.example.com", + "Port": 465, + "Username": "email-username", + "Password": "email-password" }, "Serilog": { "Using": ["Serilog.Sinks.Seq"], diff --git a/test/ContactForm.Tests/ContactForm.Tests.csproj b/test/ContactForm.Tests/ContactForm.Tests.csproj index 3bc3944..53330c4 100644 --- a/test/ContactForm.Tests/ContactForm.Tests.csproj +++ b/test/ContactForm.Tests/ContactForm.Tests.csproj @@ -19,5 +19,6 @@ + diff --git a/test/ContactForm.Tests/FunctionTest.cs b/test/ContactForm.Tests/FunctionTest.cs index 77d51cd..cc9a62a 100644 --- a/test/ContactForm.Tests/FunctionTest.cs +++ b/test/ContactForm.Tests/FunctionTest.cs @@ -7,6 +7,7 @@ using Amazon.Lambda.Core; using Amazon.Lambda.TestUtilities; using ContactForm; +using ContactForm.Models; using Xunit; @@ -15,15 +16,23 @@ namespace ContactForm.Tests public class FunctionTest { [Fact] - public void TestToUpperFunction() + public void TestOKReturned() { - - // Invoke the lambda function and confirm the string was upper cased. var function = new Function(); var context = new TestLambdaContext(); - var upperCase = function.FunctionHandler("hello world", context); - Assert.Equal("TestHELLO WORLD", upperCase); + var contactRequest = new ContactRequest() + { + Name = "James Smith", + Email = "hello@example.com", + Phone = "01234567", + Website = "https://www.example.com", + Body = "This is a message" + }; + + var upperCase = function.FunctionHandler(contactRequest, context); + + Assert.Equal("OK", upperCase); } } } \ No newline at end of file