My question is short, I have a golang application from which I am trying to send an email using net/smtp.sendmail:
This is the command which I am going to use
smtp.SendMail(server, auth, **from**, to, msg)
No usually people pass the sender's email address to from. Is there anyway I can pass a sender's name as well?
So the recipient will get an email from:
Foo Bar <foo#bar.com>
instead of an email from
<foo#bar.com>
I checked the official documentation and dozens of examples online but could not find anything.
Thank you.
You could add it to the msg for example, extending the example from the official docs you could use:
msg := []byte("From: the name <sender#example.org>\r\n" +
"To: recipient#example.net\r\n" +
"Subject: discount Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n")
Notice the: From: the name <sender#example.org>
The msg parameter should be an RFC 822-style email with headers first, a blank line, and then the message body. The lines of msg should be CRLF terminated. The msg headers should usually include fields such as "From", "To", "Subject", and "Cc".
Related
I am using mailgun Templates with Go to send emails. However when the text in my template fields contain \n no newline is added. My basic code looks like the following:
message := m.mg.NewMessage(sender, subject, body, recipient)
message.SetTemplate("my-template")
_ = message.AddTemplateVariable("myText", event.Text)
// ... Code for sending
I saw this answer about sending mail with newlines, however when I implement it like the following:
_ = message.AddTemplateVariable("myText", strings.ReplaceAll(event.Text, "\\n", "<br />"))
The email contains <br /> text in the body but no newlines.
I also tried adding the following field in the css of my template: white-space: pre-wrap; but still there was no break. Thanks for any help with adding a newline to a Mailgun Template with Golang.
Try to use triple-stash in your template instead of double-stash
{{var1}} -> {{{var1}}}
I am using Mandrill API(ruby) for sending mail. In 'rcpt' option I can give multiple email address.
Is there a way to change Subject according to email address?
Ex..
"to"=>
[{"type"=>"to",
"email"=>"user1#email.com",
"name"=>"User1 Name"},
{"type"=>"to",
"email"=>"user2#email.com",
"name"=>"User2 Name"}],
"metadata" => {
},
I want Subject like this
"Hi, #{username} you have a new mail"
Similarly is it possible to make dynamic email body according to email-address?
This seemed to have worked for me
I am a front-end dev and I was tasked to set a custom subject in mandrill.
So what I found was, in order to put dynamic content in the body of the email, I had to use double curly braces {{ client_name }} in the code.
This was already linked to variables that have been created by backend...
The task was to make the subject
the same as the client name with an added message like this...
"Hi John, hope you are well"
So I found that when logging in to Mandrill and going to your template,
You have the option to set your subject there...
Assuming your backend variables have been set up, you can actually use double curly braces in Mandrill where you set the subject
So my final result in Mandrill itself, looked like this
"Hi {{ client_name }}, hope you are well."
This seemed to have worked for me, however I did not set up all the variables in the backend, but this might help some of you at least.
It is not possible (or I didn't find the way) to send different subjects in one call
Only one subject can be set according to the documentation and my tests
https://mandrillapp.com/api/docs/messages.ruby.html#method=send-template
For dynamic content you can set variables in your template for example |TITLE|
and replace it in your code using merge_vars
merge_vars: [
{
rcpt: #user1.email,
vars: [
{name: "TITLE", content:"#{#user2.full_name} sent you a new message"}
]
},
{
rcpt: #user2.email,
vars: [
{name: "TITLE", content:"#{#user1.full_name} well received your message"}
]
}
]
The only solution for me is to make to calls using the same template but different parameters
I'm trying to make a regex that removes me in my text email: toto#toto.com.
example: I request information on your project email: toto#free.fr
So I did this that captures me "email: toto#toto.com"
message ="I request information on your project email. toto#free.fr"
message.gsub!("/(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i")
it returns me nothing, and I wish there was just in the message text.
thanks
Try this. This should work for both uppercase, lowercase and emails appear in the middle of the string.
email = /[A-Za-z]{5}:\s[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/
s = "I request information on your project email: toto#free.fr"
s.match(email).pre_match #=> "I request information on your project "
s2 = "This email: blah#bLAH.com is in the middle"
s2.match(email).pre_match #=> "This "
s2.match(email).post_match #=> " is in the middle"
But there are more cases not covered e.g. email: followed by many spaces
Your code has several problems:
You are looking for "email: ...", but you message has "email. ...".
You use gsub!, with one parameter, which is not the classic use case, and returns an Enumerator. The classic use case expects a second parameter, which indicates to what you want to substitute the found matches:
Performs the substitutions of String#gsub in place, returning str, or
nil if no substitutions were performed. If no block and no replacement
is given, an enumerator is returned instead.
You pass a string to the gsub! - "/(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i", which is different than sending a regex. To pass a regex, you need to drop the quotes around it: /(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i
So a fix to your code would look like this:
message ="I request information on your project email: toto#free.fr"
message.gsub!(/(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i, '')
# => "I request information on your project "
Also note I changed your code to use gsub instead of gsub!, since gsub! changes the underlying string, instead of creating a new one, and unless you have a good reason to do that, it is not encouraged to mutate the input arguments...
If you want to remove the email from the text use String#sub
message = "I request information on your project email. toto#free.fr"
message.sub!(/[A-Za-z]{5}:\s[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/, '')
# => "I request information on your project "
I have this command to check for valid email address. I just found out that when I try to add this to our email server (all email requests off this form are local email addresses), the email server does not allow a numeric character to start the email address/username. I have read through all the documentation for the command preg_match and cannot find how to make it fail if it starts with a numeric in the first character location. I am a newbie so any help would be appreciated.
if (!preg_match("(^[-\w\.]+#([-a-z0-9]+\.)+[a-z]{2,4}$)i", $in_email))
In php you can use following code with php filter_var function which return a boolean after filtering the variable with a specified filter condition.
if(filter_var($email,FILTER_VALIDATE_EMAIL))
{
//valid email
}
else{
//INVALID EMAIL
}
The function filter_var will return true if email is in correct format otherwise false.
Try this one;
/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
And use as follows
$regex = '/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($regex, $email)) {
// Valid email
} else {
// Invalid email
}
If we have domains without dots this answers does not work. For this case I changed from:
/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
To:
/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#([a-z0-9-]{2,})+(\.[a-z0-9-]{2,})*$/
Update: The user #Toto saw correctly one problem with regex that can start with any chars instead off numeric. And example like: #-----.--.---.----#-- was validate. So I changed /^[^0-9] for /^[a-z] and now is correct:
/^[a-z][_a-z0-9-]+(\.[_a-z0-9-]+)*#([a-z0-9-]{2,})+(\.[a-z0-9-]{2,})*$/
And work for these use cases:
user#domain
aa#aa
aa#aa.aa
aa.aa#aa
I have a text box to enter description
If i submits that need to be send through ajax and store in db.
Problem:-
Example text in textbox:- "Hi all & solve my problem"
in the next page i am getting till "Hi all"
Remaining text is missing, If I pass through get or post method using ajax.
Give me the solution. How to get all the content I placed in text box along with "&"
You need to urlencode string with escape or encodeURIComponent functions.
Yes, I have faced same type issue and find out solution that we need to pass data as a key value pair,
I had passed data in Ajax like:
data : email=" + email + "&name='" + name
But right way for passing data in Ajax is :
data: {
email : email,
name : name
}