Can someone help me out with phpmailer?

2019-09-16 13:13发布

问题:

I've made a HTML email and everything has worked so far. I was planning to send it with PHP (using the mail(); function). However, when I did this, the mail did not arrive at hotmail and gmail accounts. I googled around a bit and people suggested to use PHPmailer.

I downloaded PHPmailer and installed it on my server. So far, so good. But now I have the following code:

<?php
set_include_path('.:c:\domains\mydomain\wwwroot\phpmailer\phpmailer.inc.php');
set_include_path('.:c:\domains\mydomain\wwwroot\phpmailer\smtp.inc.php');

require("phpmailer.inc.php");


$mail = new PHPMailer();

$mail->IsHTML(true);
$mail->From     = "from@example.com";
$mail->AddAddress("mymail@mydomain.com");

$mail->Subject  = "An HTML Message";
$mail->Body     = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";

if($mail->Send()) {
  echo 'Message is sent';

} else {
  echo 'Message was not sent..';
 echo 'Mailer error: ' . $mail->ErrorInfo;
}
?>

I got several problems:

  • The output tells me the email is not sent, but it is.
  • I get two subjects
  • If I add more html (like a table), it still gets rejected by hotmail (and probably gmail too).

Also, I saw there's a SMTP function. How to use this function? Do I need to write down my own SMTP?

Would be very happy if someone could help me out!

edit:

class SMTP {
    var $SMTP_PORT = 25; # the default SMTP PORT
    var $CRLF = "\r\n";  # CRLF pair

    var $smtp_conn;      # the socket to the server
    var $error;          # error if any on the last call
    var $helo_rply;      # the reply the server sent to us for HELO

    var $do_debug;       # the level of debug to perform

    /*
     * SMTP()
     *
     * Initialize the class so that the data is in a known state.
     */
    function SMTP() {
        $this->smtp_conn = 0;
        $this->error = null;
        $this->helo_rply = null;

        $this->do_debug = 0;
    }

回答1:

I use PHPMailer in different projects.

I suggest you to send the mails via SMTP, of course PHPMailer supports it:

$mail = new PHPMailer();
$mail->IsSMTP();     // send via SMTP
$mail->Host = "smtp.gmail.com" //(or the smtp server you use)
$mail->Port = "465" //(gmail uses secure smtp so that runs on port 465)
$mail->SMTPAuth  = "true"   //we need to autenticate to the server
$mail->SMTPSecure = "ssl"  //we use ssl to protected the flow of info
$mail->Username = "mymail@gmail.com" //account 
$mail->Password = "password" //password


//build the message
 $mail->IsHTML(true);
$mail->From     = "mymail@gmail.com";
$mail->AddAddress("mymail@mydomain.com"); //who receives the mail
$mail->Subject  = "An HTML Message";
$mail->Body     = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";

if($mail->Send()) {
  echo 'Message is sent';

} 
else {
  echo 'Message was not sent..';
 echo 'Mailer error: ' . $mail->ErrorInfo;
}