我将如何为此PHP表单使用SMTP

时间:2023-01-27 13:18:20

The server i use does not allow php mail function. It uses SMTP . I have little to no experience with it. Is there any guide or page which can show me how to add SMTP to the below PHP Contact Form . I have all the required credentials for the SMTP server.

我使用的服务器不允许php邮件功能。它使用SMTP。我几乎没有经验。是否有任何指南或页面可以告诉我如何将SMTP添加到下面的PHP联系表单。我拥有SMTP服务器所需的所有凭据。

<?php

if(isset($_POST['Email'])) {



// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "cerebros.x@gmail.com";

$email_subject = "Drywall Pros Form";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['Name']) ||

    !isset($_POST['Email']) ||

    !isset($_POST['Phone']) ||

    !isset($_POST['comments'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}



$Name = $_POST['Name']; // required

$Email = $_POST['Email']; // required

$Phone = $_POST['Phone']; // not required

$comments = $_POST['comments']; // required



$error_message = "";





$string_exp = "/^[A-Za-z .'-]+$/";



 if(!preg_match($string_exp,$Name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

 }


 if(strlen($comments) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}



$email_message .= "First Name: ".clean_string($Name)."\n";

$email_message .= "Email: ".clean_string($Email)."\n";

$email_message .= "Phone: ".clean_string($Phone)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->


 <?php header ( 'Location: thankyou.php' ); ?>
Thank you for contacting us. We will be in touch with you very soon.



<?php

}

?>

1 个解决方案

#1


Please check this great SMTP library for sending emails in PHP.

请检查这个伟大的SMTP库,以便用PHP发送电子邮件。

PHPMailer:

https://github.com/PHPMailer/PHPMailer

Example ( takes from Gighub page ):

示例(取自Gighub页面):

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

#1


Please check this great SMTP library for sending emails in PHP.

请检查这个伟大的SMTP库,以便用PHP发送电子邮件。

PHPMailer:

https://github.com/PHPMailer/PHPMailer

Example ( takes from Gighub page ):

示例(取自Gighub页面):

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}