Laravel with Mailgun timed messages - php

I have found a way to delay sending e-mail through Mailgun API. I am wondering if external HTML can be used somehow to include in the mail?
Now I am doing it like this:
$mgClient->sendMessage($domain, array(
'from' => 'XY<webmaster#xy.com>',
'to' => 'XY<xy#xy.com>',
'subject' => trans('content.subject_confirm_event_registration'),
'html' => '<myHtmlCode />',
'o:deliverytime' => Carbon::now()->hours(2)->toRfc2822String()
));
But the problem is when I try anything complex, which has like 100 lines of code, it doesn't look well, and I would like a solution where I can put external file in it so that it looks like this:
$mgClient->sendMessage($domain, array(
'from' => 'XY<webmaster#xy.com>',
'to' => 'XY<xy#xy.com>',
'subject' => trans('content.subject_confirm_event_registration'),
'html' => file.blade.php
'o:deliverytime' => Carbon::now()->hours(2)->toRfc2822String()
));

You can manually render a view by using view()->render().
Illuminate\View\View::render() returns the string contents of the view. It is also used inside the class' __toString() method which allows you to echo a View object.
$mgClient->sendMessage($domain, array(
'from' => 'XY<webmaster#xy.com>',
'to' => 'XY<xy#xy.com>',
'subject' => trans('content.subject_confirm_event_registration'),
'html' => view('some.template', $data)->render()
'o:deliverytime' => Carbon::now()->hours(2)->toRfc2822String()
));

Related

Mailgun how to make Cc and Bcc optional php

I've been using mailgun for a while and I've always wondered how to make a cc/bcc optional (so if the user wants to Cc themselves it would populate and if not it wouldn't populate).
$result = $mgClient->sendMessage($domain, array(
'from' => 'postmaster#example.com',
'to' =>'to#example.com',
'cc' => 'MAKE THIS OPTIONAL',
'h:Reply-To' => 'reply#example.com',
'subject' => 'Subject,
'html' => '<!doctype html>....'
Much more simplest way is next.
If you have filled 'cc' or 'bcc', for example, in $_POST or $_GET:
$default_array = [
'from' => 'postmaster#example.com',
'to' =>'to#example.com',
'h:Reply-To' => 'reply#example.com',
'subject' => 'Subject,
'html' => '<!doctype html>....
];
if (!empty($_POST['cc']) && isset($_POST['cc'])) $default_array['cc'] = $_POST['cc'];
// the same with 'bcc' and others
$result = $mgClient->sendMessage($domain, $default_array);

Mailgun not Sending emails when variable is used as value PHP

Whenever the "to" value is hardcoded, the email is sent. However, when the string value is replaced with a string variable the email does not get sent.
$result = $mgClient->sendMessage($domain, array(
'from' => 'Eemayl Ok <mailgun#domain.net>',
'to' => $customerEmail,
'to' => Tools::safeOutput($customerEmail),
'to' => (string)$customerEmail,
'to' => $customer->email,
'to' => Tools::safeOutput($customer->email),
'to' => 'hardcoded_email#gmail.com',
'subject' => 'We Hope You get this Email!',
'text' => '',
'html' => '<html>Contact Us Ok??</a></html>'
));
The several "to"s are my attempt at variations of expressing the variable value.
Array does not accept duplicate keys. It only chooses the last value if key duplicated. According to mailgun API, You should use commas to separate multiple recipients.
$recipients = array('client1#gmail.com', 'client2#gmail.com');
$result = $mgClient->sendMessage($domain, array(
'from' => 'Eemayl Ok <mailgun#domain.net>',
'to' => implode(',', $recipients),
'subject' => 'We Hope You get this Email!',
'text' => '',
'html' => '<html>Contact Us Ok??</a></html>'
));

Mandrill's attachment without extension on Hotmail/Outlook.com

I guess there is a issue with Mandrill attachments x Hotmail/Outlook.com.
I can send e-mail attachments with Mandrill using base64_encode(). I am using the PHP library.
If I send an e-mail to Gmail or any other provider, the attachment looks fine. Downloading it and opening normally.
But sending to a Hotmail/Outlook.com, the attachments came with no extension/file format. By downloading, it can only be opened by adding the extension manually (for example, .pdf).
The code:
$file_data = file_get_contents($_FILES["test_file"]["tmp_name"])
$file_type = $_FILES["test_file"]["type"]
$mandrill = new Mandrill('MANDRILL_API_KEY_HERE');
$message = array(
'html' => $html,
'subject' => 'Testing',
'from_email' => 'sender#test.com',
'from_name' => 'Sender',
'attachments' => array(
array(
'name' => 'Test PDF document',
'type' => $file_type,
'content' => base64_encode($file_data)
)
),
'to' => array(
array(
'email' => 'to#test.com',
'name' => 'John Doe',
'type' => 'to'
)
),
'headers' => array('Reply-To' => 'sender#test.com')
);
$result = $mandrill->messages->send($message);
Any ideas? Is it a Mandrill or Hotmail issue?
Thanks!
I have found the solution. Gmail and other email services recognizes the type attribute as above, but Hotmail only recognizes the file type by adding the extension to the attachment name (name attribute).
For example:
'name' => 'Test PDF document.pdf',
It look obvious, but Hotmail requires it. Sharing my simple knowledge, since I received no answers.

Sending email with Mandrill using PHP

I'm trying to send email using Mandrill and PHP and I can't get it to send.
I've downloaded the PHP API wrapper from here:https://packagist.org/packages/mandrill/mandrill
Mandrill.php is in my root and the Mandrill folder is in the same directory.
Here's my code:
<?php
require_once 'Mandrill.php';
$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
'subject' => 'Test message',
'from_email' => 'jwjody#yahoo.com',
'from_name' => 'Sender person',
'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
'to' => array(array('email' => 'jwjody#yahoo.com', 'name' => 'Recipient 1')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#domain.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Recipient 1 first name'),
array(
'name' => 'LASTNAME',
'content' => 'Last name')
))));
//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
echo ("hello");
?>
But it won't send. I'm not sure where the failure is. Is it something obvious I'm missing?
I see the issue now.
I see what's going on now!
I changed
$mandrill->messages->sendTemplate($template_name, $template_content, $message));
to
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
And it works!
Instead of calling the sendTemplate() function I should have used
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
Once I changed the function call the mail was sent.

