• United States
IBM?
  • Site map
IBM?
  • Marketplace

  • Close
    Search
  • Sign in
    • Sign in
    • Register
  • IBM Navigation
IBM Developer Answers
  • Spaces
    • Blockchain
    • IBM Cloud platform
    • Internet of Things
    • Predictive Analytics
    • Watson
    • See all spaces
  • Tags
  • Users
  • Badges
  • FAQ
  • Help
Close

Name

Community

  • Learn
  • Develop
  • Connect

Discover IBM

  • ConnectMarketplace
  • Products
  • Services
  • Industries
  • Careers
  • Partners
  • Support
10.190.13.206

Refine your search by using the following advanced search options.

Criteria Usage
Questions with keyword1 or keyword2 keyword1 keyword2
Questions with a mandatory word, e.g. keyword2 keyword1 +keyword2
Questions excluding a word, e.g. keyword2 keyword1 -keyword2
Questions with keyword(s) and a specific tag keyword1 [tag1]
Questions with keyword(s) and either of two or more specific tags keyword1 [tag1] [tag2]
To search for all posts by a user or all posts with a specific tag, start typing and choose from the suggestion list. Do not use a plus or minus sign with a tag, e.g., +[tag1].
  • Ask a question

seeking another PHP / SendGrid example

2700077M89 gravatar image
Question by badryan  (41) | Aug 06, 2014 at 03:21 PM phpemailibmcloudsendgrid

I appreciate this has been asked before, but I'm new to both PHP and PaaS, and don't quite see how their example is going to help me.

I have the apache-php-buildpack up and running, I created a SendGrid service and used the graphical interface to bind the service to my application. However, how do I translate that simple PHP one-liner that just sends a form via email into something that works on BlueMix?

With sendmail, it used to be so easy...

if (mail($to, $subject, $body, $headers, "-f " . $_POST['email']))
{
echo 'Thank you.';
}

I understand how to get my SendGrid credentials from the VCAP_SERVICES, as described by in the aforementioned post, but where from here?

Any hints and especially code examples much appreciated!

People who like this

  0
Comment
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster

2 answers

  • Sort: 
120000K389 gravatar image
Accepted answer

Answer by Brian K. Martin (5161) | Aug 06, 2014 at 03:25 PM

SendGrid has a very nice set of docs including a live sample builder available here: https://sendgrid.com/docs/code_workshop.html

You can select PHP and it will generate a code template for you

Comment
Dave Cariello

People who like this

  1   Show 1   Share
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
2700077M89 gravatar image badryan (41)   Aug 06, 2014 at 03:28 PM 0
Share

You would not believe how much time I spent on their website today WITHOUT seeing that one...

2700077M89 gravatar image

Answer by badryan (41) | Aug 11, 2014 at 11:10 AM

For future reference, this is a piece of code that works for me: (the required autoload.php and SendGrid.php come from the user's side, and I used composer to install them).

 <?php
 require 'vendor/autoload.php';
 require 'vendor/sendgrid/sendgrid/lib/SendGrid.php';
 if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
 {
 $services = getenv("VCAP_SERVICES");
 $services_json = json_decode($services,true);
 if (isset($services_json["sendgrid"][0]["credentials"])) {
 $sendgrid_config = $services_json["sendgrid"][0]["credentials"];
 $user = $sendgrid_config["username"];
 $pass = $sendgrid_config["password"];
 $url = $sendgrid_config["hostname"];
 }
 $sendgrid = new SendGrid( $user, $pass );
 $mail = new SendGrid\Email();
 $mail->
 setFrom( $_POST['email'] )->
 addTo( "recipient@address.com" )->
 setSubject( "email from this website" )->
 setText( $_POST['email']."\nadded their address to the newsletter" );
 $response = $sendgrid->send( $mail );
 echo 'Thank you for subscribing.';
 }
 else
 {
 echo 'Your e-mail is not valid. Please try again.';
 }
 ?>
Comment
Andrew Whitfield
Sanjay.Joshi

People who like this

  2   Share
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster

Follow this question

No one has followed this question yet.

Answers

Answers & comments

Related questions

sendgrid service in php application. 2 Answers

Unable to send email using PHP in bluemix. 4 Answers

Alternatives for SendGrid 3 Answers

Bluemix.net's Wordpress boilerplate not sending emails 2 Answers

Using Java Liberty how can I send emails from my application 3 Answers

  • Contact
  • Privacy
  • IBM Developer Terms of use
  • Accessibility
  • Report Abuse
  • Cookie Preferences

Powered by AnswerHub

Authentication check. Please ignore.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • API Connect
  • Analytic Hybrid Cloud Core
  • Application Performance Management
  • Appsecdev
  • BPM
  • Blockchain
  • Business Transaction Intelligence
  • CAPI
  • CAPI SNAP
  • CICS
  • Cloud Analytics
  • Cloud Automation
  • Cloud Object Storage
  • Cloud marketplace
  • Collaboration
  • Content Services (ECM)
  • Continuous Testing
  • Courses
  • Customer Experience Analytics
  • DB2 LUW
  • DataPower
  • Decision Optimization
  • DevOps Services
  • Developers IBM MX
  • Digital Commerce
  • Digital Experience
  • Finance
  • Global Entrepreneur Program
  • Hadoop
  • Hybrid Cloud Core
  • IBM Cloud platform
  • IBM Design
  • IBM Forms Experience Builder
  • IBM Maximo Developer
  • IBM StoredIQ
  • IBM StoredIQ-Cartridges
  • IIDR
  • ITOA
  • InformationServer
  • Integration Bus
  • Internet of Things
  • Kenexa
  • Linux on Power
  • LinuxONE
  • MDM
  • Mainframe
  • Messaging
  • Node.js
  • ODM
  • Open
  • PowerAI
  • PowerVC
  • Predictive Analytics
  • Product Insights
  • PureData for Analytics
  • Push
  • QRadar App Development
  • Run Book Automation
  • Search Insights
  • Security Core
  • Storage
  • Storage Core
  • Streamsdev
  • Supply Chain Business Network
  • Supply Chain Insights
  • Swift
  • UBX Capture
  • Universal Behavior Exchange
  • UrbanCode
  • WASdev
  • WSRR
  • Watson
  • Watson Campaign Automation
  • Watson Content Hub
  • Watson Marketing Insights
  • dW Answers Help
  • dW Premium
  • developerWorks Sandbox
  • developerWorks Team
  • Watson Health
  • More
  • Tags
  • Questions
  • Users
  • Badges