How to send E-mail Templates through Mailgun? - php

I am using Mailgun as my e-mail Service provider for sending E-mail to my Colleagues. I have created some attractive E-mail Templates. But, I don't know how to send it through Mailgun. Kindly guide me...

this is simple to do if you're using the PHP library from mailgun, available here: https://github.com/mailgun/mailgun-php
The below code will send an email to a colleague of yours, you can add as many to fields as you need!
Then simply edit the array 'html' :) and execute the script!
# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$domain = "YOURDomain.mailgun.org";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'You#yourdomain.com',
'to' => 'your_colleague#someone.com',
'subject' => 'Hello',
'html' => 'some html code <b> goes here </b> and works <span style=\"color:red\">fine</span>'
));

Or you can do a file read of the template and store it temporarily:
$fileloc = fopen("/path/to/email_template.php", 'r');
$fileread = fread($fileloc, filesize("/path/to/email_template.php"));
# Instantiate the client.
$mg = new Mailgun('<your_api_key>');
$domain = "domain.com";
# Make the call to the client.
$mg->sendMessage($domain, array('from' => 'noreply#domain.com',
'to' => 'recipient#domain.com',
'subject' => 'Subject',
'html' => $fileread));

Try this
$content = view('email.info-request',
['sender_name' => Input::get('sender_name'),
'sender_email' => Input::get('senderr_email'),
'sender_subject' => Input::get('sender_subject'),
'sender_message' => Input::get('sender_message')])->render();
$mg->messages()->send('mydomain.com', [
'from' => Input::get('sender_email'),
'to' => 'admin#mydomain.com',
'subject' => 'Information Request',
'html' => $content]);

Related

Vonage/Nexmo - PHP SDK - How to send MMS?

I would like to send MMS using PHP SDK. There is no proper documentation on how to send it.
Any help in this regard highly appreciated.
See on Voange Developer: Click here
This is a simple example how to use the api:
<?php
use Nexmo\Client;
$client = new Client(new Nexmo\Client\Credentials\Basic('API_KEY', 'API_SECRET'));
$sended_message = $client->message()->send([
'to' => 'PHONE_NUMBER_TO',
'from' => 'PHONE_NUMBER_FROM',
'type' => 'mms',
'media' => ['https://www.xxx.xxx/test.jpg'],
'message' => 'example mms'
]);
print_r($sended_message);

How to use AWS SDK for PHP to send email by SES with dedicated IP?

My aws/aws-sdk-php version is 2.7.27. The emails we sent are marked as spam. I found that my AWS account has got 8 dedicated IPs in Dedicated IPs page. But the sender IP of my email isn't any of the dedicated IPs, it's a shared IP of amazon SES. In developer document of SES, I found that they said I can make a Configuration Set to specify whick IP pool is used for sending. I add a param called 'ConfigurationSetName' as the doc said but it doesn't work, my emails are still sending through shared IPs. My code is like:
$sendingParams = array(
'Source' => $fromEmail,
'Destination' => array(
'ToAddresses' => is_array($email) ? $email : array($email),
//'CcAddresses' => is_array($ccEmail) ? $ccEmail : array($ccEmail),
//'BccAddresses' => is_array($bccEmail) ? $bccEmail : array($bccEmail),
),
'Message' => array(
// Subject is required
'Subject' => array(
// Data is required
'Data' => $subject,
'Charset' => 'utf-8',
),
// Body is required
'Body' => array(
'Html' => array(
// Data is required
'Data' => $content,
'Charset' => 'utf-8',
),
),
),
'ReplyToAddresses' => array($fromEmail),
'ReturnPath' => $returnPath,
'ConfigurationSetName' => 'system',
);
if (!empty($ccEmail)) {
$sendingParams['Destination']['CcAddresses'] = is_array($ccEmail) ? $ccEmail : array($ccEmail);
}
if (!empty($bccEmail)) {
$sendingParams['Destination']['BccAddresses'] = is_array($bccEmail) ? $bccEmail : array($bccEmail);
}
$result = $this->sdkClient->sendEmail($sendingParams);
What's the problem with my code? Should I change the version of the SDK?
I find the problem. My dedicated IPs are warming up, and I can't use them before they were totally warmed up.

How to send out multi emails using mailgun API in PHP

