• Hi all,

    I am using PHPMailer for smtp authentication to send emails. I have tried both options, secured and unsecured as provided by my hosting provider.

    The problem looks like that the code does not recognize $phpmailer (i guess). The code is as below. (some of the information is fake here for
    security purpose, but actual values are used in the code.):

    add_action( ‘phpmailer_init’, ‘configure_smtp’ );
    function configure_smtp( PHPMailer $phpmailer ){
    $phpmailer->isSMTP();
    $phpmailer->Host = “mail.mydomain.net”;
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 25;
    $phpmailer->Username = “[email protected]”;
    $phpmailer->Password = “<password>”;
    $phpmailer->From = “[email protected]”;
    $phpmailer->FromName=”HRD”;
    $phpmailer->SMTPSecure = false;
    }

    and here’s the test code which sends mail:

    $to = “[email protected]”;
    $subject = “Checking email with smtp auth.”;
    $message = “Please ignore this.”;
    $ret = wp_mail($to, $subject, $message);
    echo ‘—‘; //added just for debugging.
    echo $ret;
    echo ‘++++++++++’; //added just for debugging.
    exit;

    Now it should echo —1++++++++++ whereas it always echoes —++++++++++ which means the mail could not be sent. I don’t know how to get the exact return/error code so that I can fix it. Also if i do not use PHPMailer at all the mails are sent successfully but they are reported as spam by the recipient servers to our hosting server as expected. So we have to authenticate through valid smtp credentials. Please help!

    Thanks,

    baburman

Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    First, this line:

    function configure_smtp( PHPMailer $phpmailer ) {

    should probably look like this:

    function configure_smtp() {
    	$phpmailer = new PHPMailer;

    But more importantly, have you included the PHPMailer classes somewhere else in your plugin/theme? If not, that is your problem…PHPMailer doesn’t exist when you attempt to instantiate it.

    Also be aware that PHPMailer is written to use a framewors such as Symfony, along with the Composer dependency autoloader. WordPress uses neither, so adding PHPMailer will require some customization on your part.

    Thread Starter baburman

    (@baburman)

    Thanks DionDesigns.

    It seems to work now and at least the mail is sent. But I guess the wp_mail() function is not using PHPMailer. There are two reasons in support of it:

    1- The mails are sent even if i provide wrong password for $phpmailer->Password.

    2- The From field name should be HRD and mail account should be [email protected] whereas the receiving mail shows WordPress <[email protected]> in the From field of the email.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Failed to send email through SMTP authentication.’ is closed to new replies.