2012-04-30 19 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 authを使用して送信する方法を示します。

+0

を試してみてください。 –

+0

はい、それはtlsなしでsmtp authを使用するので、安全ではなく、私はそれをお勧めしません。ローカルのsmtp withouth認証を使用してメールを送信すると便利ですが、それ以外の場合は使用しません。 – 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

関連する問題