【问题标题】:How would i go about using SMTP for this PHP form [closed]我将如何为这个 PHP 表单使用 SMTP [关闭]
【发布时间】:2015-07-14 03:46:20
【问题描述】:

我使用的服务器不允许使用 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

}

?>

【问题讨论】:

    标签: php email smtp


    【解决方案1】:

    请查看这个很棒的 SMTP 库以使用 PHP 发送电子邮件。

    PHPMailer:

    https://github.com/PHPMailer/PHPMailer

    示例(取自 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';
    }
    

    【讨论】:

    • 我明白了。所以基本上要转换现有的 php 并使其使用 SMTP,我需要获取 php Mail 自动加载器。我认为这只是将身份验证添加到主文件中,仅此而已。谢谢 。我会研究这个。
    • 对于非常简单的解决方案您可以使用 mail() documentation 但我不推荐它。从长远来看,PHPMailer 更加灵活。
    猜你喜欢
    • 2014-08-23
    • 2014-03-02
    • 1970-01-01
    • 2023-04-03
    • 2021-03-20
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多