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);
Related
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>'
));
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()
));
I am trying to send mail in symfony with attachment with the help of mailgun
I have referred this Question. And this Reference. But didn't helped me.
This is my function,
public function sendMail($to, $subject, $body, $attachment = null)
{
$mgClient = new Mailgun($this->container->getParameter('mailgun_key'));
$domain = $this->container->getParameter('mailgun_domain');
$content = [
'from' => $this->container->getParameter('mailgun_from'),
'to' => 'tester <' . $to . '>',
'subject' => $subject,
'text' => $body
];
if (!empty($attachment)) {
$result = $mgClient->sendMessage("$domain", $content);
} else {
$result = $mgClient->sendMessage("$domain", $content, [
'attachment[0]' => [$attachment]
]);
}
return $result;
}
In attachment, I'm passing the file path. i.e /home/mypc/Downloads/test.txt
But I'm receiving only blank mail. Attachment is not coming.
Where am I wrong? Please help.
The mailgun attachments documentation has the following information:
Attachments
You may attach a file from memory or by a file path.
From file path
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'Test file path attachments',
'text' => 'Test',
'attachment' => [
['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
]
]);
From memory
// Some how load the file to memory
$binaryFile = '[Binary data]';
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'Test memory attachments',
'text' => 'Test',
'attachment' => [
['fileContent'=>$binaryFile, 'filename'=>'test.jpg']
]
]);
Inline attachments
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'Test inline attachments',
'text' => 'Test',
'inline' => [
['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
]
]);
Please replace below code
$result = $mgClient->sendMessage("$domain", $content, [
'attachment[0]' => [$attachment]
]);
With
$result = $mgClient->sendMessage("$domain", $content, array(
'attachment' => array($attachment)
));
Eg.
$result = $mgClient->sendMessage("$domain", $content, array(
'attachment' => array('/home/mypc/Downloads/test.txt')
));
Referance: https://documentation.mailgun.com/user_manual.html#sending-via-api
Example below works for me.
Test
$mgClient = new Mailgun('key-abcfdfa5b40b6ea0ec0ccf9c33a90y65');
$domain = "sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org";
// SEND
$result = $mgClient->sendMessage(
$domain,
[
'from' => 'Sender <mailgun#sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org>',
'to' => 'Receiver <bentcoder#sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org>',
'subject' => 'Test subject',
'text' => 'Test message body',
],
[
'attachment' => [
'/var/www/html/local/test_app/logo.jpg',
'/var/www/html/local/test_app/ngrok.png'
]
]
);
// END
print_r($result);
stdClass Object
(
[http_response_body] => stdClass Object
(
[id] =>
[message] => Queued. Thank you.
)
[http_response_code] => 200
)
You might look into https://github.com/tehplague/swiftmailer-mailgun-bundle as well.
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