0

I'm trying to find a solution on how to send an email to multiple Hospital email addresses depending on which orders are linked to the Delivery that the change is being made on - using Postal.

So for a bit of context, I wish to send the same email to all the Hospitals that have orders linked to a delivery. So upon editing a delivery, (to change the status for example), an email must be sent to ALL the hospitals that have Orders in that Delivery.

The issue is that a Delivery can have many orders and not necessarily just one, and because of that, I'm not sure how to target all the email addresses of all the hospitals that have Orders in that specific Delivery.

Also, a Hospital is only linked to an Order and an Order is linked to a Delivery - and the only way to access the hospital's email is from the Hospital table. Which means I have to go through the Order table and then to the Hospital table.

How would I go about doing this? I'm still pretty new to MVC, so I will appreciate all the help I can get.

I'm doing this in the POST of my Edit Method in the Delivery Controller because I only wish to send an email from there after an Edit has been made. Hence why I call it after the savechanges().

These linesOrder order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); and Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); are just my attempts. They don't work, and are not part of the original code.

If there is any additional code required, please let me know and I'll add it to the question!

Models

Delivery

public class Delivery
{

    public int DeliveryID { get; set; }

    public int DriverID { get; set; }

    public virtual Driver Driver { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

Hospital

public class Hospital
{
    public int HospitalID { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

Order

   public class Order
    {
        public int OrderID { get; set; }

        public int HospitalID { get; set; }

        public int? DeliveryID { get; set; }}

        public virtual Hospital Hospital { get; set; }

        public virtual Delivery Delivery { get; set; }
    }

DeliveryVM viewModel

public class DeliveryVM
{
    public int? ID { get; set; } 

    public int DriverID { get; set; }

    public SelectList DriverList { get; set; }

    public List<OrderVM> Orders { get; set; }
}

Delivery Controller POST method:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(DeliveryVM model)
    {

        // Get the data model based on the ID of the view model
        Delivery delivery = db.Deliverys.Find(model.ID);
        // Map the view model properties to the data model
        delivery.DriverID = model.DriverID;
         ....

        db.SaveChanges();

            //Email
            Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); // I tried this but it didn't work. It was just an attempt
            Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); // I was going to use this to retrieve the hospitalIDs linked to the Order? Don't know if thats correct
            dynamic email = new Email("Example");
            email.ID = delivery.DeliveryID;
            email.To = hospital.Email; // this is where you set the email you are sending to. 
            email.Send();
            //End
        return RedirectToAction("Index");
    }

Note

  • I removed a lot of code from the Delivery Edit Method for readability, so if necessary I can add that to the question as well.
mustang00
  • 313
  • 2
  • 9
  • 23

1 Answers1

1

I think you should be able to do something like

//Email
var emailQuery = from o in db.Orders
    from h in o.Hospital
    where o.DeliveryID = model.ID 
//i'm not sure where this order comes from but if you 
//don't really have that then you may need to use your DeliverVM instead
    select new { Email = h.Email };

var emails = emailQuery.ToList();

dynamic email = new Email("Example");
email.ID = order.DeliveryID;
email.To = emails.Join(";"); // you can have more that one email because you have multiple orders
email.Send();
Joseph
  • 25,330
  • 8
  • 76
  • 125
  • I'm getting an error on this line `h in o.Hospital` its saying it failed to `SelectMany`, any ideas? – mustang00 Oct 04 '15 at 08:26
  • This line is in my DeliveryVM viewmodel `public List Orders { get; set; }` would that make a difference? – mustang00 Oct 04 '15 at 08:38
  • I changed it to use your view model variable instead, but that assumes that the ID there is the DeliveryID for your orders. – Joseph Oct 04 '15 at 13:46
  • back to my first comment again. it has an issue with `o.Hospital` - its saying it failed in the call to `SelectMany`. – mustang00 Oct 04 '15 at 14:31
  • I managed to fix that issue, however, now a new issue. The email function doesn't recognize the two emails and as emails and refuses to send. I think its because of the way the array is displaying the email addresses? `{email1@example.com};{email2@example.com}` – mustang00 Oct 04 '15 at 15:29
  • I fixed it, and I made the changes to your answer that worked for me. Thanks for setting me on the right track. – mustang00 Oct 04 '15 at 16:27
  • Seems like the [Postal documentation](http://aboutcode.net/postal/email-headers.html) suggests to seperate email addresses with "," instead of ";". – DecimalTurn Mar 02 '22 at 05:22