use of phpmailer class

风格不统一 提交于 2019-12-12 09:57:30

问题


i m using phpmailer class for sending emails. i m geeting error "Could not execute: /smtp" the following code the code is below. can someone suggest me what is the problem.

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

try {
    $mail = new PHPMailer(true); //New instance, with exceptions enabled

    $body             = file_get_contents('contents.html');
    echo $body;
    $body             = preg_replace('/\\\\/','', $body); //Strip backslashes

    $mail->IsSMTP();                           // tell the class to use SMTP
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 25;                    // set the SMTP server port
    $mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
    $mail->Username   = "test@gmail.com";     // SMTP server username
    $mail->Password   = "1234567889";            // SMTP server password

    $mail->IsSendmail();  // tell the class to use Sendmail

    $mail->AddReplyTo("rto@gmail.com","First Last");

    $mail->From       = "from@gmail.com";
    $mail->FromName   = "First Last";

    $to = "toemail@gmail.com";

    $mail->AddAddress($to);

    $mail->Subject  = "First PHPMailer Message";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->WordWrap   = 80; // set word wrap

    $mail->MsgHTML($body);

    $mail->IsHTML(true); // send as HTML

    $mail->Send();
    echo 'Message has been sent.';
} catch (phpmailerException $e) {
    echo $e->errorMessage();
}

回答1:


By using GMail, your settings might likely not be correct; try these (corrected port and SSL address):

$mail->Port       =  465;                   // set the SMTP server port
$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->Username   = "test@gmail.com";       // SMTP server username
$mail->Password   = "1234567889";   

Alternatively, use TLS:

$mail->Port       =  587;                   // set the SMTP server port
$mail->Host       = "tls://smtp.gmail.com"; // SMTP server

Edit: Remove this line below (You first provide settings to use SMTP, then you tell it to use and send trough sendmail and you put a wrong path in configs. Just stick to one service, use SMTP with above settings (or edit2 below) and see):

Remove:

$mail->IsSendmail();  // tell the class to use Sendmail

Edit2: other option (in case the first answer don't work):

$mail->SMTPSecure = 'ssl'; 
$mail->Host = 'smtp.gmail.com';


来源:https://stackoverflow.com/questions/6315052/use-of-phpmailer-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!