mailgun邮件附件在不同的服务器上不起作用

时间:2017-01-02 06:26:13

标签: php curl mailgun

当我在我的开发服务器上运行代码时,我发送一个简单的邮件使用mailgun库,而不是一切看起来很好,而邮件正在附带发送。但是当我上传到我的cpanel后运行相同的代码时,邮件没有附件发送。任何帮助,将不胜感激。感谢

//Call the function to send an email
 $subject='subject for mail';
 $body='<b>Mail body</b>';
 $email='joshiprakash9090@gmail.com';
 //$attachment=array('@/home/unitedd5/public_html/beta/upload/VIBES.png','checkbox.htm');  
 $attachment=array('@test.png','@test1.png');  
 sendmail($email, $subject, $body,$attachment);

//MailGun function to send mail with attacment.
function sendmail($to, $subject, $body,$attachment) {
    $mg_api = 'key-mykey';
    $mg_version = 'api.mailgun.net/v2/';
    $mg_domain = "mydomain";       

    $mg_message_url = "https://" . $mg_version . $mg_domain . "/messages";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $mg_api);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, true);    
    curl_setopt($ch, CURLOPT_HEADER, false);

    $emailSetter=array('from' => 'Unitedcheerleading <' . 'sales@unitedcheerleading.com' . '>',
        'to' => $to,
        'subject' => $subject,
        'html' => $body,        
    );

    $i=0;
    foreach ($attachment as $attach)
    {
        $emailSetter['attachment['.$i.']']=$attach;
        $i++;

    }

    curl_setopt($ch, CURLOPT_URL, $mg_message_url);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$emailSetter);
    $result = curl_exec($ch);
    print_r($result);
    curl_close($ch);
    $res = json_decode($result, TRUE);
    print_r($res);
}
?>

1 个答案:

答案 0 :(得分:2)

我认为在发布问题道歉之前,我没有正确地搜索堆栈溢出的解决方案。

我刚刚更改了我的代码如下。 $attachment=array('@test.png','@test1.png');

$attachment=array('test.png','test1.png'); 

foreach ($attachment as $attach)
    {
        $emailSetter['attachment['.$i.']']=$attach;
        $i++;

    }

as

 foreach ($attachment as $attach)
    {
        $emailSetter['attachment['.$i.']']=curl_file_create($attach);
        $i++;

    }

它完成了这项工作,这个问题看似重复我从

找到了答案

Mailgun Sent mail With attachment

第三个答案完成了这项工作。