PHP mail()在特定文件中不起作用

时间:2019-04-08 14:09:28

标签: php stripe-payments

我有一个页面负责购物,我想在成功时向管理员发送电子邮件。因此,当条件为真(收费成功)时,我将调用函数mail()。在下面的代码中,查看是否在($ subscription){}下(是,此条件正常运行)。但是,它永远不会起作用。如果我将mail函数单独放在一个单独的文件中,则可以正常工作。它返回true,我收到电子邮件。它只是无法从我正在处理的文件中工作。很神秘!我已经遍历了很多次,看不到任何与mail()函数冲突的东西。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
//set_error_handler("var_dump");
session_start();

if ( ( isset($_SESSION['signup_token']) ) AND ( $_POST['token'] == 
$_SESSION['signup_token'] ) ) {
// Start Charging

include_once 'classes/index.php';
\Stripe\Stripe::setApiKey("XXXXXXXXXXXXXXXXXXXXX");

// Set the stripe plan
switch ( $_POST['plan'] ) {
    case 'Basic Hosting':
        $plan = 'plan_xxxxxxxxxxxxxxxxxx';
        break;
    case 'Business Hosting':
        $plan = 'plan_xxxxxxxxxxxxxxxxxxxx';
        break;
    case 'Pro Hosting':
        $plan = 'plan_xxxxxxxxxxxxxxxxxxxxxx';
        break;
    case "Discount Hosting":
        $plan = 'something';
        break;
    case 'New Website';
        $plan = 'New Website';
        break;
}

$pin = mt_rand('100000', '999999');

try {

    // Create the Stripe customer
    $customer = \Stripe\Customer::create([
        'email' => $_POST['email'],
        //'description' => $_POST['first_name'] . ' ' . $_POST['last_name'],
        'metadata' => array(
            'Purchased from' => 'example.com',
            'Name' => $_POST['name'],
            'Support PIN' => $pin,
        ),
        'source'  => $_POST['stripeToken'],
    ]);

    // Subscribe the customer to the selected plan
    $subscription = \Stripe\Subscription::create([
        'customer' => $customer->id,
        'items' => [['plan' => $plan]],
    ]);

    if ( $subscription ) {
        $to = "something@gmail.com";
        $subject = "New subscription to";
        $txt = "<strong>Customer Name:</strong> <br />";
        $txt .= "<strong>Customer Email:</strong> <br />";
        $txt .= "<strong>Support PIN:</strong> ";
        $headers = "From: store@example.com" . "\r\n";
        $headers .= "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        if (mail( $to, $subject, $txt, $headers ) ) {
            echo "<p>Email to admin sent</p>";
        };
        include 'purchase_success.php';
    }

    if ( $plan == 'New Website') {
        include 'includes/new_website_email.php';
        //include 'includes/new_subscription_email.php';
    }

} catch (Exception $e) {

    error_log("unable to sign up customer:" . $_POST['stripeEmail'].
    ", error:" . $e->getMessage());
    $error = $e->getMessage();

}

// End Charging
} else {

    echo "Why are you here?";

}

1 个答案:

答案 0 :(得分:0)

我认为这段代码没有任何问题。我将我的应用程序放在其他服务器上,并且运行良好。由于这是在开发环境中,因此我将仅使用其他服务器。也许这可以作为某人的榜样。如果其他一切都因mail()失败,则可能是服务器!