在这里,我粘贴了一个完整的代码,用于在不打开任何 Outlook 或使用 gmail 凭据等的情况下发送邮件。它使用 PHP 发送邮件。希望这可以帮助你。
if(isset($_POST['submit'])) {
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
if(trim($_POST['ContactNum']) == '' && is_numeric($_POST['ContactNum'])) {
$hasError = true;
} else {
$Contacting = trim($_POST['ContactNum']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '' && preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$_POST['email'])) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//----------------------Email Validation-----------------//
function EmVal($e)
{
return preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$e);
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'abc@abc.com';
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments \n\nContact Number:\n $Contacting";
$headers = 'From: WebBestow <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;}
}
?>
您可以根据您的要求更新字段。并设置action="youpage.php"。此代码应在 yourpage.php 的 html 文档中