Giter Club home page Giter Club logo

mailjetswiftmailer's Introduction

MailjetSwiftMailer

Build Status Packagist Packagist GitHub license

A SwiftMailer transport implementation for Mailjet ([NEW] we now support send API v3.1 ) Mailjet Send API v3.1 Compatible Mailjet send API V3 and V3.1

If you found any problem, feel free to open an issue!

TODO

  • Adding URL tags
  • Sandbox Mode
  • Improve unit-tests (lots of code duplications)

Installation

Require the package with composer

composer require mailjet/mailjet-swiftmailer

Usage Example

$transport = new MailjetTransport($dispatchEvent, $apiKey, $apiSecret);
$transport->setClientOptions(['url' => "api.mailjet.com", 'version' => 'v3.1', 'call' => true]);


$transport->send($message);

(Send API v3 is selected by default)

Mailjet client custom configuration

You can pass an array in transport's constructor or use setClientOptions function:

$clientOptions = ['url' => "api.mailjet.com", 'version' => 'v3.1', 'call' => false];
$transport = new MailjetTransport($dispatchEvent, $apiKey, $apiSecret, $clientOptions);


or

$transport->setClientOptions(['url' => "api.mailjet.com", 'version' => 'v3.1', 'call' => true]);

Properties of $options:

  • url (Default: api.mailjet.com) : domain name of the API
  • version (Default: v3) : API version (only working for Mailjet API V3 +)
  • call (Default: true) : turns on(true) / off the call to the API
  • secured (Default: true) : turns on(true) / off the use of 'https'

Mailjet custom headers

It is possible to set specific Mailjet headers or custom user-defined headers, through SwiftMailer.

For example:

$headers = $message->getHeaders();

$headers->addTextHeader('X-MJ-TemplateID', $templateId);
$headers->addTextHeader('X-MJ-TemplateLanguage', true);
$vars = array("myFirstVar" => "foo", "mySecondVar" => "bar");
$headers->addTextHeader('X-MJ-Vars', json_encode($vars));

Note: You need to json_encodeyour array of variables in order to be compatible with SMTP transport.

Mailjet bulk sending

$emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', ...]

$messages = [];
foreach ($emails as $email) {
    $message = new \Swift_Message('Test Subject', '<p>Foo bar</p>', 'text/html');
    $message
        ->addTo($email)
        ->addFrom('[email protected]', 'From Name')
        ->addReplyTo('[email protected]', 'Reply To Name')
    ;

    array_push($messages, $message);
}
$transport = new MailjetTransport($dispatchEvent, $apiKey, $apiSecret);
$result = $transport->bulkSend($messages);

Note: does not work with Spool (SwiftMailer removed bulkSend from its API).

Integration in Symfony

If you want to use MailjetTransport in your Symfony project follow these small steps:

  1. composer require mailjet/mailjet-swiftmailer
  2. Into your services.yml, register MailjetTransport:
swiftmailer.mailer.transport.mailjet:
    class: Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport
    arguments:
        - "@swiftmailer.transport.eventdispatcher.mailjet"
        - "%mailjet.api_key%"
        - "%mailjet.secret_key%"

Note: We set mailjet.api_key and mailjet.secret_key into parameters.yml

  1. Finally, configure SwiftMailer in your config.yml:
# Swiftmailer Configuration
swiftmailer:
    transport: mailjet

Note: You can also inject your own Mailjet\Client:

mailjet.transactionnal.client:
    class: "%mailjet.client.class%"
    arguments:
        - "%mailjet.api_key%"
        - "%mailjet.secret_key%"
        - %mailjet.transactionnal.call%
        - %mailjet.transactionnal.options%

swiftmailer.transport.eventdispatcher.mailjet:
    class: Swift_Events_SimpleEventDispatcher

swiftmailer.mailer.transport.mailjet:
    class: Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport
    arguments:
        - "@swiftmailer.transport.eventdispatcher.mailjet"
        - "%mailjet.api_key%"
        - "%mailjet.secret_key%"
        - %mailjet.transactionnal.call%
        - %mailjet.transactionnal.options%
    calls:
        - method: setExternalMailjetClient
          arguments:
              - '@mailjet.transactionnal.client'

Mailjet references

Execute Tests

vendor/bin/phpunit -c .

Contributing

If you want to contribute to this project, look at over here

mailjetswiftmailer's People

Contributors

ksaveras avatar latanasov avatar mskochev avatar muldos avatar nightbr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mailjetswiftmailer's Issues

Sandbox Mode

Hi,

Is there a way to use Sandbox Mode? I tried by setting this in the headers

$headers = $message->getHeaders();
$headers->addTextHeader('X-MJ-SandboxMode', true);
// or
$headers->addTextHeader('SandboxMode', true);

but without success, the email is still sent.

Regards

Error when using format v3.1 and reply-To adress without a name

I tried to use the message format v3.1 because it allows a beter handling of cc & bcc addresses. However, when switching my code from v3 to v3.1, the mailjet API returns me an error:

{"ErrorIdentifier":"caac57f0-5f98-4d52-8c8b-f606caefd1ce","ErrorCode":"send-0018","StatusCode":400,"ErrorMessage":"Property value cannot be null.","ErrorRelatedTo":["ReplyTo.Name"]}

If I look at the code, it seems that MailjetSwiftMailer tests if the replyTo obtained from the message is an array or not, and does not test if the name is empty or not before setting it.

It also seems that swift always provides an array, even if the message contains only one address.

Needs attention on Packages on Packagist.org

Your package on packagist.org have (Legacy Auto-Update, Needs Attention)

