In a "contact-us" form I'd like to send to info@mydomain.com
an e-mail message where the sender / from e-mail address is the text
written into a textbox on the form by the online user.
protected void btnInvia_Click(object sender, EventArgs e)
{
EmailTemplateInfo emailTemplate = EmailTemplateProvider.GetEmailTemplate("JollyTicketContactMail", CMSContext.CurrentSiteName);
string[,] replacements = new string[5, 2];
replacements[0, 0] = "FirstName";
replacements[0, 1] = tbNome.Text;
replacements[1, 0] = "lastname";
replacements[1, 1] = tbCognome.Text;
replacements[2, 0] = "messagetext";
replacements[2, 1] = tbTesto.Text;
replacements[3, 0] = "bookingcode";
replacements[3, 1] = tbCodice.Text;
replacements[4, 0] = "requesteremail";
replacements[4, 1] = tbEmail.Text;
// Set resolver
ContextResolver resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = replacements;
// Enable macro encoding for body
resolver.EncodeResolvedValues = true;
EmailMessage message = new EmailMessage();
string fromEmailAddress = tbEmail.Text;
string toEmailAddress = "mkormos@opera21.it";
// you can set here which template to use html
// or plain text or default (which has been setup in Settings)
message.EmailFormat = EmailFormatEnum.PlainText;
message.From = EmailHelper.GetSender(emailTemplate, fromEmailAddress);
//// here message.From is what I expect it to be ......
message.Recipients = toEmailAddress;
message.ReplyTo = fromEmailAddress;
message.Subject = ddlTipo.Text;
message.Body = resolver.ResolveMacros(emailTemplate.TemplateText);
// Disable macro encoding for plaintext body and subject
resolver.EncodeResolvedValues = false;
message.PlainTextBody = resolver.ResolveMacros(emailTemplate.TemplatePlainText);
message.CcRecipients = emailTemplate.TemplateCc;
message.BccRecipients = emailTemplate.TemplateBcc;
// Attach template meta-files to e-mail
MetaFileInfoProvider.ResolveMetaFileImages(message,
emailTemplate.TemplateID,
EmailObjectType.EMAILTEMPLATE,
MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);
EmailSender.SendEmail(CMSContext.CurrentSiteName, message, true);
}
I have configured a testing environment with gmail smtp with credentials and ssl.
The e-mail is sent but when I receive it the sender is the same gmail account configured in the smtp server and NOT the email address the user edited into the field.