douhuan4699 2016-12-19 10:41 采纳率: 100%
浏览 66

使用Mailgun API的简单联系表单

I am one of the dejected ex-Mandrill subscribers who has suddenly had to find another email platform. I chose Mailgun but am beggining to regret this decision as it all seems to be way above my head.

I was able to use very basic javascript/php and Zurb Foundation's validate to create a simple contact form on my website but I'm having trouble figuring out where to begin as far as incorporating Mailgun's API and I can't seem to find any documentation that doesn't require I open Terminal or start from scratch...

jQuery:

$('#contact-form').on('valid.fndtn.abide', function() {
    var name = $("input#name").val();
    var email = $("input#email").val();
    var message = $("textarea#message").val();
    //Begin Ajax Call
    $.ajax({
        type: "POST",
        url: "assets/php/mail.php",
        data: {
            'key': 'XXXXXXXX',
            'message': {
                'from_email': email,
                'from_name': name,
                'headers': {
                    'Reply-To': email
                },
                'subject': 'From My Site',
                'text': message,
                'to': [{
                    'email': 'me@me.com',
                    'name': 'me',
                    'type': 'to'
                }]
            }
        },
        success: function() {
            $('#contact-form').html("<div id='success' class='large-6 large-offset-6 medium-12 columns'></div>");
            $('#success').html("<h2 class='collapse'>Message recieved.</h2>")
                .append("<p class='collapse'>Thanks " + name + ", I will be in touch with you shortly.</p>")
                .hide()
                .fadeIn(1500);
        },
    }); //ajax call
    return false;
});

PHP:

<?php

 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];

 $msg = "
 Name: $name
 Email: $email
 Commments:
 $message
 ";

 $to = "me@me.com";
 $subject = "From My Site";
 $message = $msg;
 mail($to,$subject,$message,$headers);

 ?>

For anyone with any Mailgun experience, where do I start? I just want people to be able to send me an email from my site, and would rather not have to switch to another platform like SendGrid and pay $10/mo to receive the odd email here and there. Thanks for any insight.

  • 写回答

1条回答 默认 最新

  • douchensi8625 2016-12-19 12:54
    关注
        $config = array ();
        $config ['api_key'] = "key-#########################";
        $config ['api_url'] = "https://api.mailgun.net/v3/xyz.com/messages";
    
        $message = array ();
        $message ['from'] = "SITE NAME < feedback@xyz.com>";
        ;
        $message ['to'] = $to_email;
        $message ['h:Reply-To'] = "SITE NAME < feedback@xyz.com>";
        ;
        $message ['subject'] = 'Test Subject';
        $message ['html'] = '<b>Test Message</b>';
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $config ['api_url'] );
        curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
        curl_setopt ( $ch, CURLOPT_USERPWD, "api:{$config['api_key']}" );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 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_POST, true );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $message );
        $result = curl_exec ( $ch );
        curl_close ( $ch );
        echo $result;
    

    Before using the above code you need to create mailgun account & generate api-key for you domain.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!