• Hi guys,

    I’m hoping someone can help me understand the relationship between this plugin and SendGrid’s own PHP Client Library (https://github.com/sendgrid/sendgrid-php). Looking at the plugin documentation at SendGrid’s website, it looks like there’s at least some cross-referencing happening, but I didn’t see all of the same library files in the plugin. Attempting to post a new campaign using an installed copy of the library while the plugin is activated results in an error (“Fatal error: Cannot declare class SendGrid\Email, because the name is already in use in /xxxxx/sendgrid-php/lib/helpers/mail/Mail.php on line 877”). Am I able to use any other library functions to access non email/contact endpoints with just the plugin activated?

    If it’s helpful, I’m ultimately trying to create and send a new campaign based on a WP_Query for the day’s curated posts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Anonymous User 12640152

    (@anonymized-12640152)

    Hello.

    You get this error because our plugin already comes with the official sendgrid-php library.

    Our plugin currently uses version v4.0.2 of the sendgrid-php library to send emails through SMTP or the SendGrid API.

    Thank you,
    SendGrid

    Thread Starter heyitsstacey

    (@heyitsstacey)

    That’s great! Is there a good way to reference the library? I think I might still be missing something.

    At the moment, I just have a test file called email-sendgrid.php that requires the wp-load.php file so that I can use WordPress functions. Previously, I also required the sendrid-php.php file, which resulted in the fatal error about SendGrid/Email already being in use. Removing that manually loaded sendgrid-php library, I get “Fatal error: Uncaught Error: Class ‘SendGrid’ not found in /email-sendgrid.php:188 Stack trace: #0 {main} thrown in /email-sendgrid.php on line 188”

    Anonymous User 12640152

    (@anonymized-12640152)

    Hello.

    The library code should be automatically loaded into WordPress existing pages. If you want to create a custom PHP page for your WordPress installation, there are a few tutorials and examples on the web.

    We tested it this way and it works:

    <?php
       	 require_once(dirname(__FILE__) . '/wp-config.php');
    
        	$wp->init();
        	$wp->parse_request();
        	$wp->query_posts();
        	$wp->register_globals();
        	$wp->send_headers();
    
    	$email = new SendGrid\Email();
    	$email
        		->setFrom('[email protected]')
        		->setHtml('<strong>Hello World!</strong>')
        		->addCategory('customCategory')
    	;
    
    	wp_mail('[email protected]', 'Subject goes here' . site_url(), 'Message goes here', $email);
    ?>

    We saved this file as test.php in the root directory of the WordPress installation and accessed it as https://ourwebsite.com/test.php

    You might encounter some issues depending on how your .htaccess file is configured for your website.

    We hope this helps! Let us know if it worked for you.

    Thank you,
    SendGrid

    Thread Starter heyitsstacey

    (@heyitsstacey)

    Thank you for the snippet! It hasn’t quite worked for me yet, but I’ll keep playing with the configuration and see what I can come up with.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘SendGrid WordPress Plugin + Official PHP Client Library’ is closed to new replies.