I have a method that returns a list of products from the database that I would like to send to a certain email address. The method I would like to send returns a list of items. There's a ContactUs formula that sends a message but from costumer to store owner that I'm considering using as a base. However the data I want to send as an email are only supposed to be sent to the storeowner email address without a sender.
I want to send the list of products that is returned from this method:
public IList<BestsellersReportLine> DailyBestsellersReport()
{
OrderStatus os;
PaymentStatus? ps = null;
ShippingStatus ss;
int billingCountryId = 0;
int recordsToReturn = 999;
int orderBy = 1;
int groupBy = 1;
int? paymentStatusId = null;
if (ps.HasValue)
paymentStatusId = (int)ps.Value;
// Specifies the time range for sold products/day
var range = new
{
startTimeUtc = DateTime.Today.AddDays(-1),
endTimeUtc = DateTime.Today.AddSeconds(-1),
CreatedOnUtc = DateTime.Today.AddDays(-1),
};
var query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (o.CreatedOnUtc >= range.startTimeUtc && o.CreatedOnUtc <= range.endTimeUtc) &&
(!paymentStatusId.HasValue || paymentStatusId == o.PaymentStatusId)
select opv;
var query2 = groupBy == 1 ?
//group by product variants
from opv in query1
group opv by opv.ProductVariantId into g
select new
{
EntityId = g.Key,
TotalAmount = g.Sum(x => x.PriceExclTax),
TotalQuantity = g.Sum(x => x.Quantity),
}
:
//group by products
from opv in query1
group opv by opv.ProductVariant.ProductId into g
select new
{
EntityId = g.Key,
TotalAmount = g.Sum(x => x.PriceExclTax),
TotalQuantity = g.Sum(x => x.Quantity),
}
;
switch (orderBy)
{
case 1:
{
query2 = query2.OrderByDescending(x => x.TotalQuantity);
}
break;
case 2:
{
query2 = query2.OrderByDescending(x => x.TotalAmount);
}
break;
default:
throw new ArgumentException("Wrong orderBy parameter", "orderBy");
}
if (recordsToReturn != 0 && recordsToReturn != int.MaxValue)
query2 = query2.Take(recordsToReturn);
var result = query2.ToList().Select(x =>
{
var reportLine = new BestsellersReportLine()
{
EntityId = x.EntityId,
TotalAmount = x.TotalAmount,
TotalQuantity = x.TotalQuantity
};
return reportLine;
}).ToList();
//return result;
return result.OrderBy(opv => opv.TotalQuantity).ToList();
}
As an email into this method:
[HttpPost, ActionName("ContactUs")]
[CaptchaValidator]
public ActionResult ContactUsSend(ContactUsModel model, bool captchaValid)
{
//validate CAPTCHA
if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid)
{
ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptcha"));
}
if (ModelState.IsValid)
{
string email = model.Email.Trim();
string fullName = model.FullName;
string subject = string.Format(_localizationService.GetResource("ContactUs.EmailSubject"), _storeInformationSettings.StoreName);
var emailAccount = _emailAccountService.GetEmailAccountById(_emailAccountSettings.DefaultEmailAccountId);
if (emailAccount == null)
emailAccount = _emailAccountService.GetAllEmailAccounts().FirstOrDefault();
string from = null;
string fromName = null;
string body = Core.Html.HtmlHelper.FormatText(model.Enquiry, false, true, false, false, false, false);
//required for some SMTP servers
if (_commonSettings.UseSystemEmailForContactUsForm)
{
from = emailAccount.Email;
fromName = emailAccount.DisplayName;
body = string.Format("<strong>From</strong>: {0} - {1}<br /><br />{2}",
Server.HtmlEncode(fullName),
Server.HtmlEncode(email), body);
}
else
{
from = email;
fromName = fullName;
}
_queuedEmailService.InsertQueuedEmail(new QueuedEmail()
{
From = from,
FromName = fromName,
To = emailAccount.Email,
ToName = emailAccount.DisplayName,
Priority = 5,
Subject = subject,
Body = body,
CreatedOnUtc = DateTime.UtcNow,
EmailAccountId = emailAccount.Id
});
model.SuccessfullySent = true;
model.Result = _localizationService.GetResource("ContactUs.YourEnquiryHasBeenSent");
//activity log
_customerActivityService.InsertActivity("PublicStore.ContactUs", _localizationService.GetResource("ActivityLog.PublicStore.ContactUs"));
return View(model);
}
model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage;
return View(model);
}
Note: ContactUs -method is a default method in NopCommerce, iv'e tried changing a few things but im not getting annywhere. I just need to send the list of products retrieved from the database through "DailyBestsellersReport method" to the email adress of the store owner.
In simple theory I would like to do something like this:
[HttpPost, ActionName("Bestsellers")]
[CaptchaValidator]
public ActionResult SendBestSellersList(BestSellersReportLine model)
{
DailyBestsellersReport();
}
Solution
Although this isn't a direct answer, If you are looking for an easy reusable option I recommend MVC Mailer, It's available as a NuGet Package:
https://www.nuget.org/packages/MvcMailer
And also there is a nice step by step that anyone can get their head around -