So I am writing a game with Unity and I am sending players an email with all of their stats on how they played. The way I send the email is I format a string that has the html already written that replaces a specifier $_table to insert the data I need to send to a php script on my server that emails the user. All of this works.
I am having a problem though with sending links to images. Since I am doing this in a C# program I am having a hard time with <img src ="example.com/example.png"/> tags.
When it sends it sends it formated as <img src=\"example.com/example.png\"/> and doesn't show the image.
How can I send this string correctly in C#?
I have already tried " and using #"""" to format the string.
Any and all help is appreciated.
::Edit::
Updated for code.
So in my C# code I am reading from a txt file my base html. The problem is
when I am replacing with my table which has img tags in it.
I have a Email Function SendEmail which sends a form. In the form it has a
message parameter which is just a string. This form is sent to a php script
which sends to the email from the form.
//Unity Code
public static void SendEmail(string _email, string subject, string message)
{
if(CheckEmail(_email))//Checks if valid email form
{
WWWForm form = new WWWForm();
form.AddField("from", "example#example.com");
form.AddField("email", _email);
form.AddField("subject", subject);
form.AddField("message", message);
WWW web = new WWW("http://www.example.com/_email.php", form); //Unity class for various web functions
}
else
{
label.text = "Invalid Email";
}
}
The message I am sending is a table with img links which are referenced by string variables for readability.
public static string GetFormatedMetricTable(){
int time = 0;
string clockIcon = "<img src = "http://files.softicons.com/download/web-icons/web- grey-buttons-by-axialis-team/png/48x48/Clock.png"/>";
return "<table><tr><td>" + time + clockIcon + "</td></tr></table>";
}
The code above is reduced for this example but only content wise.
In my php code I am using a simple mail function to send the email from my smtp server.
The problem is in the string formatting of the double quote.
When viewed within the html code from the email I get /" which results in the images not showing.
Desired result http://subligaming.com/example.html
Note: Using Mono .Net
Here is my php code just incase.
<?php
$from = $_REQUEST['from'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
// $newMessage = str_replace(""", '$_"', $message);
// $newerMessage = str_replace("$_", "", $newMessage);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Subliminal Gaming <$from>\r\n";
mail($email, "$subject",
$message, $headers );
echo "Thank you for using our mail form";
?>
I am currently reading up on how the string literals work in php and that may be the problem but I do not know how to fix it. Any pointers?
Just try to use simple quotes in the html tag
<img src ='example.com/example.png'/>
Related
I'm using MailKit for received messages via Imap.
Now I need a search-logic for the software and I got a some troubles.
For ex, I using:
var query = SearchQuery.BodyContains("SomeTextfromEmail");
( or var query = SearchQuery.MessageContains("SomeTextfromEmail"); )
foreach (UniqueId uid in imapfolder.Search(query)){
//some logic
}
So, I can't get messages with text contains on it with Search filter.
I'm downloaded message ( message.WriteTo(string.Format(#"C:{0}.eml", uid)); )
and see the message content. Text of message in a base-64 format.
How do it correctly? I need decode message text from base64?
Problem are here:
var query = SearchQuery.BodyContains("Iphone").And(SearchQuery.All); //this construction find messages in folder
foreach (var uid in imapfolder.Search(query))
{
Console.WriteLine(message.From + " : " + uid.ToString());
}
var query = SearchQuery.BodyContains("Received from Iphone").And(SearchQuery.All); //this construction can't find message in folder
foreach (var uid in imapfolder.Search(query))
{
Console.WriteLine(message.From + " : " + uid.ToString());
}
Your question is a jumbled mess of confusion (your question starts out saying you are trying to figure out how to search and then you end with asking how to base64 decode the raw message that you saved to a file) so I have no idea exactly what you want to know.
If all you want is the decoded text body, you can do this:
var message = imapfolder.GetMessage (uid);
var text = message.TextBody;
This will be the decoded text string (either base64 decoded or quoted-printable decoded if it needs to be).
The MimeKit README and FAQ are both full of useful information on the basics of how to use them. I would highly recommend reading them as they may be helpful.
Update:
Based on the comment added to my answer, it sounds like what you want to do is this?
var matched = new UniqueIdSet ();
foreach (var uid in folder.Search (SearchQuery.BodyContains ("iPhone"))) {
var message = folder.GetMessage (uid);
var body = message.TextBody;
if (body != null && body.Contains ("Received from iPhone"))
matched.Add (uid);
}
I'm really new to the job as a web developer and I have a Problem that I cant solve alone, also I can't find any answers which fit my Problem.
So this is the Construct, I have a PHP Page with this Code:
<?php
$url = 'http://myserver.de/list.aspx';
$xml = new SimpleXMLElement($url);
$name = $xml->List->member->name;
?>
And I got this C#-Code from an aspx-project (list.aspx):
StringBuilder xmlBuilder = new StringBuilder();
xmlBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xmlBuilder.Append("<List>");
foreach (var nameandplz in fullname.Zip(plz, Tuple.Create))
{
xmlBuilder.Append("<member>");
xmlBuilder.Append("<name>" + nameandplz.Item1 + "</name>");
xmlBuilder.Append("<postcalcode>" + nameandplz.Item2 + "</postalcode>");
xmlBuilder.Append("</member>");
}
xmlBuilder.Append("</List>");
Context.Response.ContentType = "text/xml";
Context.Response.BinaryWrite(Encoding.UTF8.GetBytes(xmlBuilder.ToString()));
Context.Response.End();
So I just use the StringBuilder to build a String with XML.
fullname and plz are listed.
In my PHP-CIde i call the URL and the Problem is that the SimpleXMLElement doesnt write the XML-Code into the $xml.
I tried everything, i called the URL manually and the XML File is displayed correctly.
Do I use the wrong format to get the XML File per SimpleXMLElement?
To get the data from the URL you must set $data_is_url = true according to the PHP Documentation ( http://php.net/manual/fr/simplexmlelement.construct.php ); so XMLElement try to build an XML from the string given.
You can use the function libxml_get_errors to get XML errors.
So here a code that will get the content from the URL :
<?php
$url = 'http://myserver.de/list.aspx';
$xml = new SimpleXMLElement($url, 0, true);
$name = $xml->List->member->name;
Caution : you access List / Member / Name without testing if List Member or Name exists.
I need to parse html code into string, because i'm later using it as my body content of email message:
Is there a way to parse html code like this:
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Well done!</h4>
<p>You have successfully subscribed!</p>
<hr>
<p class="mb-0">We will be sending you newsletter on weekly basis.</p>
</div>
into string in a clean way, without doing it like this:
string body = #"<div class=""alert alert-success"" role=""alert"">" + "</div>"
so i could pass it to mail like this:
MailMessage mailMessage = new MailMessage();
mailMessage.Body = body;
instead of having a whole series of html code inside string there.
Is there maybe any other "way" to make "design" for email messages?
PS: i also do not want to load html from external file
I would like to use html code to have a decent design for subscribing to newsletter message on email.
Thank you!
/*
If they are static and don't need to be read from a file, as you've indicated, I would create a project->property->resource string for each message. You can paste the email body into a string variable and then just access it like this:
*/
string body = MyNamespace.Properties.Resources.emailBody1;
/* the string will be formatted with all the quotes and \r\n just as it was from what you pasted in. */
// you could even add some substitution to customize the email
body.Replace("{EmailRecipient}", strEmailRecipient);
Since you are using C#, you can use String interpolation. http://www.informit.com/articles/article.aspx?p=2422807
You can create a template object were you can pass all the necessary variables for your message.
e.g
class WelcomeEmail {
public WelcomeEmail(String message, String title) {
}
public override string ToString() {
}
}
i have some html content and i stored it in string variable and i want to print it directly.Is there any way in c# ?i have a javascript code which is not working
string emailbody="HTML i need to send";
Page.RegisterStartupScript("StatusMessage", "<SCRIPT LANGUAGE=\"JavaScript\">function printsheet(" + emailbody + "){var win = window.open('mywindow', 'left=0', 'top=0')var html = Zstring; win.document.open()win.document.write(html);win.print();}</Script>");
You have many ways to do that.
One way, make the string public
public string emailbody="HTML i need to send";
and on aspx page you render it as:
<%=emailbody%>
One other way is to use a Literal control and render it there. When you have UpdatePanel this is the only way.
Eg, you place the Literal on page, on the point you wish to render your text as:
<asp:Literal runat="server" id="txtRenderOnMe" />
and on code behind you type:
txtRenderOnMe.Text = "HTML i need to send";
Now, in your case the issue is that you render a string on the javascript code without the quotas as the other jesse point out on their comments.
string emailbody="HTML i need to send";
Page.RegisterStartupScript("StatusMessage", "<script language=\"JavaScript\">function printsheet('" + emailbody + "'){var win = window.open('mywindow', 'left=0', 'top=0')var html = Zstring; win.document.open()win.document.write(html);win.print();}</script>");
Got this error while call the function
static public void DisplayAJAXMessage(Control page, string msg)
{
string myScript = String.Format("alert('{0}');", msg);
ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}
Calling this function:
string sampledata = "Name :zzzzzzzzzzzzzzzz<br>Phone :00000000000000<br>Country :India";
string sample = sampledata.Replace("<br>", "\n");
MsgBox.DisplayAJAXMessage(this, sample);
I need to display Name,Phone and Country in next line.
Unterminated string constant means you've forgotten to close your string. You can't have an alert that runs over multiple lines. When the script is outputting to the browser, it's actually including the new lines.. not the "\n" like the javascript expects. That means, your alert call is going over multiple lines.. like this:
alert('Name :zzzzzzzzzzzzzzzz
Phone :00000000000000
Country :India');
..which won't work, and will produce the error you're seeing. Try using double backslash to escape the backslash:
string sample = sampledata.Replace("<br>", "\\n");
"\n" is a newline for C#, i.e. your js contains:
something('...blah foo
bar ...');
what you actually want is a newline in js:
something('...blah foo\nbar ...');
which you can do with:
string sample = sampledata.Replace("<br>", "\\n");
or:
string sample = sampledata.Replace("<br>", #"\n");
You need to escape/encode your string being consumed by JavaScript:
Escape Quote in C# for javascript consumption
Your Unterminated is not in C# is in Javascript generated code.