如何从这个php脚本触发电子邮件?

时间:2016-05-08 12:10:50

标签: php forms

我是php的新手,我试图找出如何在付款成功后向自己发送电子邮件。我该怎么办呢?如果你看到脚本的一半,就会出现一个' if'定义成功的条带支付呼叫的声明 - 这是应该发送电子邮件的点。

我是否包含POST电子邮件请求 - 如下所示:

($_POST['email'])) {

$email_to = "me@example.com";
$email_subject = "Email subject line";

这是目前正常运行的php脚本 - 您将会看到' IF'声明在剧本的一半。

<?php
require('config.inc.php');
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email=$_POST['email'];
// Stores errors:
$errors = array();
// Need a payment token:
if (isset($_POST['stripeToken'])) {

$token = $_POST['stripeToken'];

// Check for a duplicate submission, just in case:
// Uses sessions, you could use a cookie instead.
if (isset($_SESSION['token']) && ($_SESSION['token'] == $token)) {
  $errors['token'] = 'You have apparently resubmitted the form.';
} else { // New submission.
  $_SESSION['token'] = $token;
}   

} else {
$errors['token'] = 'Your subscription cannot be processed because you   must have JavaScript enabled. Please try again.';
}

// Set the order amount somehow:
$amount = 2700; // $20, in cents

// Validate other form data!

// If no errors, process the order:
if (empty($errors)) {

// create the charge on Stripe's servers - this will charge the user's card
try {

  // Include the Stripe library:
  require_once('lib/Stripe.php');

  // set your secret key: remember to change this to your live secret key in production
  // see your keys here https://manage.stripe.com/account
  Stripe::setApiKey(STRIPE_PRIVATE_KEY);
  // Charge the order:
  $charge=Stripe_Customer::create(array(
  "card"=>$token,
  "email" => $email,
  "plan" =>"newsletter",
  ));
  // Check that it was paid:
  if (!empty($charge)) {
    //echo $amount;
    // Store the order in the database.
    // Send the email.
    // Celebrate!
    /*$cookie_name = "success_msg";
    $cookie_value = "Your Payment is successfully done";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");*/
    $_SESSION['success_msg']="Your subcription was successfull - thank you!<br><br>We will send you further details shortly on how to access your account.";
    echo "<script>window.location.href='index.php'</script>";
    exit(0);

  } else { // Charge was not paid!

    echo '<div class="alert alert-error"><h4>Payment System Error!</h4>Your payment could NOT be processed (i.e., you have not been charged) because the payment system rejected the transaction. You can try again or use another card.</div>';
  }

} catch (Stripe_CardError $e) {
    // Card was declined.
  $e_json = $e->getJsonBody();
  $err = $e_json['error'];
  $errors['stripe'] = $err['message'];
} catch (Stripe_ApiConnectionError $e) {
    // Network problem, perhaps try again.
} catch (Stripe_InvalidRequestError $e) {
    // You screwed up in your programming. Shouldn't happen!
} catch (Stripe_ApiError $e) {
    // Stripe's servers are down!
} catch (Stripe_CardError $e) {
    // Something else that's not the customer's fault.
}

} // A user form submission error occurred, handled below.

} // Form submission.
?>

asdasd

1 个答案:

答案 0 :(得分:0)

您可以创建一个发送电子邮件的函数:

function sendEmail () {
  $email_to = "me@example.com";
  $email_subject = "Email subject line";
  ... ETC ...
}

然后在付款成功的脚本中,在通知用户之前,您可以包含以下功能:

...
if (!empty($charge)) {
    ...

    // Include your function if you wrote this to an external script
    require_once '/path/to/php/sendEmail.function.php';
    // Then make use of it:
    sendEmail();

    /*$cookie_name = "success_msg";
    $cookie_value = "Your Payment is successfully done";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");*/
    $_SESSION['success_msg']="Your subcription was successfull - thank you!<br><br>We will send you further details shortly on how to access your account.";

    // Redirect the user to a new page
    echo "<script>window.location.href='index.php'</script>";

    // Then you can exit runtime
    exit(0);
}
// Charge was not paid!
else {
    echo '<div class="alert alert-error"><h4>Payment System Error!</h4>Your payment could NOT be processed (i.e., you have not been charged) because the payment system rejected the transaction. You can try again or use another card.</div>';
}
...

非常重要的是,电子邮件功能将发送电子邮件或正常继续,因为您不希望失败的电子邮件中断用户体验。你可以有一个后备,以便如果电子邮件没有发送它将它写入数据库或其他东西,但在此过程中不会退出并出错。

相关问题