簡體   English   中英

修改Javascript / PHP sendEmail函數以使用Mailgun HTTP POST

[英]Modify Javascript/PHP sendEmail function to use Mailgun HTTP POST

我有一個sendEmail函數,它是我上線的較大PHP腳本的一部分,我需要對其進行修改以使用新的Mailgun帳戶。 我對PHP還是相當陌生,對於郵件服務器等則是新手,所以在過去的一周里這一直是一個挑戰。 Mailgun文檔提供了一個使用PHP通過HTTP POST發送示例 (單擊頂部的PHP按鈕):

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;

}

我現有的sendEmail函數如下所示:

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);

}

我知道我不想在函數中指定目標,主題,文本等,因為它是從其他現有區域中繪制它們,所以我嘗試在函數的某一點添加類似的內容(對不起,我沒有如何一切看起來都放在一起,因為它太混亂了,以至於我從頭開始:

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

我還添加了$ch = curl_init(); 就在sendEmail函數以及所有curl_setopt行中。 除此之外,我迷路了,而且您可能會猜到,什么也沒發生。

有人可以告訴我如何將兩者結合起來,為什么或將我指向與此相似的地方嗎?

在此先感謝您的幫助!

據我對您的問題的了解,您已經將底部的代碼作為正在使用的腳本的一部分,但是實際的郵件發送需要按照頂部給出的示例Mailgun代碼進行嗎?

我將這兩個功能融合在一起。 尚未測試 可能有錯誤。

該函數具有相同的名稱,並以與以前完全相同的方式被調用,接受相同的參數。 您會看到我已經刪除了代碼的底部(實際上是發送了電子郵件),並將其替換為示例代碼。 在其中,我用函數在頂部接收的值替換了它提供的占位符文本: $to$subj$msg 我還刪除了所有$headers行,因為這些行專門針對mail()函數,我們不再使用它。 [編輯:現在也應該使用密件抄送選項。]

您仍然需要更改一些內容: my-api-key-hereExcited User <me@samples.mailgun.org> 此信息應該可以從您的Mailgun帳戶獲得。

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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM