使用SMTP联系表单

时间:2015-02-19 22:09:14

标签: email smtp contact-form

如果我不使用smtp,我有一个简单的联系表单。我试图添加smtp与mandrill一起使用,但它不起作用。电子邮件没有通过mandrill。

即使我启用了' $ mail-> SMTPDebug = 1;'我发送后也没有看到任何错误。我已尝试过本地和实时服务器环境。

<?php
require_once('../mail/class.phpmailer.php');

$emailaddress = "name@domain.com"; 

// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$ip = $_SERVER['REMOTE_ADDR'];
$message = $_POST['message'];


$subject = "Contact Form: $name";
$body = "You have received a new message from website.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nIP Address: $ip\n\nMessage:\n$message";

$mail             = new PHPMailer();
$mail->IsSMTP(); 
$mail->Host       = "smtp.mandrillapp.com"; 
//$mail->SMTPDebug  = 1;                     
$mail->SMTPAuth   = true;                 
$mail->Host       = "smtp.mandrillapp.com"; 
$mail->Port       = 587;                    
$mail->Username   = "username"; 
$mail->Password   = "password";        

$mail->CharSet  = 'UTF-8';  
$mail->SetFrom($email_address);
$mail->AddReplyTo($email_address);
$mail->Subject    = "Contact Form: ".$_POST['name']." ";
$mail->MsgHTML($body);

$mail->AddAddress($emailaddress);
$mail->Send();

?>

1 个答案:

答案 0 :(得分:0)

我明白了。我还需要包含'class.stmp.php'文件。它不必是调用,但需要与'class.phpmailer.php'在同一个文件夹中。