Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Send email using Gmail through mail() ? Where do I put the password?

Tags:

php

email

gmail

I am trying to send user a activation link through mail by using my gmail account. how do i set it up.How do Send email using Gmail? Where do I put the password?.

Is it to ancient or should I go for object oriented method.

// 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";
}
like image 967
Asif Avatar asked Apr 30 '12 07:04

Asif


People also ask

Can I use Gmail SMTP server for sending mail?

Use the Gmail SMTP serverIf you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.

How do I add SMTP credentials to Gmail?

Enter smtp.gmail.com as your host. Input 465 (SSL) or 587 (TLS) as SMTP port. Postfix (mail transfer agent) on the server prefers STARTTLS (port submission/587) over port 465; hence, it is recommended to use port 587. Enter your Gmail account's email (e.g., [email protected]) in the username field.


2 Answers

Download phpmailer and try the following code

<?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!";
like image 153
Vahid Chakoshy Avatar answered Oct 10 '22 05:10

Vahid Chakoshy


the mail builtin is not very suitable for this, it supports only simple setups.

have a look at pear mail, the examples show you how to send using smtp auth.

like image 32
mata Avatar answered Oct 10 '22 04:10

mata