Thank you for that heads' Up. I was able to figure out a path and I was wondering if you can help me to get the following info from the CommonController. In order to send an Email that contains the contactUs supplied info, I would like to configure the sender with the following: 1. The default Email Address that I initially setup for the Store 2. The default SMTP Settings that I provided during the configuration, such as my sendgrid Host, User and password, and other network settings. For example, I would like to configure these properly: //send email mail.To.Add(new MailAddress(Store.DefaultEmailAddress))); using (var smtpClient = new SmtpClient()) { smtpClient.Host = SendGrid.Host; smtpClient.Port = SendGrid.Port; smtpClient.EnableSsl = SendGrid.EnableSsl; smtpClient.Credentials = new NetworkCredential(SendGrid.Username, SendGrid.Password); smtpClient.Send(mail); }
I looked at the EmailSender.cs, IEmailSender.cs and found that this info was already getting paseed into these methods. Please let me know how to setup CommonController to get these variables.
Also, is it possible to add this modification to the Theme that I am creating or do I have to modify the original nop4.2 code ?
Sorry i am a little confused - you want to use this method to send an email straight away because you dont want to save the uploaded file ? or do you still want to use the queing system like normal emails ?
The model for the contactus view is prepared using public virtual ContactUsModel PrepareContactUsModel(ContactUsModel model, bool excludeProperties) in nopCommerce42\Presentation\Nop.Web\Factories\CommonModelFactory.cs
You could change the model and fill in the new fields in this routine
webzest wrote:
Also, is it possible to add this modification to the Theme that I am creating or do I have to modify the original nop4.2 code ?.
You can not add code to a theme - only .css, content files and .cshtml So you would need to change the core code
You are correct, I am trying to avoid saving ContactUs files to the Server and would like to simply send the Email directly. Specifically, instead of providing the SendGrid credentials to ContactUsSend CommonController, I would like to use the information that I already provided to nop from the following fields during setup, which are the Default Store Email address and SMTP Service Credentilas.
I tried to inject the "using Nop.Web.Areas.Admin.Models.Messages" into the CommonController, attempting to get these properties from the EmailAccountModel, but is is failing to at run time.
Would you happen to know an easy way to get these values into the CommonController?
public string Email { get; set; }
[NopResourceDisplayName("Admin.Configuration.EmailAccounts.Fields.DisplayName")] public string DisplayName { get; set; }
[NopResourceDisplayName("Admin.Configuration.EmailAccounts.Fields.Host")] public string Host { get; set; }
[NopResourceDisplayName("Admin.Configuration.EmailAccounts.Fields.Port")] public int Port { get; set; }
[NopResourceDisplayName("Admin.Configuration.EmailAccounts.Fields.Username")] public string Username { get; set; }
[NopResourceDisplayName("Admin.Configuration.EmailAccounts.Fields.Password")] [DataType(DataType.Password)] [NoTrim] public string Password { get; set; }
[NopResourceDisplayName("Admin.Configuration.EmailAccounts.Fields.EnableSsl")] public bool EnableSsl { get; set; }
Those values are in the database which you can access adding the following code into the top of nopCommerce42\Presentation\Nop.Web\Views\Common\ContactUs.cshtml Then the values are available in emailAccount.*
I was trying to do the same thing, but winded up writing a dedicated controller for the Contact page, using the instructions from this thread and the System.Net.Mail functions, which handled the Attachment and SMTP call to SendGrid directly. Of course you have to inject these to get to the default Email of your Store: private readonly EmailAccountSettings _emailAccountSettings; private readonly IEmailAccountService _emailAccountService;
Then, from your Nop.Web/Controllers/CommonControllers/ContactUsSend you can setup your Email processor to process the Email body and attachment. I am providing the steps below. the IList<IFromFile> ImageFile comes from your csHtml Form field that encapsulates the attahced files information for processing.
Exactly like this:
[HttpPost, ActionName("ContactUs")] [PublicAntiForgery] [ValidateCaptcha] //available even when a store is closed [CheckAccessClosedStore(true)] public virtual IActionResult ContactUsSend(ContactUsModel model, bool captchaValid, IList<IFormFile> ImageFile) { //validate CAPTCHA if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid) { ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptchaMessage")); }
model = _commonModelFactory.PrepareContactUsModel(model, true);
if (ModelState.IsValid) { var subject = _commonSettings.SubjectFieldOnContactUsForm ? model.Subject : null; var emailAd = _emailAccountService.GetEmailAccountById(_emailAccountSettings.DefaultEmailAccountId);
try { MailAddress from = new MailAddress(model.Email.Trim(), model.FullName.Trim()); MailAddress to = new MailAddress(emailAd.Email.Trim(), emailAd.DisplayName.Trim()); MailMessage mail = new MailMessage(from, to); mail.Subject = subject; mail.Body = Core.Html.HtmlHelper.FormatText(model.Enquiry, false, true, false, false, false, false); foreach (var file in ImageFile) { if (file.Length > 0) { using (var ms = new MemoryStream()) { file.CopyTo(ms); var fileBytes = ms.ToArray(); Attachment data = new Attachment(new MemoryStream(fileBytes), file.FileName); mail.Attachments.Add(data); } } }
SmtpClient SmtpServer = new SmtpClient(emailAd.Host.Trim()); SmtpServer.Port = emailAd.Port; SmtpServer.Credentials = new System.Net.NetworkCredential(emailAd.Username.Trim(), emailAd.Password.Trim()); SmtpServer.EnableSsl = emailAd.EnableSsl; SmtpServer.Send(mail);