【问题标题】:How to pass C# variable in php url (Html e-mail button)如何在 php url 中传递 C# 变量(Html 电子邮件按钮)
【发布时间】:2018-12-07 18:53:01
【问题描述】:

我的应用程序使用 Gmail API 向用户发送包含 button 的 Html 电子邮件。按钮 URL 必须具有内置用户的电子邮件地址(toAddress 的值)。

我的问题是如何结合 C# 变量 toAddress 和 php URL 传递,使目标链接看起来像示例:

<a href = http://mypage.com/Email-list.php?email=VALUE_OF_toAddress

之后我会将toAddress 值保存到数据库中。

这里是sendEmail 方法和htmlbody

public static string sendEmail(string emailTo)
    {
        string fromEmailPassword = ConfigurationManager.AppSettings["FromEmailPassword"];
        string fromEmail = ConfigurationManager.AppSettings["LastLogin"];
        string fromPassword = fromEmailPassword;


        var fromAddress = new MailAddress(fromEmail, fromEmail);
        var toAddress = new MailAddress(emailTo, emailTo);

        string subject = "Player";
        string body = "";

        try
        {
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };


            MailMessage message = new MailMessage(fromAddress, toAddress);

            message.Subject = "Player";
            message.IsBodyHtml = true;
            string htmlBody;

            htmlBody = @"
                        <html lang=""en"">
                            <head>    
                                <meta content=""text/html; charset=utf-8"" http-equiv=""Content-Type"">
                                <title>
                                    Player
                                </title>
                                <style type=""text/css"">
                                </style>
                            </head>
                            <body>
                            <div class="button">
                                <a href = http://mypage.com/Email-list.php?(-??????-) target =_blank style=""display: block; text-decoration: none;-webkit-text-size-adjust: none;text-align: center;color: #ffffff; background-color: #3F2409; border-radius: 4px; -webkit-border-radius: 4px; -moz-border-radius: 4px; max-width: 132px; width: 92px;width: auto; border-top: 0px solid transparent; border-right: 0px solid transparent; border-bottom: 0px solid transparent; border-left: 0px solid transparent; padding-top: 5px; padding-right: 20px; padding-bottom: 5px; padding-left: 20px; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;mso-border-alt: none"">
                                <span style = ""font-size:16px;line-height:32px;"" > Yes, I Agree</span>
                                </a>
                            </div>
                            </body >
                        </html >
                        ";


            message.Body = htmlBody;

            {
                smtp.Send(message);
                return "Sent";
            }

        }`enter code here`

【问题讨论】:

    标签: c# php html email url-parameters


    【解决方案1】:

    您可以像 $@"&lt;text_here" 一样对字符串文字 htmlBody 使用字符串插值。因此,基本上将$ 放在@ 之前,然后使用{} 将您的toAddress 变量包围在href 中。

    像这样:

    $@"&lt;whole_lot_of_html&gt; href=""http://mypage.com/Email-list.php?email={toAddress} &lt;rest_of_your_html&gt;"

    根据您的示例,我不知道您的数据层是如何设置的,因此我无法为您提供有关如何将toAddress 保存到数据库的指示。但是,如果您在发送电子邮件后仍然可以访问您的 toAddress 变量,您应该可以在此处处理。话虽如此,您可能想为此提出另一个问题。

    【讨论】:

      【解决方案2】:

      解决办法

      string htmlBody = string.Format("<html lang=\"en\"> <head> <meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"> <title> Player </title> <style type=\"text/css\"> </style> </head> <body> <div> <a href=\"http://mypage.com/Email-list.php?email={0}\" target=\"blank\" style=\"display: block; text-decoration: none;-webkit-text-size-adjust: none;text-align: center;\"> <span style=\"font-size:16px;line-height:32px;\">Yes, I Agree</span> </a> </div> </body > </html >",emailTo);
      

      带有附加到 URL 的电子邮件地址(C# 变量 emailTo)的 HTML 按钮

      &lt;html&gt;&lt;body&gt;div&gt;&lt;a href=\"http://mypage.com/Email-list.php?email={0}\"&gt;Submit&lt;/span&gt; &lt;/a&gt; &lt;/div&gt; &lt;/body &gt; &lt;/html &gt;",emailTo);

      【讨论】:

      • 这绝对是一种方法。字符串插值和格式化字符串基本上完成了同样的事情。
      猜你喜欢
      • 2017-11-30
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2019-04-03
      • 2015-12-11
      • 2019-07-25
      • 1970-01-01
      相关资源
      最近更新 更多