Question

I have a sendEmail function as part of a larger php script that I got online and I need to modify it to use my new Mailgun account. I am rather new to PHP and even newer to mail servers and the like so this has been a challenge over the last week. Mailgun documentation gives an example for sending via HTTP POST using PHP (click on the PHP button on the top):

function send_simple_message() {
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, 'api:my-api-key-here');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2');
  curl_setopt($ch, CURLOPT_POSTFIELDS, array('from' => 'Excited User <me@samples.mailgun.org>',
                                             'to' => 'obukhov.sergey.nickolayevich@yandex.ru',
                                             'subject' => 'Hello',
                                             'text' => 'Testing some Mailgun awesomness!'));

  $result = curl_exec($ch);
  curl_close($ch);

  return $result;

}

and my existing sendEmail function looks like:

public function sendEmail($to, $subj, $msg, $shortcodes = '', $bcc = false) {

    if ( !empty($shortcodes) && is_array($shortcodes) ) :

        foreach ($shortcodes as $code => $value)
            $msg = str_replace('{{'.$code.'}}', $value, $msg);

    endif;

    /* Multiple recepients? */
    if ( is_array( $to ) )
        $to = implode(', ', $to);

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: ' . address . "\r\n";

    /* BCC address. */
    if ( $bcc ) {
        $headers .= 'Bcc: ' . $to . "\r\n";
        $to = null;
    }

    $headers .= 'Reply-To: ' . address . "\r\n";
    $headers .= 'Return-Path: ' . address . "\r\n";

    /*
     * If running postfix, need a fifth parameter since Return-Path doesn't always work.
     */
    // $optionalParams = '-r' . address;
    $optionalParams = '';

    return mail($to, $subj, nl2br(html_entity_decode($msg)), $headers, $optionalParams);

}

I know that I don't want to specify to, subject, text, etc in my function as it is drawing those from other existing areas so I tried adding something like this at one point to the function (sorry I don't have how it all looked put together because it got so messed up that I scratched it an started over:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('from' => 'Webmaster <webmaster@mydomain.com>',
                                             'to' => $to,
                                             'subject' => $subj,
                                             'text' => $msg));

I also added $ch = curl_init(); just inside the sendEmail function as well as all the curl_setopt lines. Beyond that I was lost and as you can probably guess, nothing happened.

Can someone show me how to combine the two and why or point me somewhere that closely resembles this sort of thing??

Thanks in advance for any help!

Was it helpful?

Solution

From what I understand of your question, you've got the bottom code as part of a script you're using, but the actual mailing needs to be done along the lines of the sample Mailgun code given at the top?

I've mashed those two functions together. It's not tested. There may be errors.

The function has the same name, and gets called exactly the same way it was before, accepting the same parameters. You'll see that I've removed the bottom part of the code - which actually sent the email - and replaced it with the example code. Within that, I've replaced the placeholder text it provided with the values the function receives at the top: $to, $subj and $msg. I also removed all of the $headers lines as these refer specifically to the mail() function, which we are not using any more. [Edit: now should work with bcc option too.]

You will still need to change a few things: my-api-key-here and Excited User <me@samples.mailgun.org>. This information should be available from your Mailgun account.

public function sendEmail($to, $subj, $msg, $shortcodes = '', $bcc = false) {

  if ( !empty($shortcodes) && is_array($shortcodes) ) :

      foreach ($shortcodes as $code => $value)
          $msg = str_replace('{{'.$code.'}}', $value, $msg);

  endif;    

  /* Multiple recepients? */
  if ( is_array( $to ) )
      $to = implode(', ', $to);

  /* BCC address. */
  $bccrecip = '';
  if ( $bcc ) {
      $bccrecip = $to;
      $to = '';
  }

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, 'api:my-api-key-here');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2');
  curl_setopt($ch, CURLOPT_POSTFIELDS, array('from' => 'Excited User <me@samples.mailgun.org>',
                                             'to' => $to,
                                             'bcc' => $bccrecip,
                                             'subject' => $subj,
                                             'text' => $msg));

  $result = curl_exec($ch);
  curl_close($ch);

  return $result;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top