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.

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

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。