I have tried to run the following code for sending multiple emails using mailgun API in PHP. But no email is sent using the code. My code is :
require 'vendor/autoload.php';
use Mailgun\Mailgun;
$mgClient = new Mailgun("my-api-key");
$domain = "sandboxfa3e9009746840be831c6edc9e9f1ee9.mailgun.org";
$result = $mgClient->sendMessage("$domain",
array('from' => 'Mailgun Sandbox - Test
<postmaster#sandboxfa3e9009746840be831c6edc9e9f1ee9.mailgun.org>',
'to' => 'email_1#gmail.com, email_2#gmail.com',
'subject' => 'Mailgun bulk-msg test for KB',
'text' => 'Your mail do not support HTML',
'html' => 'html-portion here...',
'recipient-variables' => '{"email_1#gmail.com": {"first":"Name-1",
"id":1}, "email_2#gmail.com": {"first":"Name-2", "id": 2}}')
);
var_dump($result);
?>
#===============================================
Have i do any mistake in the code to send multi email ? If you have any solution, please help.
I found this from their API. (Not as answer maybe. Just a reference)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
$mg = Mailgun::create('key-example');
# $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);
Source
Aside from what Abdulla pointed out I see that you're writing "$domain" as the literal domain parameter. If you write it with quotation-marks, then it will be interpreted as a string, and not the variable itself.
Change the following and it should work:
$result = $mgClient->sendMessage("$domain",
[...]
to
$result = $mgClient->sendMessage($domain,
[...]

Override CakeEmail destination globally

Is there an easy way to "override" the destination of an email using CakeEmail?
I have a model method that send an email using this piece of code:
$Email = new CakeEmail();
$Email->config('default');
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($foo['User']['email'])
->send();
And this configuration:
class EmailConfig
{
public $default = array(
'host' => 'smtp.server.com',
'port' => 587,
'username' => 'user#domain.com',
'password' => 'pwdAsYouKnow',
'from' => array('noreply#domain.com' => 'Company'),
'transport' => 'Smtp'
);
}
As you can see, I send the email for a dynamically defined user's email.
My objective is when I'm developing locally on my machine, to force every call to ->send() to force a destination like developer#domain.com.
I can easily detect if I'm on development, but I don't know how to force in a "master" way to CakeEmail only send to a defined account overriding the one set on the call.
I already tried to set an
'to' => array('developer#domain.com' => 'Dev'),
inside $default but, no success.
I appreciate any help, thanks!
i'm assuming when you are on local machine you run in debug mode, so you can check if debug mode is on then use that to send to different email
if(Configure::read('debug')){
$emailTo = 'developer#domain.com'
}
else{
$emailTo = $foo['User']['email'];
}
then you just use variable for email address:
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($emailTo)
->send();

AWS SDK Guzzle error when sending email with SES

I am attempting to send mail using AWS SES sendEmail method, and I'm having trouble with an error. I have read this question: AWS SDK Guzzle error when trying to send a email with SES
I am dealing with a very similar issue. The original poster indicates that they have a solution, but did not post the solution.
My code:
$response = $this->sesClient->sendEmail('example#example.com',
array('ToAddresses' => array($to)),
array('Subject.Data' => array($subject), 'Body.Text.Data' => array($message)));
Guzzle code producing the error (from aws/Guzzle/Service/Client.php):
return $this->getCommand($method, isset($args[0]) ? $args[0] : array())->getResult();
Error produced:
Catchable fatal error: Argument 2 passed to Guzzle\Service\Client::getCommand() must be of the type array, string given
Looking at the Guzzle code, I can see that the call to getCommand will send a string if args[0] is set and is a string. If args[0] is NOT set then an empty array is sent.
What am I missing here please?
SOLUTION:
It turns out I was trying to use SDK1 data structures on the SDK2 code base. Thanks to Charlie Smith for helping me to understand what I was doing wrong.
For others (using AWS SDK for PHP 2) :
Create the client -
$this->sesClient = \Aws\Ses\SesClient::factory(array(
'key' =>AWS_ACCESS_KEY_ID,
'secret' => AWS_SECRET_KEY,
'region' => Region::US_EAST_1
));
Now, structure the email (don't forget that if you're using the sandbox you will need to verify any addresses you send to. This restriction doesn't apply if you have been granted production status) -
$from = "Example name <example#example.com>";
$to ="example#verified.domain.com";
$subject = "Testing AWS SES SendEmail()";
$response = $this->sesClient->getCommand('SendEmail', array(
'Source' => $from,
'Destination' => array(
'ToAddresses' => array($to)
),
'Message' => array(
'Subject' => array(
'Data' => $subject
),
'Body' => array(
'Text' => array(
'Data' => "Hello World!\n Testing AWS email sending."
),
'Html' => array(
'Data' => "<h1>Hello World!</h1><p>Testing AWS email sending</p>"
)
),
),
))->execute();
That should work now.
Here is the relevant section in the AWS SDK for PHP 2 documentation:
http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Ses.SesClient.html#_sendEmail

Categories