【问题标题】:Sending activation emails on EC2 server using SMTP使用 SMTP 在 EC2 服务器上发送激活电子邮件
【发布时间】:2013-10-17 04:28:15
【问题描述】:

这是我在这里的第一个问题,所以感谢大家阅读本文。 我正在关注 PHP 学院关于注册和登录部分的教程。

我正在使用 Amazon 的 EC2 服务(到目前为止效果很好),我想知道如何更改我的代码以使用我的 SMTP 服务器(来自另一个网络主机)发送我的激活电子邮件。 请参阅下面的代码。 如果您有任何问题,请告诉我。再说一次,这是我的第一次。

感谢您的帮助。

亲切的问候, 恩里科·尤斯曼

General.php 文件:

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

Users.php 文件:

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 文件:

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
}

【问题讨论】:

  • 你的问题是什么?

标签: php email amazon-ec2 smtp activation


【解决方案1】:

使用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;
    }
}

【讨论】:

  • 如果我的 SMTP 服务器需要身份验证,我该怎么办?我该如何实施?在此先感谢:)
  • @EnricoEusman:我已经更新了我的答案。 If SMTP requires user&passIf you are using encrypted SMTP.
  • 谢谢。现在可以了。虽然有时我会收到错误,但连接已超时。有没有我可以使用的功能让它不断尝试?或者只是不显示错误 -> 转到成功页面 -> 单击以重新发送电子邮件?是否可以给我的电子邮件起个名字?在收件箱中,它只是说 activate@proxico.nl 但我希望这样说:Activation - Proxico
  • @EnricoEusman:我已经添加了try {} catch() {} 块,所以如果有任何错误,函数会返回false。发送失败时该怎么办完全取决于您的逻辑。我还修复了 ->setFrom 从名称设置。
  • 非常感谢!你救了我!希望有一天能帮到你!
【解决方案2】:

您要么需要编辑 php.ini 文件以指定适当的 SMTP 服务器参数,要么最好使用不同的方法:

Sending email with PHP from an SMTP server

【讨论】:

    【解决方案3】:

    使用 PHPMailer 类在代码中轻松指定 smtp 服务器。否则只需修改 php.ini 文件即可。

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

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 2012-06-10
      • 2016-09-07
      • 1970-01-01
      • 2021-10-01
      • 2015-06-19
      相关资源
      最近更新 更多