MAMP和PHPmailer用于Gmail的联系表单

时间:2013-07-05 05:14:47

标签: php mamp phpmailer

我是新手,我正在尝试使用PHPmailer制作联系表单。 我不知道这是否正确,但我这样做是因为mail()函数在本地服务器中不起作用,顺便说一下!我正在用MAMP进行测试。 我从这里得到了代码https://phpacademy.org/course/php-contact-form 在那里使用mail()函数,但我无法理解如何使用MAMP进行测试。然后我下载PHPMailer,我运行一个例子并且完美地工作。所以我剪切并粘贴代码并做了一件奇怪的事情。 这是正确的吗?因为我可以在我的电子邮件中收到$ message和$ name 我在这里先向您的帮助表示感谢。

<?php  
if (empty($_POST) === false) {
  $errors = array();

  $name         = $_POST['name'];
  $email        = $_POST['email'];
  $message  = $_POST['message'];

if (empty($name) === true || empty($email) === true || empty($message) === true) {
    $errors[] = 'Nombre, Email y Mensaje son obligatorios'; 
} else {
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = 'Este Email no es valido';  
    }
    if (ctype_alpha($name) === false ) {
        $errors[] = 'El nombre debe contener unicamente letras!';
    }
}
if (empty($errors) === true) {

    //=====MailForm=======


    //SMTP needs accurate times, and the PHP time zone MUST be set
    //This should be done in your php.ini, but this is how to do it if you don't have access to that
    date_default_timezone_set('Etc/UTC');

    require '../class.phpmailer.php';

    //Create a new PHPMailer instance
    $mail = new PHPMailer();
    //Tell PHPMailer to use SMTP
    $mail->IsSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug  = 2;
    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';
    //Set the hostname of the mail server
    $mail->Host       = 'smtp.gmail.com';
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port       = 587;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';
    //Whether to use SMTP authentication
    $mail->SMTPAuth   = true;
    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username   = "username@gmail.com";
    //Password to use for SMTP authentication
    $mail->Password   = "password";
    //Set who the message is to be sent from
    $mail->SetFrom('username@gmail.com', 'Form by '.$name);

    //Set who the message is to be sent to
    $mail->AddAddress('username@hotmail.com', 'username');
    //Set the subject line
    $mail->Subject = 'PHPMailer GMail SMTP test JC';
    //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
    $mail->MsgHTML($message);

//=====MailForm END=======

            $mail->Send() 


    header('location:form5.php?sent');  //redirect user
    exit();

}

}
?>

0 个答案:

没有答案