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.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能