使用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个回答

    $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.

duanhuan5409
duanhuan5409 谢谢,我有两个......会尝试谢谢。
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