Mandrill and PHP not sending transactional email out

So I've made a template called 'Basic' within Mandrill. I did a test send and it looks great. I put Mandrill into test mode and used the test API key in my code. I'm just trying to get the php to send out a test transactional email, but no email gets sent. Here is the printed response I get:
Array ( [0] => Array ( [email] => amiecrutchley02#gmail.com [status] => sent [_id] => 89bfab4c3938486eb9e36564f79a3e9f [reject_reason] => ) )
So I'm really not sure why I'm not receiving anything.
Here is my code:
<?php
require_once('includes/mandrill/Mandrill.php');
$mandrill = new Mandrill('my_api_key');
$message = array(
'subject' => 'Thank You For Your Purchase',
'from_email' => 'no-reply#acq.com',
'from_name' => 'ACQ',
'to' => array(array('email' => 'amiecrutchley02#gmail.com', 'name' => 'Amie')),
'merge_vars' => array(array(
'rcpt' => 'amiecrutchley02#gmail.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Amie'),
array(
'name' => 'LASTNAME',
'content' => 'Crutchley')
))));
$template_name = 'Basic';
$template_content = array(
array(
'name' => 'main',
'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, your profile has been updated!'),
array(
'name' => 'footer',
'content' => 'ACQ, Copyright 2014')
);
$response = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
print_r($response);
?>
a test API key is specifically designed to not send email. It's designed so you can mimic sending email, but doesn't actually send. You're also not charged for sending tests. Here's the Mandrill KB about what test mode is, and how it works: Does Mandrill have a test mode or sandbox?
Well, weird, but my test API was the problem. I tried the public API and boom! It works!

Categories