I am building a website for my company and I have contact form on three different pages, with diffrent URLs.
Does anyone know how you can send the current URL sent from?
Say like I'm on http://localhost:59379/en/about/
and I send a message from this URL, I want this URL send to my email.
Like:
Message Send from:{URL} in my Mailbox.
I´m using SMTP
public void SendContactMail(string from, string name, string phone, string message)
{
try
{
var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
var newMail = new MailMessage { From = new MailAddress("HEY@LIVE.SE") };
newMail.To.Add(new MailAddress("hello@live.se"));
newMail.Subject = $"Contact {@date} form mail from: {@from}";
newMail.IsBodyHtml = true;
var body = $@"
{name} har fyllt i kontaktformuläret på ... <br/>
E-post: {@from}<br/>
Telefon:{phone} <br/>
Meddelande:{message}";
var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
newMail.AlternateViews.Add(view);
Client.Send(newMail);
}
catch (Exception)
{
//some random stuff here.
}
}
HTML form:
<div class="col-xl-12">
<p class="text-white contact-us-title">Kontakta gärna oss!</p>
<input type="text" required class="form-control fc-contact" style="width:15rem;" placeholder="Namn" name="Name" aria-label="Recipient's username" aria-describedby="basic-addon2">
<br />
<input type="text" required class="form-control fc-contact" style="width:15rem;" placeholder="E-post" name="Email" aria-label="E-post" aria-describedby="basic-addon2">
<br />
<input type="text" required class="form-control fc-contact" style="width:15rem;" placeholder="Tele" name="Phone" aria-label="Tele" aria-describedby="basic-addon2">
<br />
<textarea class="form-control fc-contact" style="height:7rem; width:15rem; " placeholder="Meddelande" name="Message" aria-label="Message" aria-describedby="basic-addon2"></textarea>
<br />
<br />
<button type="submit" class="btn btn-success mb-5"><span>Skicka</span></button>
</div>
You need to somehow pass the URL of the page containing the email form, to the method sending the email so it can be included inside the email message.
The problem is most likely right now that you're posting to an action on a controller and then the Request.Url
would just be the URL of the controller action you are posting to - which is not what you want.
So .. what you could do is to include the URL of the "referrer" page (the page you have the form located on - which is the page URL you want to track) as a hidden form field next to the user input fields and then just handle that in your controller POST action and include it as part of the email message being sent.