首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当通过PHP和CURL使用邮件时,PHPMailer的AddStringAttachment相当于什么?

当通过PHP和CURL使用邮件时,PHPMailer的AddStringAttachment相当于什么?
EN

Stack Overflow用户
提问于 2016-07-03 06:10:36
回答 1查看 338关注 0票数 2

This is an answer在使用Mailgun的类时回答了这个问题。我正在寻找一个适用于在PHP中使用CURL的答案。

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

代码语言:javascript
运行
复制
$mail->AddStringAttachment($attachment1, $title1);
$mail->AddStringAttachment($attachment2, $title2);

因为我不是从服务器获取一个文件,而是用一个字符串进行组合,所以我需要为每个附件指定标题。

现在,我想通过PHP和CURL使用Mailgun来完成这个任务。到目前为止,我正在使用以下技术发送没有附件的邮件:

代码语言:javascript
运行
复制
$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的CURL来指定标题意味着什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-04 20:51:49

我放弃了使用字符串附件,而是在临时目录中创建了两个临时文件(基于先前由另一个函数组成的内容)(目录名基于用户的唯一ID)。(感谢drew010让我走上正确的道路。)

我怀疑以下功能对其他人是否有用,但也许不同的部分将有助于其他希望类似功能的人。

代码语言:javascript
运行
复制
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}");
}

如上面所示,函数从Mailgun的响应中剥离换行符,以便能够使用json_encode。装饰和小写转换只是我的首选。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38167223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档