Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email from localhost with gmail(windows)

Tags:

php

email

I want to use the mail() function from my localhost. I have WAMP installed and a Gmail account. I know that the SMTP for Gmail is smtp.gmail.com and the port is 465. What I need to configure in WAMP so I can use the mail() function? Thanks

like image 402
no_freedom Avatar asked Feb 13 '11 06:02

no_freedom


People also ask

Can I send email using localhost?

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost. For example, you can configure C:\xampp\php\php. ini and c:\xampp\sendmail\sendmail.

Can I use Gmail as SMTP server?

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.


1 Answers

Ayush's answer was very useful, below a slightly simplified approach
1) Download PHPMailer
2) Extract to folder within you php project and rename it to phpmailer
3) Create gmail-sample.php and paste the following code:

    <?php
    require("phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    // ---------- adjust these lines ---------------------------------------
    $mail->Username = "[email protected]"; // your GMail user name
    $mail->Password = "your-gmail-password"; 
    $mail->AddAddress("[email protected]"); // recipients email
    $mail->FromName = "your name"; // readable name

    $mail->Subject = "Subject title";
    $mail->Body    = "Here is the message you want to send to your friend."; 
    //-----------------------------------------------------------------------

    $mail->Host = "ssl://smtp.gmail.com"; // GMail
    $mail->Port = 465;
    $mail->IsSMTP(); // use SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->From = $mail->Username;
    if(!$mail->Send())
        echo "Mailer Error: " . $mail->ErrorInfo;
    else
        echo "Message has been sent";
    ?>

4) Send mail from Browser (e.g. http://localhost/your-project/gmail-sample.php).

like image 63
Wolfi Avatar answered Oct 03 '22 15:10

Wolfi