Improving laravel mails to outlook/hotmail - laravel

I have a Laravel app sending emails via MailGun. For Gmail addresses it works right of the bat, but for Outlook/Hotmail they are marked as spam and have broken formatting. I have identified some issues but not sure how to best fix them.
I am aware this post is broad but the common thing is how to please outlook/hotmail.
Multipart messages
According to MailGun help:
It is best to send multi-part emails using both text and HTML or text only. Sending HTML the only email is not well received by ESPs.
Given my Mailables' build function below, how can I achieve that?
public function build()
{
return $this->from('name#mysite.com')
->subject('Welcome to mysite!')
->view('emails.welcome_new_user', [
'updateLink' => env('APP_URL') . '/subscribers/' . $this->subscriber->id . "/edit/" . $this->subscriber->token,
'deleteLink' => env('APP_URL') . '/subscribers/' . $this->subscriber->id . "/delete/" . $this->subscriber->token
]);
}
Headers
According to MailGun help:
Make sure you are using unsubscribe links and headers in your emails. Many ESPs (particularly Hotmail) pay attention to this and if they are not there, you are likely to get filtered.
In the view I have two unsubscribe links, one as a button in the body and one in the footer as plain text link so that should be fine. But how do I supply Headers and which headers? I assume it is done similar to this section in Laravel docs:
$message->getHeaders()->addTextHeader('Custom-Header', 'HeaderValue');
Other tips on specific Outlook/Hotmail requirements
Also most appreciated

Related

Go-Mail multiple recipients and attachment

I am trying to make an email service using go-mail library and made it working. However there are few challenges that i am unable to solve
I have the struct for form data-
type SendMailBody struct {
EmailTo []string `form:"sendTo"`
Subject string `form:"subject"`
Body string `form:"body"`
}
The form data that i am sending to the API is
subject: Notification
sendTo:["abc#gmail.com", "xyz#gmail.com"]
body:You have been notified
Challenges-
If I pass a single email in "sendTO", It is working fine. But after passing the slice of emails, it is unable to send emails to the recepients. How can i make it work?
If I pass the attachment through Form data, how can I attach it with the mail. In documentation, it is mentioned that we can do it like that "m.Attach("/tmp/image.jpg")" . But how should i do it if i pass the attachment via form data in API
Please guide me through that.
Some more details are needed to help here. In particular which go-mail are you using?
For 1.)
If you refer to https://github.com/wneessen/go-mail, using Msg.To() should work fine with multiple recipient. See the documentation at: https://pkg.go.dev/github.com/wneessen/go-mail#Msg.To
If you refer to https://github.com/go-mail/mail, there is Message.SetAddressHeader() (https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetAddressHeader) which does not support multiple recipient addresses. You would need to use Message.SetHeaders() for the "To"-header instead (https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetHeaders).
For 2.)
This totally depends on how you read the attachment data (and again also on the go-mail library you are using). https://github.com/wneessen/go-mail has different ways of attaching and embedding files (i. e. from a local file, from embedFS, from an io.Reader...)

laravel request return message keys translations

I have a Laravel Application and another APP making calls via API to Laravel. These 2 projects are separated.
Laravel and App have their own multilanguage system. They work independently but uses the same key translations.
So my idea was that all Laravel responses must be translations key, like: 'messages.success'.
With this response, the App can translate it.
All of these are working fine.
The problem appeared when I started working with Laravel Requests for validating forms.
In this case, the validation errors are automatically translated so the App receives the response translated with the default language of the Laravel application.
So what can I do?
I thought with 2 ideas but I don't know if they can work.
1: Passing the language into params. Don't know if it can work, how can I set the language before Laravel validates the Request?
2: Override the functionality of Requests to return messages without translate, so instead of "Felicidades" return "messages.success". I really like this approach. But how can I do it for all the rules? Overriding the messages function like this:
public function messages()
{
return [
'unique' => 'validation.unique'
];
}
For every rule works... but I feel bad.
Another approaches?
What is the best way to fix this problem?
I would suggest that you use this hacky solution in 2 lines of code. Go to /resources/lang/{code}/validation.php. You can see that it returns an array of messages by default. Modify it like so:
// Replace return in the first line
$ret = [
/* all the translations go here as normal */
];
// Add this as the last line. This will replace all values with their keys.
return array_combine(array_keys($ret), array_keys($ret));
After that you can use validation as per usual and you'll get validation message keys instead of messages. Cheers and hope this helps.

Encode E-Mail in Laravel 5.7

