Sending email using the mailgun PHP APIs

I am trying to send an email with mailguns php api:

define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');

$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);

$mailgun->sendMessage(MAILGUN_DOMAIN, [
                'from'      => 'noreply@signstoptt.com',
                'to'        => $email,
                'subject'   => 'Sign Stop mailing list confirmation.',
                'html'      => "
                    Hello{$name},</br></br>
                    This is a test." 
            ]);

I even tried using array () instead of [].

In my php error log, the following error appears:

MissingRequiredParameters

This means that what I pass to the post function is incomplete or incorrect. after checking the post function in RestClient, I see that the function requires 2 arrays, not 1, so I tried to add a second array with message attachments, and it just got more errors, this time with guzzle (dependency for mailgun)

[26-Jan-2015 14:32:50 UTC] PHP Fatal error:  Uncaught exception 'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs!' in C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php:187
    Stack trace:
    #0 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php(116): Mailgun\Connection\RestClient->responseHandler(Object(Guzzle\Http\Message\Response))
    #1 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(106): Mailgun\Connection\RestClient->post('signstoptt.com/...', Array, Array)
    #2 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(53): Mailgun\Mailgun->post('signstoptt.com/...', Array, Array)
    #3 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\subscribe.php(26): Mailgun\Mailgun->sendMessage('signstoptt.com', Array)
    #4 in C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php on line 187

Someone else had this problem. I am launching a site on a Glassfish netbeans server setup. I also used the composer to install mailgun and its dependencies.

EDIT: Added more info.

init.php

<?php

    require_once 'vendor/autoload.php';

    define('MAILGUN_KEY', 'key-854743a7e');
    define('MAILGUN_PUBKEY', 'pubkey-b00e47d7');

    define('MAILGUN_DOMAIN', 'example.com');
    define('MAILGUN_LIST', 'customers@example.com');
    define('MAILGUN_SECRET','xjhbJH7');

    $mailgun = new Mailgun\Mailgun(MAILGUN_KEY);

    $mailgunValidate = new Mailgun\Mailgun(MAILGUN_PUBKEY);

    $mailgunOptIn = $mailgun->OptInHandler();

subscribe.php

<?php

require_once 'init.php';

if(isset($_POST['name'], $_POST['email']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];

    $validate = $mailgunValidate->get('address/validate', [
            'address' => $email
        ])->http_response_body;

    if($validate->is_valid)
        {
            $hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email);

            $result = $mailgun->sendMessage(MAILGUN_DOMAIN, [
                'from'      => 'noreply@example.com',
                'to'        => $email,
                'subject'   => 'example mailing list confirmation.',
                'html'      => "
                    Hello{$name},</br></br>
                    You submitted a request to join our mailing list, to confirm this subscription please click on the link provided below.</br></br>
                    http://localhost:8000/confirm.php?hash={$hash}" 
            ]);


            $mailgun->post('lists/' . MAILGUN_LIST . '/members', [
                'name'          => $name,
                'address'       => $email,
                'subscribed'    => 'no'
            ]);

            header('Location: ./');

        }
}

?>

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Subscribe | Mailing list</title>
    </head>
    <body>
        <div class="container">
            <form action="subscribe.php" method="post">
                <div class="field">
                    <label>
                        Name
                        <input type="text" name="name" autocomplete="off">
                    </label>
                </div>
                <div class="field">
                    <label>
                        Email
                        <input type="text" name="email" autocomplete="off">
                    </label>
                </div>
                <input type="submit" value="Subscribe" class="button">
            </form>
        </div>
    </body>
</html>
+4
1

text, , html .

define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');

$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);

$mailgun->sendMessage(MAILGUN_DOMAIN, [
                'from'      => 'noreply@signstoptt.com',
                'to'        => $email,
                'subject'   => 'Sign Stop mailing list confirmation.',
                'text'      => 'Hello ' . $name . ', this is a test.',
                'html'      => '
                    Hello ' . $name . ',</br></br>
                    This is a test.'
            ]);

; .

-1

All Articles