PHP Mailer发送邮件未含区号问题及格式修改需求
Let's tackle your two issues step by step:
1. Ensure the Area Code (Country Code) is Included in Emails
The most common reason the area code isn't showing up is that your form isn't submitting the full number (including country code) generated by the intlTelInput plugin. Here's how to fix that:
Step 1: Verify the Hidden Input in Your Form
The intlTelInput plugin creates a hidden input that stores the full E.164 formatted number (e.g., +15551234567). Make sure your initialization code enables this, and the hidden input exists in your form:
In your JavaScript (where you initialize the plugin):
$("#phone").intlTelInput({ hiddenInput: "full_phone", // This tells the plugin to create a hidden input with name="full_phone" initialCountry: "auto", // Adjust as needed geoIpLookup: function(callback) { // Optional: For auto-detecting user's country $.get("https://ipinfo.io", function() {}, "jsonp").always(function(resp) { var countryCode = (resp && resp.country) ? resp.country : "us"; callback(countryCode); }); } });
In your index.php form (inside the .lp-pom-form-f div):
<input type="tel" id="phone" name="phone" class="lp-pom-form-field"> <!-- The plugin will automatically add this hidden input if configured correctly --> <input type="hidden" name="full_phone" id="full_phone">
Step 2: Update mail.php to Use the Full Number
Instead of using the visible phone input value, use the hidden full_phone value which includes the country code:
// Replace this line (if present): $phone = $_POST['phone']; // With this: $phone = $_POST['full_phone'];
2. Format the Phone Number to Match Your Desired Style (Figure 2)
To get the human-readable format you want (like +1 (555) 123-4567), you have two reliable options:
Option A: Client-Side Formatting (Using the Plugin)
On form submission, use the plugin's getNumber method to retrieve the formatted number and send it via another hidden input:
Add this to your JavaScript:
$("form").submit(function(e) { // Get the number formatted in international style (matches your Figure 2 example) var formattedNumber = $("#phone").intlTelInput("getNumber", intlTelInputUtils.numberFormat.INTERNATIONAL); // Set the formatted number to a new hidden input $("#formatted_phone").val(formattedNumber); });
Add the hidden input to your index.php form:
<input type="hidden" name="formatted_phone" id="formatted_phone">
Then in mail.php, use this formatted value in your email:
$formattedPhone = $_POST['formatted_phone']; // Include $formattedPhone in your email body
Option B: Server-Side Formatting (PHP)
If you prefer to handle formatting on the server, use the libphonenumber-for-php library (the PHP port of Google's trusted phone number formatting tool):
- Install the library via Composer:
composer require giggsey/libphonenumber-for-php
- Use it in
mail.phpto format the E.164 number:
require 'vendor/autoload.php'; use libphonenumber\PhoneNumberUtil; use libphonenumber\PhoneNumberFormat; $phoneUtil = PhoneNumberUtil::getInstance(); try { $numberProto = $phoneUtil->parse($_POST['full_phone'], null); $formattedPhone = $phoneUtil->format($numberProto, PhoneNumberFormat::INTERNATIONAL); } catch (Exception $e) { // Handle invalid number $formattedPhone = $_POST['full_phone']; } // Use $formattedPhone in your email
Final Checks
- Make sure all hidden inputs are inside the
<form>tag so they're submitted. - Test the form to confirm the country code is included and the format matches your Figure 2 example.
内容的提问来源于stack exchange,提问作者Umut Can Alparslan

