如何通过邮件()发送邮件?我在哪里输入密码? [英] How do I Send email using Gmail through mail() ? Where do I put the password?

查看:103
本文介绍了如何通过邮件()发送邮件?我在哪里输入密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用我的Gmail帐户通过邮件向用户发送激活链接。如何设置?如何使用Gmail发送电子邮件?我在哪里输入密码?

I am trying to send user a activation link through mail by using my gmail account. how do i set it up.How do Send email using Gmail? Where do I put the password?.

是古代还是应该去面向对象的方法。

Is it to ancient or should I go for object oriented method.

// secure the password
$passWord = sha1($passWord);
$repeatPass = sha1($repeatPass);
// generate random number
$random =rand(1200345670,9999999999);

//send activation email
$to = $email;
$subject = "Activate your account";
$headers = "From: ti.asif@gmail.com";
$server = "smtp.gmail.com";
$body = "Hello $username,\n\n You registered and need to activate your account. Click the link below or paste it into the URL bar of your browser\n\nhttp://phpacademy.info/tutorials/emailactivation/activate.php?id=$lastid&code=$code\n\nThanks!";

ini_set("SMTP",$server);

if (!mail($to,$subject,$body,$headers))
    echo "We couldn't sign you up at this time. Please try again later.";
else
{
    // register the user            
    $queryreg = mysql_query("
        INSERT INTO users VALUES ('','$userName','$passWord','$fullName','$date','$random','0','$email')
                            ");

    $lastid = mysql_insert_id();

    die ("You have been registered. <a href='login.php'>Click here</a> to return to the login page.");
    echo "Successfully Registered";
}


推荐答案

下载phpmailer 并尝试以下代码

<?php
$mail  = new PHPMailer();   
$mail->IsSMTP();

//GMAIL config
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the server
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "gmailusername";  // GMAIL username
$mail->Password   = "gmailpassword";            // GMAIL password
//End Gmail

$mail->From       = "from@email.com";
$mail->FromName   = "you name";
$mail->Subject    = "some subject";
$mail->MsgHTML("the message");

//$mail->AddReplyTo("reply@email.com","reply name");//they answer here, optional
$mail->AddAddress("address@to.com","name to");
$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {//to see if we return a message or a value bolean
    echo "Mailer Error: " . $mail->ErrorInfo;
} else  echo "Message sent!";

这篇关于如何通过邮件()发送邮件?我在哪里输入密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