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条回答 默认 最新

相关推荐 更多相似问题