php mail()函数无法使用phpmailer工作

时间:2017-09-04 05:12:06

标签: php smtp phpmailer

<?php

require 'PHPMailerAutoload.php';
//echo !extension_loaded('openssl')?"Not Available":"Available <br/>";

$name = $_POST['username'];
$email = $_POST['email'];
$number = $_POST['phone'];
$profession = $_POST['profession'];

$to = 'example@gmail.com';
$subject = 'user registration';
$phone = "phone number:".$number;

$message = "client details:"."\n"."Name:".$name."\n"."email:".$email."\n"."phone number:".$number."\n"."profession:".$profession;

$headers = "From:".$email;

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               

$mail->isSMTP();                                      
$mail->Host = 'ssl://smtp.gmail.com';      
$mail->SMTPAuth = true;                            
$mail->Username = 'example@gmail.com';                      
$mail->Password = 'password';                           
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom($email, $name);

$mail->Subject = $subject;
$mail->Body    = $message;

if($mail->send()) {
    header("Location: ../../thankyouNew.html");
}
else {
    header("Location: ../../somethingWrong.html");
}

&GT;

代码正在执行else块,我想发送邮件到example@gmail.com并在执行邮件功能后将用户返回到thankyou.html页面。我是这个php的新手,我将非常感谢帮助谢谢你提前。 忘记下面的行........

1 个答案:

答案 0 :(得分:1)

您实际上并未指定发送电子邮件的位置。您需要使用addAddress()方法,如下所示。此方法需要一个参数,但您可以提供两个参数 - 与setFrom()方法相同;首先是目标地址,然后是可选的显示名称。

$mail = new PHPMailer;

// ...

$mail->setFrom($email, $name);
$mail->addAddress($to); // Add this method to specify a recipient 
$mail->Subject = $subject;
$mail->Body    = $message;
if($mail->send()) {
    // ...
}
// ...