I have a page who i can see all user profiles.
So i have a foreach who show me all my profiles.
i want to display the user email
this is the way i do:
{{$user->email}}
but i want to encode the mailto because i want to prevent spam mails
so i want it like:
<a href="mailto:mail#example.com">
instead of
<mail#example.com
does anyone know how i can do this in laravel?
because there is a tool like this:http://www.wbwip.com/wbw/emailencoder.html
but here i can only encode one email
is there a way in laravel who i can say encode($user->email) and then i have the mail like on the top??
thank you so much!
i searched everywhere but i can not find anything
maybe what you need is htmlentities in php
htmlentities — Convert all applicable characters to HTML entities
read more here
You should try this:
{!! link_to("mailto:".$user->email, $user->email) !!}

decrypt random error with Laravel's built-in encryption facilities

I send invitation by mail to users with a encrypted email to know which user respond to invitation. Something like:
Hello, click on this link to start learning: https://example.org/start-learning?e=fwTreaN0WybffXdDfZZUNYB3FTFfZObCb7QFF5C4AFJvTjXabIPtRfcoXLkFYMUvD4FIZsmrDdEFN2OPKcTrAOSQLZfuKdfwcic1WtBxWSXWR1GEJD6we213A3BEPBpca0BxaaQ4GGMPFeRyXp6fPrG9WnTgWogwXUcnVtdwSEEdNHGuZsClTxR2AtD2JZN8VAEsRQKpFFShEDR2SET4KxGhLGM3M0FdDelrJtO8KXS2YRaddH==
The encrypted email is the long string above. I encode mail like this in a Mailable class:
$url = 'https://example.org/start-learning?e=' . encrypt($this->to[0]['address']);
Then this $url is added in a mail template like this:
<a href="{{$url}}>click me<a>
Then, when user clicks the link, it routes to a controller and the controller decrypts the payload:
decrypt($request->input('e'));
Then, it works for about 99% of people clicking link. But for about one percent, it does not work, I have an error decrypting. And I don't know why. This is the same Laravel application which encrypts and decrypts. Is there an reason for such a weird behavior?
Side note: I know decrypt always work and has not a random behavior (BTW I tested it on 10000 entries, it's OK). There must be something else with the mail process I don't understand.
I think you should use urlencode() when creating link so instead of:
$url = 'https://example.org/start-learning?e=' . encrypt($this->to[0]['address']);
you should use:
$url = 'https://example.org/start-learning?e=' . urlencode(encrypt($this->to[0]['address']));
to make sure it will be valid.

Is it possible to use nested merge tags in Mandrill?

Is it possible to use nested merge tags?
What we need is to be able to define the UNSUB tag depending on the values from other merge tags, like on this example:
<a
href="*|UNSUB:*|COMMUNITYURL|*/site/unsuscribe/user_id/*|USERID|*/hash/*|HASH|*/type/all|*">
Unsubscribe
</a>
Is this correct?
Should be done in a different way?
It's not currently possible to nest merge tags. In this case, your best bet would be to add your own List-Unsubscribe headers (using the headers parameter in the API) and pass either your own URL, or a URL constructed with your merge tags but not the UNSUB merge tag.
From the API:
What happens when the [unsubscribe] link is clicked?
If a recipient clicks the generated link, the message status is changed to Unsubscribed and > the recipient's address is added to the Rejection Blacklist in your
Mandrill account. The redirect URL will be appended to include two
querystring parameters that can be used for processing the unsubscribe
in your own system or database:
md_id - the _id, as provided in the API call and webhooks of the
specific message where the unsubscribe link was clicked
md_email - a URL-encoded version of the recipient's email address
So you don't have to worry about generating an unsubscribe url for every user. Just put a generic url and mandrill automatically will append to you the email of that user in the md_email variable.
The documentation suggest that you do this then:
Click here to unsubscribe.
And mandrill will append the correct user email and id:
http://mywebsite.com/unsub?md_email=fulanito#gmail.com&m_id=23321
Since mandrill doesn't support nested merge tags, here is the solution for the case like yours if you dont mind after user click unsub, it did't go to mandrill's black list, since the unsubsription are all handled by your app.
PER YOUR CASE:
Manually compose your unsubscribe link
e.g.
*|COMMUNITYURL|*/site/unsuscribe/user_id/*|USERID|*/hash/*|HASH|*/type/all/
At the end of your link, append the mandrill recipient email var.email=*|EMAIL|* e.g.
*|COMMUNITYURL|*/site/unsuscribe/user_id/*|USERID|*/hash/*|HASH|*/type/all/?email=*|EMAIL|*
Put the link to your mandrill template
Unsubscribe Me
reference: mandrill doc

Resources