这是我在这里的第一个问题,所以感谢大家阅读这篇文章。我正在遵循PHP学院在注册和登录部分的教程。
我正在使用亚马逊的EC2服务(到目前为止运行良好(,我想知道如何更改我的代码以使用我的SMTP服务器(从另一个虚拟主机(发送我的激活电子邮件。请参阅下面的代码。如果您有任何疑问,请告诉我。同样,这是我的第一次。
感谢您的帮助。
亲切问候恩里科·尤斯曼
常规.php文件:
function email($to, $ubject, $body) {
mail($to, $subject, $body, 'From: activate@proxico.nl');}
用户.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");
}
注册.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
}
使用 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;
}
}
您需要编辑 php.ini 文件以指定适当的 SMTP 服务器参数,或者最好使用其他方法:
使用 PHP 从 SMTP 服务器发送电子邮件
使用 PHPMailer 类在代码中轻松指定 smtp 服务器。否则只需修改 php.ini 文件。
https://code.google.com/a/apache-extras.org/p/phpmailer/