If you have published packages from GitHub on http://packagist.org please logout/login with GitHub to let us upgrade the auto-update hooks and remove the deprecated GitHub service hooks. See “GitHub Hook” on https://packagist.org/about#how-to-update-packages for more details.

Note that things will keep working fine as-is until January 2019 so no rush doing this but do it when you get a chance to make sure your packages keep updating smoothly.

Source about how-to-do: https://twitter.com/packagist/status/1037035587453235201

Issue Mailjet

hello , i have an issue i get an error message when i load the page that sends my message : Uncaught exception 'Symfony\Component\DependencyInjection\Exception\InvalidArgumentException' with message 'There is no extension able to load the configuration for "swiftmailer.mailer.transport.mailjet"
thanks

CustomCampaign

Hi,

I would like to use mailjet "CustomCampaign" property to attach my mail to a campain.
https://dev.mailjet.com/guides/#grouping-into-a-campaign

I did not find any logic property of Swift_Mime_Message to link correctly CustomCampain into your getMailjetMessage function to push a pull request

Do you have any simple idea or do I need to build my own Swift_Transport ?

v3 Cc & Bcc handling misleading

Hello,

Using mailjet/mailjetBundle, I noticed that the only way Swift_Message are formatted is using Recipient property.

However, belong to mailjet v3 documentation, to, cc and bcc could be used.

The fact is, I could not send any CC message with default configuration and the only way I found to do so was to switch into v3.1.

I don't know if it is intended because there's probably no equivalent (I'm not sure) of Recipient in SwiftMailer but at least I thought it worths a little warning in documentation like :
"If you want to add CC or BCC to your emails, you should check on v3.1 as v3 will split them into several mails."

Is it alright or am I missing something there ?

upgrade 3 to 3.1

Is there anything else to adjust (I don't see any) in order to upgrade 3 to 3.1, I've try bulkSend but none of my email where sent with the version's option changed to 3.1. They do in 3.
I add the swiftmailer event in the bulkSend method of your MailjetTransport, they seem correct.
Any idee of what happens ? I don't have the log of the API response (which would help a lot but your Client call method is private and I couldn't override your Client Class)

using Symfony 3.4
adding header : X-Mailjet-TrackOpen

Sending simple mail

Hi,

I just discover this bundle so my question is not really related to the lib. If you can't answer it, I will just install it.

So I use SwiftMailer to send emails (ex : registration confirmation) from a website using the Mailjet SMTP. Mails are not sent.

The Symfony profiler says that 1 mail is spooled when I create a user account, but it is never sent.
php bin/console swiftmailer:spool:send returns 0 emails sent. I tried memory and file spools.

When using the php bin/console swiftmailer:email :send, mails are correctly sent (mails have the mailjet headers and they are in the mailjet sent mails interface).

When I'm trying the OVH SMTP, mails are correcly sent, so the problem seems to be a SwiftMailer misconfiguration.

app/config/config.yml

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host_prod%"
    port:      "%mailer_port_prod%"
    encryption:      "%mailer_encryption_prod%"
    username:  "%mailer_user_prod%"
    password:  "%mailer_password_prod%"
    spool:     { type: memory }

app/config/parameters.yml

    mailer_transport: smtp

    mailer_host_prod: in-v3.mailjet.com
    mailer_port_prod: 587
    mailer_encryption_prod: tls
    mailer_user_prod: api_key
    mailer_password_prod: api_secret

    mailer_host_ovh: ssl0.ovh.net
    mailer_port_ovh: 465
    mailer_encryption_ovh: ssl
    mailer_user_ovh: email
    mailer_password_ovh: password

PS : There is no example about Symfony nor this lib in this page, it could be useful

V3.1 - Not handling Swift_Image

Current version (v3.1) is not handling the Swift_Image instances it it is only handle the Swift_Attachment instances from the children. That is why it is not displaying embed images to the email.

image

Ambiguity with variables, documentation and Swiftmailer API

Hi,

TL;DR: Could you add a documentation on how to pass variables with Swiftmailer which is in compliance with Swiftmailer interfaces ?


From SMTP official documentation :

X-MJ-Vars - Global variables used for personalisation in a JSON format

My expectation is that I must provide a JSON encoded object with all my properties.

That expectation is confirmed by the fact that most documentations about headers use addTextHeader method (like here).
The prototype of that method is the following :

    /**
     * Add a new basic text header with $name and $value.
     *
     * @param string $name
     * @param string $value
     */
    public function addTextHeader($name, $value = null);

So nothing warn me that passing a JSON encoded data is wrong at that time.

After digging, I figured out that passing JSON data doesn't work here and I need to pass array data.

So the current way of passing data is in violation with Swiftmailer interfaces. That violation creates noises on static analysis tools. Also, It makes unclear how it works.

Could you add a documentation on how to pass variables with Swiftmailer which is in compliance with Swiftmailer interfaces ?

Swiftmailer 6 compatibility

Swiftmailer 6 has been released since may. It drops some supports and requires PHP7.
symfony/swiftmailer-bundle@3 requires swiftmailer/swiftmailer@6.

Is there any plan to add support for Swiftmailer 6 ?

MailjetTransport.php optimation

There are the following issues in the file MailjetTransport.php:

PHPDocs are wrong:

  • some @parameters are not declared
  • some @throws are not declared
  • some @returns contain not all possible return types
  • some @var use relative paths which can not be mapped by some IDE's e.g. (PHPStorm)

Logic:

  • There is some logic which is unnecessary in the function bulkSend

Variables:

  • $resultStatus is never used
  • $sendCount is double declared

Support for Swiftmailer 6.0

Swiftmailer 6.0 was released something like 6 months ago, and IRC, should not have too many changes impacting a simple Transport.

Any support planned for 6.0 ? :}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.