2016-07-03 24 views
2

This is an answer当使用Mailgun的类的问题。我正在寻找一个适用于在PHP中使用CURL的答案。当通过PHP和CURL使用Mailgun时,PHPMailer的AddStringAttachment的等价物是什么?


使用的PHPMailer的课,我可以通过以下方式发送多个附件:

$mail->AddStringAttachment($attachment1, $title1); 
$mail->AddStringAttachment($attachment2, $title2); 

,因为我不是取出由服务器上的文件,并在一个字符串我不是作曲,我需要指定每个机箱的标题。


现在,我想通过PHP和CURL使用Mailgun来完成该操作。到目前为止,我采用了以下技术用于发送邮件不带附件:

$api_key="[my api key]"; 
$domain ="[my domain]"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    "from" => "[sender]", 
    "to" => $to, 
    "subject" => $subject, 
    "html" => $content 
)); 

在此之后相同的阵列中的指定字段的约定,什么是使用PHP和卷曲与发送字符串的附件,并指定标题的等效Mailgun?

+0

为什么不使用Mailgun PHP api? –

+0

我可能不得不这样做,但由于我只使用了MG的一些功能,我不介意让一个库更少。 –

+1

如果您只想添加一个附件,那么只需将''attachment'=>'@/path/to/file.ext'添加到'CURLOPT_POSTFIELDS'数组中即可。但是他们的API指出,所有附件都使用表单名称'attachment',因此如果不覆盖以前的内容,就不能添加多个附件。在他们的API库中,使用来自GuzzleHttp的代码构造multipart/form-data请求,所以添加多个请求并不是问题。 – drew010

回答

1

我放弃了使用字符串的附件,而是创建了两个临时文件(基于先前由另一功能构成的内容)的临时目录(基于用户的唯一ID的目录名)的内部。 (感谢drew010开始我沿着正确的方向。)

我怀疑是下面的函数将是对别人有用,但也许各个部分将可帮助他人希望类似的功能。

function sendFormattedEmail ($coverNote, $attachment1, $title1, $attachment2, $title2) { 
    global $userID, $account; 

    if (!file_exists("temp_{$userID}")) { 
     mkdir("temp_{$userID}"); 
    } 

    file_put_contents("temp_{$userID}/{$title1}", $attachment1); 
    file_put_contents("temp_{$userID}/{$title2}", $attachment2); 

    $api_key="[api_key]"; 
    $domain ="[my_domain]"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
    curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages'); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
     "from" => "[my_return_address]", 
     "to" => $account, 
     "subject" => "your requested files", 
     "text" => $coverNote, 
     "attachment[1]" => new CurlFile("temp_{$userID}/{$title1}"), 
     "attachment[2]" => new CurlFile("temp_{$userID}/{$title2})" 
    )); 

    $response = curl_exec($ch); 
    $response = strtolower(str_replace("\n", "", trim($response))); 
    $result= json_decode($response, true); 
    $status = explode(".", $result["message"]); 

    if ($status[0] == "queued") { 
     echo json_encode(array ("result" => "success")); 
    } 
    else { 
     echo json_encode(array ("result" => "failure")); 
    } 

    curl_close($ch); 

    unlink ("temp_{$userID}/{$title1}"); 
    unlink ("temp_{$userID}/{$title2}"); 
    rmdir ("temp_{$userID}"); 
} 

如上所示,功能条以使得能够使用json_encode的从Mailgun的响应中的换行字符。修剪和小写转换只是我的偏好。

报告结果返回给调用函数后,它消除了两个临时文件,然后将临时目录。

相关问题