【问题标题】:How do I Send email using Gmail through mail() ? Where do I put the password?如何通过 mail() 使用 Gmail 发送电子邮件?我把密码放在哪里?
【发布时间】:2012-05-09 22:31:08
【问题描述】:

我正在尝试使用我的 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";
}

【问题讨论】:

    标签: php email gmail


    【解决方案1】:

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

    【讨论】:

    • 如何在我的代码上实现这个?我不太擅长 OOP、课堂等等。
    【解决方案2】:

    mail 内置函数不太适合这个,它只支持简单的设置。

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

    【讨论】:

    • 您无需任何其他资源即可实现这一目标。
    • 是的,但是它使用没有 tls 的 smtp auth,所以它不安全,我不会推荐它。如果您使用没有身份验证的本地 smtp 发送邮件,这很有用,否则我不会使用它。
    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 2011-05-11
    • 2020-01-06
    • 2021-10-03
    • 2014-05-23
    • 2016-08-29
    • 2012-01-07
    相关资源
    最近更新 更多