1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HOW TO use gmail to send emails in php using PHPMailer

Discussion in 'PHP' started by resellwww, Jun 4, 2008.

  1. #1
    1 Download PHPMailer from http://phpmailer.sourceforge.net
    2 Extract to folder phpmailer
    3 Create a file email.php
    4 Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)

    ?>
    require("phpmailer/class.phpmailer.php");
    IsSMTP(); // send via SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "username@gmail.com"; // SMTP username
    $mail->Password = "password"; // SMTP password
    $webmaster_email = "username@doamin.com"; //Reply to this email ID
    $email="username@domain.com"; // Recipients email ID
    $name="name"; // Recipient's name
    $mail->From = $webmaster_email;
    $mail->FromName = "Webmaster";
    $mail->AddAddress($email,$name);
    $mail->AddReplyTo($webmaster_email,"Webmaster");
    $mail->WordWrap = 50; // set word wrap
    $mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
    $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
    $mail->IsHTML(true); // send as HTML
    $mail->Subject = "This is the subject";
    $mail->Body = "Hi,
    This is the HTML BODY "; //HTML Body
    $mail->AltBody = "This is the body when user views in plain text format"; //Text Body
    if(!$mail->Send())
    {
      echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
      echo "Message has been sent";
    }
    ?> 
    Code (markup):
    5 Open the file class.smtp.php in phpmailer directory
    6 Paste this code
    $host = "ssl://smtp.gmail.com";
    $port = 465;
    before the line 104 #connect to the smtp server
    Hint: Search for #connect
    7 Open this page in browser and it will send the email using GMail.

    Hint: When you want to email the details from a form, set the variables using the form variables.
    eg. $mail->Username=$_POST['email']
     
    resellwww, Jun 4, 2008 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    I don't like to trust scripts with my google account login :S

    anybody else use this?
     
    crath, Jun 4, 2008 IP
  3. resellwww

    resellwww Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have used this.. its just uses google SMTP server nothing else...
    its uses PHPmailer which is certified project of sourceforge
     
    resellwww, Jun 4, 2008 IP
  4. robp2175

    robp2175 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am a n00b to php. Can you explain in more detail how I could use this in a form. Maybe give me a quick example. I appreciate it. Thank you very much.
     
    robp2175, Feb 12, 2009 IP
  5. boxcar

    boxcar Peon

    Messages:
    449
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This in no way would bypass the relay issues for bulk mail. I have used a similar script and it works. However, you will hit a 200 to 500 relay limit.

    I do have a question relating to this topic though....
    Is there a way to create a mailbox in PHP (and delete it). I was hoping to do this programmatically rather than in Simple Control Panel (installed on the dedicated server provided by GoDaddy).

    Regards,
    boxcar
     
    boxcar, Feb 12, 2009 IP
  6. glassfairy

    glassfairy Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it gives the error Fatal error: Call to undefined function IsSMTP() in D:\wamp\www\email.php on line 3

    i put email.php outside the folder phpmailer.
     
    glassfairy, Jan 4, 2010 IP
  7. bugsbane

    bugsbane Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The call to that must be replaced with the following:

    $mail = new PHPMailer();
    $mail->IsSMTP();

    but you've probably figured this out in the past year or so. Ciao.
     
    bugsbane, Feb 8, 2012 IP
  8. Bhavik Mehta

    Bhavik Mehta Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Used above mentioned script to send mails via gmail using PHP - I got a mail from gmail support regarding a suspicious activity on my account.

    Please let me know if there are any changes to be made to this code so that I dont face such security issue. Thanks!
     
    Bhavik Mehta, Sep 3, 2013 IP