如何使用 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,nn You registered and need to activate your account. Click the link below or paste it into the URL bar of your browsernnhttp://phpacademy.info/tutorials/emailactivation/activate.php?id=$lastid&code=$codennThanks!";
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!";
内置

mail不是很适合这个,它只支持简单的设置。

看看梨子邮件,这些示例向您展示如何使用 SMTP 身份验证发送。

最新更新