1

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
}

3 Answers3

1

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

Community
  • 1
  • 1
klugerama
  • 3,273
  • 16
  • 24
1

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/

KHMKShore
  • 1,435
  • 9
  • 16
1

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;
    }
}
Community
  • 1
  • 1
Glavić
  • 39,315
  • 12
  • 70
  • 99
  • so what do I do if my SMTP server requires authentication? How do I implement it? Thanks in advance :) – Enrico Eusman Oct 09 '13 at 18:39
  • @EnricoEusman: I have updated my answer. [If SMTP requires user&pass](http://swiftmailer.org/docs/sending.html#smtp-with-a-username-and-password) or [If you are using encrypted SMTP](http://swiftmailer.org/docs/sending.html#encrypted-smtp). – Glavić Oct 09 '13 at 18:49
  • Thankks. It works now. Although sometimes I get an error, that the connection has timed out. Is there a function that I can use so that it keeps trying? Or just not show the error -> go to success page -> click to resend email? And is it possible to give my email a name? In the inbox, it just says activate@proxico.nl but I want it so say: Activation - Proxico – Enrico Eusman Oct 09 '13 at 19:12
  • @EnricoEusman: I have added `try {} catch() {}` block, so if there will be any error, function returns `false`. What to do when send fails is all up to your logic. I have also fixed `->setFrom` to set from name. – Glavić Oct 09 '13 at 19:21
  • 1
    Thank you so much! You saved me! I hope I can help you out some day! – Enrico Eusman Oct 09 '13 at 19:34