Как передать переменную C # в php url (кнопка html e-mail) - PullRequest
0 голосов
/ 28 июня 2018

Мое приложение отправляет HTML-сообщение, содержащее button, пользователю с помощью Gmail API. У URL кнопки должен быть встроенный адрес электронной почты пользователя (значение toAddress).

Мой вопрос заключается в том, как объединить переменную C # toAddress и передачу URL-адреса php, чтобы целевая ссылка была похожа на Пример:

<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`

Ответы [ 2 ]

0 голосов
/ 29 июня 2018

Вот решение

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);

Кнопка HTML с адресом электронной почты (переменная C # emailTo), прикрепленным к URL

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

0 голосов
/ 28 июня 2018

Вы можете использовать строковую интерполяцию для строкового литерала htmlBody, например, $@"<text_here". Поэтому, по сути, поместите $ перед @, а затем используйте {}, чтобы окружить переменную toAddress в href.

Как это:

$@"<whole_lot_of_html> href=""http://mypage.com/Email-list.php?email={toAddress} <rest_of_your_html>"

Я не знаю, как настроен ваш слой данных на вашем примере, поэтому я не могу дать вам подсказки о том, как сохранить toAddress в базе данных. Однако, если у вас все еще есть доступ к вашей переменной toAddress после отправки электронного письма, вы сможете обработать это здесь. При этом, возможно, вы захотите задать еще один вопрос для этого.

...