Score:0

Can I send emails using the php mail function with SendGrid smtp relay?

kp flag

I have been trying to send emails from my app using the PHP mail function. I am currently hosted at DigitalOcean and the only way for me to send out emails is with services like SendGrid.

I have done everything according to their integration guide, but I still do not know how it works.

Can I send emails using the PHP mail() function?

Here is what I have done so far ...

  1. Update the CNAME records to match those of SendGrid.
  2. Verify that the records have been set.
  3. I have authenticated my domain.
  4. I have sent an email using Outlook.

I can send out emails with my Outlook but I cannot send out emails from my app. I really don't understand how this works?

Are there any records I need to update before I can send out emails? Do I have to use SendGrid API to send out emails or can I simply use PHP mail() function?

By the way, I have cPanel installed on my server and it handles my emails outgoing, and incoming

Score:1
no flag

The php mail() function on Linux and similar Unix like system uses the system sendmail command as the mail interface.

For that sendmail command to use Sendgrid (or another service) means that your system SMTP service needs to be configured to do so. Typical smtp servers are for example postfix, exim and the venerable sendmail itself. That may not be the best solution when your server hosts email domains.

Alternatively install ssmtp which is much easier to configure than conventional fully fledged smtp servers and adjust the sendmail_path in your php.ini configuration file to use ssmtp

kp flag
Thank you for helping. I am a PHP programmer but have no experience with SMTP or SSMTP. I am using cPanel as management software for my server. I think that they have exim. What records do I need for this to work?
Score:0
sn flag

It's always best to send email using PHP with cURL or the SMTPAPI Header, as per documentation provided in SendGrid Manual.

URL: SendGrid PHP Code Example

So it will always hit recipient's inbox.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.