Schedule emails with Cronhooks and your webhook
Almost every web app requires sending emails to it's users. Either welcoming them to be onboard or sending reminders to update their most recent information.
Well sending email is pretty simple but it when it comes to scheduling those emails then it becomes much more than simple, on top of that if those scheduled emails need to be sent at specific times of different timezones than you start loosing some of your hairs (i start to pull out my beard hairs while thinking ofcourse). :D :D
That's why i built Cronhooks so you might save some of your hairs. :D So lets start digging.
1. Getting started
You will be needing a Cronhooks account. You can create a free account here.
2. Implementing the webhook endpoint in your API / web application
We will use aspnet core but this logic is pretty straight forward and can be implmented in any technology.
namespace Controllers
{
public class WebhooksController : Controller
{
public WebhooksController()
{
}
[HttpPost]
public async Task<IActionResult> SendEmail(SendEmailRequest request)
{
try
{
using (var smtpClient = CreateSMTPClient())
{
MailMessage message = new MailMessage(
new MailAddress("from@email", "From Name"),
new MailAddress(request.ToEmailAddress, request.ToName)
);
message.Subject = request.Subject;
message.IsBodyHtml = true;
message.Body = request.Message;
await smtpClient.SendMailAsync(message);
}
return Ok();
}
catch(Exception ex)
{
return Problem();
}
}
private SmtpClient CreateSMTPClient()
{
SmtpClient client = new SmtpClient("your.smtp.host", "yourport");
client.Credentials = new NetworkCredential("yourusername", "yourpassword");
client.EnableSsl = true;
return client;
}
}
public class SendEmailRequest
{
public string ToEmailAddress { get; set;}
public string ToName { get; set;}
public string Subject { get; set;}
public string Message { get; set;}
}
}
3. Create a schedule in Cronhooks
Now all setup from your side. Login into Cronhooks app and create new schedule. You can also use Cronhooks API to create schedules but we will use web app for this tutorial.
Provide a title for your understanding, URL of your webhook, timezone, time and payload. Thats all, you are all set. Now you will recieve an email at given time according to timezone.
Now you can schedule as much as emails you want using one webhook.
Thats all for now. There will be more articles for scheduling using different APIs.
Thanks,