如何通过邮件()使用Gmail发送电子邮件?我在哪里输入密码?

时间:2012-04-30 07:34:25

标签: php email gmail

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

它是古代还是我应该采用面向对象的方法。

// 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";
}

2 个答案:

答案 0 :(得分:10)

Download 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!";

答案 1 :(得分:1)

mail内置版不太适合这种情况,它只支持简单的设置。

查看pear mail,示例向您展示如何使用smtp auth发送。