Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Send e-mails with sender retrieved from a form field View modes: 
User avatar
Member
Member
asovrano-opera21 - 6/19/2013 5:38:51 AM
   
Send e-mails with sender retrieved from a form field
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.

User avatar
Member
Member
Swainy - 6/19/2013 10:25:36 AM
   
RE:Send e-mails with sender retrieved from a form field
Hi,

Unless i've misread what you are trying to do, the issue is this line...
message.From = EmailHelper.GetSender(emailTemplate, fromEmailAddress);

This needs to be:
message.From = tbEmail.Text;

Though I would be careful doing this as some spam filters may block this as you are spoofing the email address (i.e. the email hasn't actually originated from the email you are sending from).

Thanks,
Matt

User avatar
Member
Member
asovrano-opera21 - 6/21/2013 8:29:35 AM
   
RE:Send e-mails with sender retrieved from a form field
Dear Matt, the statement:
EmailHelper.GetSender(emailTemplate, fromEmailAddress);
returns the correct value, that is tbEmail.Text.
BUT
when the e-mail is sent the Sender changes. I think it's a smtp server issue.
Maybe someone using gmail smtp server has already encountered such a problem.
Thank you very much and Kind Regards.
Andrea

User avatar
Certified Developer 8
Certified Developer 8
norashlea - 6/21/2013 3:56:20 PM
   
RE:Send e-mails with sender retrieved from a form field
Hi Andrea,
I've got 2 clients that use the gmail smtp server, and in both cases we've found that when emails from the website arrive they are always "from" the gmail account that the website authenticates with, irrespective of the email address that has been set up as the sender for the particular email - even if it's another mailbox that is in the gmail account. Also, a copy of the email is in the "Sent Items" box for the authenticating gmail account. It seems that gmail re-writes the email before it sends it.
Sharon