为什么使用phpMailer发送邮件时出错?

时间:2017-05-18 12:30:30

标签: php email

为什么使用phpMailer发送邮件时出错?

<?php
    require 'PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $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 encryption, 'ssl' also accepted

    $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->WordWrap = 50;                                 // Set word wrap to 50 characters
    $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';
    }
  

无法发送消息.Mailer错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

1 个答案:

答案 0 :(得分:-1)

<?php

    $mailto = $_POST['mail_to'];
    $mailsub = $_POST['mail_subject'];
    $mailmsg = $_POST['mail_message'];
    require 'includes/PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail ->IsSmtp();
    $mail ->SMTPDebug = 1;
    $mail ->SMTPAuth = true;
    $mail ->SMTPSecure = 'ssl';
    $mail ->Host = "smtp.gmail.com";//If Your sending accound server is gmail use this else change
    $mail ->Port = 465; // or 587
    $mail ->IsHTML(true);
    $mail ->Username = "abc@gmail.com"; //use your sending mail accound address
    $mail ->Password = "abc123"; //use the account password of the above used mail.
    $mail ->SetFrom("abc@gmail.com");//use your sending mail accound address (Same)
    $mail ->Subject = $mailsub;
    $mail ->Body = $mailmsg;
    $mail ->AddAddress($mailto);

    if(!$mail->Send()){
        echo "Mail not Send";
    }
    else{
        echo " Mail Send";
    }
?>

Tihs Code正在运作