Question

This is my first question on here, so thanks everyone for reading this. I'm following the tutorials by PHP academy on the Register & Login part.

I'm using Amazon's EC2 service (Which is working great so far) and I would like to know how to change my code to use my SMTP server (From another webhost) to send my activation emails. See code below. If you have any questions, please let me know. Again, this is my first time.

Thanks for the help.

Kind regards, Enrico Eusman

General.php file:

function email($to, $ubject, $body) {
mail($to, $subject, $body, 'From: activate@proxico.nl');}

Users.php file:

function register_user($register_data) {

array_walk($register_data, 'array_sanitize');
        $register_data['password'] = md5($register_data['password']);

        $fields = '`' . implode('`, `', array_keys($register_data)) . '`';
        $data = '\'' . implode('\', \'', $register_data) . '\'';

        mysql_query("INSERT INTO `users` ($fields) VALUES ($data)");
        email($register_data['email'], 'Activate your account',"Hello " . $register_data['first_name'] . ",\n\nYou need to activate your account, so use the link below:\n\nhttp://ec2-54-229-189-136.eu-west-1.compute.amazonaws.com/activate.php?email=" . $register_data['email'] . "&email_code=" . $register_data['email_code'] . " \n\n - Proxico");

}

Register.php file:

if (isset($_GET['success']) && empty($_GET['success'])) {
    echo 'You\'ve been registered successfully! Please check your email for an activation link. Didn\'t get an email? Click here to resend.';
} else {
    if (empty($_POST) === false && empty($errors) === true) {
    $register_data = array(
        'username'      => $_POST['username'],
        'password'      => $_POST['password'],
        'first_name'    => $_POST['first_name'],
        'last_name'     => $_POST['last_name'],
        'email'         => $_POST['email'],
        'email_code'    => md5($_POST['username'] + microtime())
    );

    register_user($register_data);
    header('Location: register.php?success');
    exit();


} else if (empty($errors) === false) {
    echo output_errors($errors);
    // output register errors
}
Was it helpful?

Solution

Use Swiftmailer.

function email($to, $subject, $body) {
    require_once('swift/lib/swift_required.php');
    
    try {
        $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
            ->setUsername('your username')
            ->setPassword('your password')
        ;
        $mailer = Swift_Mailer::newInstance($transport);
        $message = Swift_Message::newInstance()
            ->setFrom('activate@proxico.nl', 'Activation - Proxico')
            ->setTo($to)
            ->setSubject($subject)
            ->setBody($body, 'text/html')
        ;
        return $mailer->send($message);

    } catch (Exception $e) {
        return false;
    }
}

OTHER TIPS

You'll either need to edit your php.ini file to specify the appropriate SMTP server parameters, or even better use a different method:

Sending email with PHP from an SMTP server

Use the PHPMailer class to easily specify an smtp server in code. Otherwise just modify the php.ini file.

https://code.google.com/a/apache-extras.org/p/phpmailer/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top