Sendgrid | Display sender name with email in “from” field

|
| By Webner

If we want that when we send email to our users using sendgrid api then the name of the sender (if we provide) should appear along with sender-email then follow the process given below :

To accomplish this, we just need to pass an extra parameter “fromname” to the curl post request as follow:

$params = array(
    'api_user' => $user,
    'api_key' => $pass,
    //'x-smtpapi' => json_encode($json_string),								'to'        => $to,
    'from' => ‘manjukk @mailcheck.com’,
    'fromname' => ‘Super Admin’,
    'subject' => $subject,
    'html' => $body,
);

// Generate curl request
$session = curl_init(‘your sendgrid api url’);

// Tell curl to use HTTP POST
curl_setopt($session, CURLOPT_POST, true);


// Tell curl that this is the body of the POST
curl_setopt($session, CURLOPT_POSTFIELDS, $params);

// obtain response
$response = curl_exec($session);

If we are using sendgrid’s libraries to send emails, then we can use following php syntax to set fromname:

$sendgrid = new SendGrid($username, $password);

$email = new SendGrid\ Email();
$email
    -
    > setFrom(‘manjukk @mailcheck.com’) -
    > setFromName(‘Super Admin’) -
    > setSmtpapiTos($recipients) -
    > setSubject('%subject%') -
    > setSubstitutions(array(
        '%subject%' => $subjects,
        '%body%' => $bodyContents
    )) -
    > setHtml('%body%');

$mailStatus = $sendgrid - > send($email);

Result : After using our own text in the fromname, this text will be shown to the recipient as the name of sender as shown in the following screenshot :
1

Leave a Reply

Your email address will not be published. Required fields are marked *