Relating email replies to original email (e-mail thread) with Sendgrid - laravel

I'm sending e-mails with Sendgrid. When the recipients reply to the mails, Sendgrid will parse the reply and POST to my webhook.
How can I relate the reply to the original message, so that I have a message thread?
I'm thinking that this may be possible using X-SMTPAPI headers and unique_args.
Update:
I think this is how I did it (but it was a while ago)
$arguments = ['unique_args' => [
'sentMessage_id' => $this->sentMessage->id,
'tenant_id' => \app('currentTenant')->id,
]];
$header = \json_encode($arguments);
$this->withSwiftMessage(function ($message) use ($header) {
$thread = $this->sentMessage->message->thread;
$headers = $message->getHeaders();
$headers->addTextHeader('X-SMTPAPI', $header);
$headers->addTextHeader('References', "<{$thread->first_mail_reference}>");

Related

Error 500: Mailgun's servers are currently unreachable

Using laravel I'm trying to send batch emails with the Mailgun api.
$recipients = new Collection();
$subscription_addresses = new Collection();
foreach($request['subscriptions'] as $subscription){
$recipients->put($subscription['email'], (object)array('id' => $subscription['id']));
$subscription_addresses->push($subscription['email']);
}
$recipient_chunks = $recipients->chunk(500);
$subscription_address_chunks = $subscription_addresses->chunk(500);
for($i = 0; $i < count($recipient_chunks); $i++){
$mgClient = Mailgun::create(env('MAILGUN_SECRET'), 'https://' . env('MAILGUN_ENDPOINT'));
$domain = env('MAILGUN_DOMAIN');
$params = array(
'from' => env('MAIL_FROM_NAME') . ' <' . env('MAIL_FROM_ADDRESS') . '>',
'to' => $subscription_address_chunks[$i]->toArray(),
'subject' => $request['newsletterForm']['subject'],
'html' => view('emails.newsletter', $request['newsletterForm'])->render(),
'recipient-variables' => json_encode($recipient_chunks[$i], JSON_FORCE_OBJECT)
);
# Make the call to the client.
$mgClient->messages()->send($domain, $params);
}
The chunking per 500 is because Mailgun queues only 500 mails per request according their docs.
Anyway, while testing with 3 recipients, everything works fine on localhost.
However, while sending on a live domain, I get an error Mailgun's servers are currently unreachable.
Anybody who knows this issue? Not much to find on stackoverflow or in the mailgun docs.
The MX records and mailgun configuration should be ok since I'm able to send single mails on the live site with
Mail::send('emails.confirmation', $request->all(), function($message) use ($request){
$message->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'));
$message->to($request['userInfo']['email'], $request['userInfo']['firstname'] .' '. $request['userInfo']['lastname'])->subject('Uw bestelling bij Hof te Berchemveld');
});
I was facing the same issue & found out that my domain & sender mail domain was different. Now after resolving that issue it worked fine for me.

Laravel email contact form 7

I'm developing a small script locally, which allows users to send an email from the post posted by another user. A simple contact form that then sends an email from user to user.
I have configured my .env with the corresponding Mailtrap sample data.
My problem is that every email sent is sent to Mailtrap and not to the user's email.
public function html_email(Request $request)
{
$request->validate([
'message_to' => 'required|string|max:255',
]);
$data = array('name' => Auth::user()->name);
Mail::send('mail', $data, function($message) {
$message
->to(request('email_to'))
->subject('Someone is interested in your ad!');
$message->from(Auth::user()->email, Auth::user()->name);
});
return back()
->with('success', __('app.email_successfully_sent'));
}
What am I doing wrong?
Ok, but setting a real email in .env when in a post a user has to contact another user, where is the email addressed? To the email set by default to .env? I want it to be sent to the email address of the user who posted the post.

How to add more parameter on controller laravel mail

I want to send email using Laravel and success with this code
Mail::to($email)->send(new SendMail($name))
Now I want to send more parameter like subject with this code, but didn't work.
No error message but email not being sent.
Mail::send(new SendMail($name function($message) use ($email){
$message->to($email)->subject('Verify your Account');
}));
Any suggestions? Thanks in advance!
You can use something like that
$template = 'view.email';
$paramets = ['data'=>$data];//Your to value to print in email
$email = 'to#email.com'// to email
$cc = 'cc#email.com' // or null
$toName = 'John Doe' // or null
$subject = 'Verify your Account';
Mail::send($template, $parameters, function($message) use($email,$cc,$toName,$subject) {
$message->from(env('sender'), env('nameSender'));
if($cc) $message->cc($cc);
$message->to($email, $toName)
->subject($subject);
});
I am using a generic method. So i can use this method to all emails that i need to send

How can i validate an list of emails in Laravel 5.4

I have a list of emails that get sent from the client side which are seperated by a comma. How can i validate all these emails to ensure that they are valid emails? e.g a user can capture emails in a text box like this one#one.com, two#two.com,three#three.com etc
I built a similar functionality at some point by using customer validation functions
Validator::extend("emails", function($attribute, $value, $parameters) {
$rules = ['email' => 'required|email'];
$emails = array_map('trim', explode(';', $value)); //$value
foreach ($emails as $email) {
$data = ['email' => $email];
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
return false;
}
}
return true;
});
If you are looking for how to verify the email to check whether they are valid and real email, you can simply do it by using SMTP server. There is a free github project for you: https://github.com/tintnaingwinn/email-checker
If you read through the readme, the package provides a custom validation rule for you.
EDIT: I apologize if this is off the topic since the question was about validating list of emails. But you can use this package if you want to test the email with SMTP server.
Hope this helps.
If you are trying to validate an array of emails then easiest way to validate is to use dot . syntax with a wildcard *. Here's the example:
$request->validate([
'emails.*' => ['email'],
...
]);

IronMq : mail doesn't send but the queue is working

I use IronMQ for to send my email since (laravel framework)
1. for signup email (it's oki with my SMTP and IronMQ)
2. Notification email (between users).
I'm a problem with the n°2 : iron receive the queue and It work but my SMTP don't receive the message for to send it.
I don't understand why because the config SMTP and IronMQ does not change between the two (Mail::queue)
PHP Code :
/****************************
Notification email (buged)
*****************************/
$emailTo = $product->user->email;
$emailFrom = $emailSend;
$data = Input::only('messageSend', 'name');
$data['product'] = $product;
Mail::queue('emails.products.iWant', $data, function($mail) use($emailTo, $emailFrom){
$mail
->from($emailFrom)
->to($emailTo)
->subject("Je suis intéressé(e) par votre produit");
});
/****************************
Signup email (it's oki)
*****************************/
$data = array('id' => $id, 'name' => $name, 'key' => $activation_code);
Mail::queue($view, $data, function($mail) use($email, $subject) {
$mail
->to($email)
->subject($subject);
});
With Mail::queue it's no possible to keep the message_id or it's Success or failed ?
Thanks

Resources