RatherLogical
RatherLogical

Reputation: 310

How to integrate SMTP auth into a sendmail PHP script

Bear in mind, I am new to PHP

Hi, i'm having trouble sending SMTP mail from a script that only uses sendmail.

The below code is my original old code that works but that gives a warning on Gmail when viewed. It says this message can not be verified. I'm trying to send the user their credentials from a password reset form (I will be implementing this on the registration form as well).

Original Code:

    ##### Mail functions #####

    function sendLostPasswordEmail($myusername, $email, $newpassword)
    {

    global $domain;
    $message = "
    You have requested a new password on example.com

    Here is Your new password information:

    username:  $myusername
    password:  $newpassword


    Regards,
    example Administration
    ";

    if (sendMail($email, "Your password has been reset.", $message, "[email protected]"))
    {
    return true;
    } else
    {
    return false;
    }


    }

    function sendMail($to, $subject, $message, $from)
    {


    $from_header = "From: $from";

    if (mail($to, $subject, $message, $from_header))
    {
    return true;
    } else
    {
    return false;
    }
    return false;
    }

    function sendActivationEmail($myusername, $password, $uid, $email, $actcode)
    {
    global $domain;
    $link = "https://example.com/includes/activate.php?uid=$uid&actcode=$actcode";
    $message = "
    Thank you for registering on https://example.com,

    Your account information:

    username:  $myusername
    password:  $password

    Please click the link below to activate your account.

    $link

    Regards
    $domain Administration
    ";

    if (sendMail($email, "Please activate your account.", $message, "[email protected]"))
    {
    return true;
    } else
    {
    return false;
    }
    }

    ?>

Code to implement (I think)

    require_once "php/Mail/mail.php";

    $from = "Example Company <[email protected]>";
    $to = "<>";
    $subject = "Your Password Has Been Reset";
    $body = "$message";

    $host = "mail.example.com";
    $port = "465";
    $username = "[email protected]";
    $password = "***";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

I believe the 2nd code snippet will fit in with the original somewhere, however, I have been trying for several hours to solve this problem to no avail. I appreciate any help, if you need to see the actual password recovery form code or think I could improve my question please tell me.

Upvotes: 1

Views: 1285

Answers (2)

Pia Wurtzbach
Pia Wurtzbach

Reputation: 94

Try using PHPMailer. This tool is so powerful. You just need to include this to your website, configure and you are ready to go. Plus, good documentation and easy to understand.

Upvotes: 0

Trac Nguyen
Trac Nguyen

Reputation: 486

There are many reasons preventing you to send smtp email (supposing that you are use correct SMTP library)

  • You are using port 465, this is likely connection using smtps, please check you host in phpinfo() if it supports open_ssl extension.
  • Your host doesn't allow SMTP connection (A2hosting shared host for example)
  • Your SMTP provider need verification from your domain to allow email to be sent.

Hope that can help.

Upvotes: 1

Related Questions