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>'
));
Related
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);
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()
));
This issue really driving me crazy this past hours. Why is the email that send with Mandrill not showing the right information about the Cc email address.
So for example, I want to send email to
receiver_to_a#mail.com
receiver_cc_a#mail.com
receiver_cc_b#mail.com
When I see the email header on receiver_cc_a#mail.com. its always show no cc, and that email is send "to" receiver_cc_a#mail.com, not as cc
Is anyone having the same problem? I already try sending email in PHP with PhpMailer and also try from the PHP API from Mandrill itself but no luck.
You need to set the option preserve_recipients to true.
$message = array(
'html' => '<p>Example HTML content</p>',
'text' => 'Example text content',
'subject' => 'example subject',
'from_email' => 'message.from_email#example.com',
'from_name' => 'Example Name',
'to' => array(
array(
'email' => 'recipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'to'
),
array(
'email' => 'ccrecipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'cc'
)
),
'headers' => array('Reply-To' => 'message.reply#example.com'),
'preserve_recipients' => true
);
I am trying to use send grid as an email platform for my php but the problem is I cannot send multiple emails as a CC header and also I cannot edit the FROM header to display a Name in front of the email address.
$from = "News Letter <new#gmail.com>" // cannot get the "News Letter" to Display
$cc = array("aaaaaa#gmail.com","bbbbb#gmail.com");// doesnt send to arrays
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => $to,
'cc' => $cc,
'subject' => $subject,
'html' => $body,
'from' => $headers
);
You need to use some additional fields in your $params array, like so:
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => $to,
'toname' => 'Newsletter Person',
'cc' => $cc,
'subject' => $subject,
'html' => $body,
'from' => $headers,
'fromname' => 'Newsletter'
);
Sendind directly via the Mail.Send endpoint does not allow for an array in the CC field, however if you use the SendGrid PHP library then you can use and array for CC
I need to send emails to multiple recipients. The number of recipients will vary depending on the data in the db.
Mandrill allows me to only add multiple recipients using an array.
Below is what works for multiple recipients
//email array that needs to be added to the 'to' key
$emailArray = ["example#example.com","test#test.com","hello#test.com","world#test.com"];
$mandrill = new Mandrill('xxxxxxxxxxxxxxx');
$message = array(
'subject' => 'Thanks for signing up',
'from_email' => 'support#test.com',
'to' => array(
array(
'email' => 'hello#test.com',
'name' => 'Hello Test'
),
array(
'email' => 'goodbye#test.com',
'name' => 'Goodbye Test',
)
),
'global_merge_vars' => array(
array(
'name' => 'FIRSTNAME',
'content' => 'JOHN'
),
array(
'name' => 'LASTNAME',
'content' => 'DOE')
));
//print_r($message);
$template_name = 'hello-world';
print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
Below is what i need to generate dynamically depending on the length of the emailArray
to' => array(
//the below array should be dynamically generated
array(
'email' => 'hello#test.com',
'name' => 'Hello Test'
),
array(
'email' => 'goodbye#test.com',
'name' => 'Goodbye Test',
)
)
Here is the array. Currently with fixed values.
//email array that needs to be added to the 'to' key
$emailArray = ["example#example.com","test#test.com","hello#test.com","world#test.com"];
My questions is How do generate the 'To' values based on the length of the email array?
is there a way i can implode the entire array script?
An effective way would be to use array_map I have added in some code that also takes an array of names as well (I couldn't see where you were getting the names from in your code).
$toAddresses = array('example#example.com','test#test.com','hello#test.com','world#test.com');
$names = array('Exmaple', 'Test', 'Hello', 'World');
$mandrillTo = array_map( function ($address, $name) {
return array(
'email' => $address,
'name' => $name
);
},
$toAddresses,
$names
);
This passes the 0th element from each array into the function and returns an array of two values, then passes the 1st, 2nd etc and returns each result in a new array ($mandrillTo)