dongquanlin1885 2017-02-16 23:30
浏览 97
已采纳

使用PHPMailer,Mailgun和HTTP API发送电子邮件

I Am able to send emails off this web server

I am on a shared web server using godaddy, and it's been awesome for me in a number of ways. I host two aspects of my website on here: a public wordpress site and an employee intranet. That's right, it has two aspects, with my custom built intranet being hosted next to a wordpress site. I CAN send emails.
The way I can send emails is via the Mailgun plugin for wordpress. As far as I can tell, this is the ONLY way my web server is sending emails right now. I have tried PhpMailer and php mail() vanilla, neither of those are doing squat.
I am using mailgun as my mail server, and it's working out pretty well so far. I use the wordpress plugin with ninja forms, and all my submissions to the forms get emailed to my email. The MX entries and txt files are all setup, so that is not my issue.

I was able to send emails with the program when it was hosted locally

I know the emails sent from the intranet are configured correctly, because it worked all day long when hosted locally. When it was hosted locally, I was sending through php mail() [I know, I know, hold your jeers] and it worked fine. When I brought the intranet online however, I started getting zero emails.
go daddy tells me "buzz off" essentially. They say no matter what smtp server I'm pointing to, they see no record of an email trying to be sent through them. No matter what host, no matter what domain, no matter what, they can not see the emails trying to be sent.
I have another mx entry, I believe because of our office 365 email server. This mx entry was brought to my attention because of godaddy. That mx entry points to outlook. From what I can tell, this is clogging my smtp abilities.
How does the mailgun plugin send emails then? Http. I know, I know, this sounds weird. It sounds weird to me too, I can find a whopping one site on the internet that even talks about it, and it's On our site. What it seems to be saying is that you can send the email info via http, which the mail server decodes and emails via smtp. I think this will be the easiest way to send email on my site, as it is what the Mailgun plugin does.

How do I know the Mailgun plugin uses http instead of smtp?

Mailgun Plugin configuration So, the plugin asks for my api key and domain name, nothing else. What I'm HOPING to do is to figure out how to do this with my php code. Phpmailer has no documentation on this that I can find, and Mailgun recommends using Phpmailer or the Wordpress plugin.

I cracked open the plugin, and am very confused

I would describe myself as Php attempting. This is way too much for me. The code I THINK is allowing the plugin to do it's thang is this

public function api_call($uri, $params = array(), $method = 'POST')
{
    $options = get_option('mailgun');
    $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
    $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $options['domain'];

    $time = time();
    $url = $this->api_endpoint.$uri;
    $headers = array(
        'Authorization' => 'Basic '.base64_encode("api:{$apiKey}"),
    );

    switch ($method) {
    case 'GET':
        $params['sess'] = '';
        $querystring = http_build_query($params);
        $url = $url.'?'.$querystring;
        $params = '';
        break;
    case 'POST':
    case 'PUT':
    case 'DELETE':
        $params['sess'] = '';
        $params['time'] = $time;
        $params['hash'] = sha1(date('U'));
        break;
    }

    // make the request
    $args = array(
        'method'    => $method,
        'body'      => $params,
        'headers'   => $headers,
        'sslverify' => true,
    );

    // make the remote request
    $result = wp_remote_request($url, $args);
    if (!is_wp_error($result)) {
        return $result['body'];
    } else {
        return $result->get_error_message();
    }
}

If this is not it, the two main files are in the comments, because of my reputation.

Alternative hopes

I understand this might be straight impossible. If it is, i am thinking there are two alternatives, there might be more. The two alternatives I see are sending my emails the way Ninja Form does to this plugin, but I have no clue as to how to go about that. The second is forcing phpmailer through the outlook mx entry, and sending it via smtp. How I would go about this is also beyond me. Any other suggestions or solutions are welcome.

  • 写回答

1条回答 默认 最新

  • dongxixian7803 2017-02-17 06:51
    关注

    You should search before posting. GoDaddy blocks outbound SMTP, but they provide a gateway that you can use instead. This blocks many sending scenarios (such as GoDaddy failing SPF checks). MX records have absolutely nothing to do with outbound mail.

    You can use PHPMailer with HTTP services like MailGun by using it to construct messages for you. Build your message as you would normally in PHPMailer, then call preSend() and getSentMIMEMessage() instead of send(). You will then have a complete message you can submit to the HTTP API.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同