2012-04-30 31 views
3

我想通過使用我的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: [email protected]"; 
$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"; 
} 

回答

1

mail內建不是很適合這個,它僅支持簡單的設置。

看看pear mail,這些例子展示瞭如何使用smtp認證發送。

+0

你可以做到這一點,沒有任何其他資源。 –

+0

是的,但它使用smtp驗證沒有tls,所以它是不安全的,我不會推薦它。如果您使用本地smtp不帶身份驗證發送郵件很有用,但否則我不會使用它。 – mata

9

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  = "[email protected]"; 
$mail->FromName = "you name"; 
$mail->Subject = "some subject"; 
$mail->MsgHTML("the message"); 

//$mail->AddReplyTo("[email protected]","reply name");//they answer here, optional 
$mail->AddAddress("[email protected]","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!"; 
+0

如何在我的代碼上實現這個功能?我不太擅長OOP,課堂等等。 –

+0

感謝您的幫助:-) – Sebas

+0

http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html – Sebas

相關問題