php - 如何通过 mail() 使用 Gmail 发送电子邮件?我把密码放在哪里?

标签 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";
}

最佳答案

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

关于php - 如何通过 mail() 使用 Gmail 发送电子邮件?我把密码放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10380051/

相关文章:

php - 处理php soap服务器异常

javascript - 使用隐藏输入是在外部 JavaScript 文件中检索 php 变量的有效方法吗?

javascript - 预加载 html 网页或非 flash web 应用程序?

android - 无法阅读 gmail 通知

google-app-engine - 模式是否可以用于发送地址可配置的产品中

php - iPhone App : How to get data from an API, 解析并存储以供以后使用?

javascript - dropzone js 为什么我无法上传文件?

asp.net - 我的 ASP.Net 代码可以从 sendgrid 获得电子邮件已发送的确认吗?

php - 在 php 中使用发送确认电子邮件

azure - 使用 powershell、cmd 或 python 自动创建 M365 Outlook 配置文件