phpmailer - 电子邮件不能发送到Gmail

时间:2015-07-16 05:34:26

标签: php email smtp gmail phpmailer

我遇到使用phpmailer发送邮件的问题。我是初学程序员。我正在尝试联系表格。我的代码如下(submit.php)。请建议我..提前致谢。

session_start();    
require_once 'libs/phpmail/PHPMailerAutoload.php';
$errors = array();

if(isset($_POST['name'], $_POST['phone'],$_POST['mail'],$_POST['message'])){

    $fields = array(
        'name' => $_POST['name'],
        'phone' => $_POST['phone'],
        'email' => $_POST['mail'],
        'message' => $_POST['message']
    );

    foreach ($fields as $field => $data) {
        if(empty($data)){
            $errors[] = 'The '. $field . ' field is required';
        }
    }

    if(empty($errors)){

        $m = new PHPMailer;

        $m -> isSMTP();
        $m -> SMTPAuth = true;

        //$m -> SMTPDebug = 2;

        $m -> Host = 'smtp.gmail.com';
        $m -> Username = 'xxxx@gmail.com';
        $m -> Password = 'xxxx';
        $m -> SMTPSecure = 'ssl';
        $m -> Port = 465;

        $m -> isHTML();
        $m -> Subject = 'Contact form submitted';
        $m -> Body = 'From: ' . $fields['name']. '('. $fields['phone'] . $fields['email']. ')'.'<p>' .$fields['message'] .'</p> ';

        $m -> FromName = 'Contact';

       // $m ->addReplyTo($fields['email'], $fields['name']);

        $m -> addAddress('ssss@gmail.com', 'xxxxxxxx');

        if($m->send()){
           header('Location: thanks.php');
             die();


        }else{
            $errors[] = 'Sorry could not send email. Please try again';

        }

    }


}else{
    $errors[] = 'some thing went wrong';
}

$_SESSION['error'] = $errors;
$_SESSION['field'] = $fields;


header('Location: form.php');

2 个答案:

答案 0 :(得分:0)

我的设置phpmailer,一切正常

function __construct ($to, $subject, $body) {
            date_default_timezone_set('Etc/UTC');
    //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 = 0;
    //Ask for HTML-friendly debug output
            $mail->Debugoutput = 'html';
            $mail->CharSet = 'UTF-8';
    //Set the hostname of the mail server
            $mail->Host = "mail.xxxxxx.com";
    //Set the SMTP port number - likely to be 25, 465 or 587
            $mail->Port = 25;
//Whether to use SMTP authentication
        $mail->SMTPAuth = true;
        $mail->AuthType = 'PLAIN';
//Username to use for SMTP authentication
        $mail->Username = "xxxx";

//Password to use for SMTP authentication
        $mail->Password = "xxxx";
//Set who the message is to be sent from
        $mail->setFrom('erp@xxxxxx.com');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
        $mail->addAddress($to);
//Set the subject line
        $mail->Subject = $subject;
//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($body);

//Replace the plain text body with one created manually
        $mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

        $this->mail = $mail;

    }

    function SendMail () {
        //send the message, check for errors
        if (!$this->mail->send()) {
            return "Mailer Error: " . $this->mail->ErrorInfo;
        } else {
            return true;
        }

    }

// using
$email = $this->request->getPost('email');
$smtp = new \SmtpClient($email, 'Test', $template);
$result = $smtp->SendMail();

答案 1 :(得分:0)

删除

$m -> Subject = 'Contact form submitted';

再试一次。

当我删除它有效的subject时。