Help on gmail SMTP connection

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Help on gmail SMTP connection

Post by highjo »

hello just step into the swift mailer universe.trying to send an email from my Xampp 1.6.0a apache server on localhost.the php version is 5.2.1.I've seen the documentation online and opted to use the gmail SMTP server(before i uploap to my host and set the proper smtp server).But i'm having an issue.First of all i can see the function setUsername, i'm seeing setOption and setDomain.How do i make it work?Is the problem from the facti'm on localhost? Here is the code of my sendmail function

Code: Select all

 
<?php
include_once('swift/Swift.php');
include_once('swift/Swift/Connection/SMTP.php');
 
function sendEmail($message,$to)
{
$smtp =& new Swift(new Swift_Connection_SMTP("smtp.gmail.com",Swift_Connection_SMTP::PORT_SECURE,Swift_Connection_SMTP::ENC_TLS));
            $stmp->setUsername("myusername");
            $smtp->setpassword("mypass");
            
            
            $swift =& new Swift($smtp);
            $mes =& new Swift_Message("Voucher from txtBuddy",$message,"test/html");
            if($swift->send($mes,$to,"some@trueema.com"))
            {
                return "ok";
            }
            else
            {
                die("We are unable to send you the mail for some technical problem");
            }
        }
 
 
$tosend = '<html >
                                <head>
                                    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                                    <title>Payed voucher number from txtBuddy</title>
                                </head>
 
                                <body style="height:100%;min-height:100%;">
                                    <center>
                                        <div style="width:800px;position:relative;_height:100%;min-height:100%">
                                            <div style="width:600px;height:200px;margin:0 200px 0 200px auto">
                                                <div style="background-color:#4A7CAE;color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:bold">
                                                    TxtBuddy credit voucher
                                                </div>
                                                <div style="background-color:#E4EFF8;height:180px;color:#F38F5F;">
                                                    Text buddy is glad to send you your text voucher 
                                                    <b style="color:#003399">'. $voucheritself.' </b>
                                                    suscribed through i-wallet. This voucher is valid to send only
                                            '. $smsbought.' sms bought from txtBuddy.For sending text message click
                                                     <a href="#">Here</a> to send your first text sms with TxtBuddy
                                                </div>
                                            </div>
                                        </div>
                                    </center>
                                </body>
                            </html>';
            //pass the variable got from the list function on $voucherdetails which hold the values of the retreiveVoucherDetails call
            $mailstatus=$h->sendEmail($tosend,$mail);
            if($mailstatus == 'ok')
            {
?>
                <div>Dear customer a Email has been sent to your mail box with the voucher detail</div>
<?php
            }
 
?>
 


i'm seeing this error each time i launch it
Fatal error: Call to a member function setUsername() on a non-object in C:\Program Files\xampp\htdocs\servTxtBuddy\callhandler.class.php on line 83
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Help on gmail SMTP connection

Post by Chris Corbyn »

Code: Select all

$smtp = new Swift_Connection_SMTP("smtp.gmail.com",Swift_Connection_SMTP::PORT_SECURE,Swift_Connection_SMTP::ENC_TLS);
            $stmp->setUsername("myusername");
            $smtp->setpassword("mypass");
           
           
            $swift = new Swift($smtp);
            $mes = new Swift_Message("Voucher from txtBuddy",$message,"test/html");
            if($swift->send($mes,$to,"some@trueema.com"))
            {
                return "ok";
            }
            else
            {
                die("We are unable to send you the mail for some technical problem");
            }
Your $smtp variable was an instance of Swift instead of an instance of SMTP since you wrapped "new Swift()" around it. You need to do it in two steps like above.

Also, drop the ampersands "&" if you're using PHP5.... they're harmless mostly, but unneeded :)
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: Help on gmail SMTP connection

Post by highjo »

Yeah this answer was fast thanks a lot.I've just copy and paste your post and replace things.But is throwing the same fatal error message.I barely see a mistake here. My problem is still unsolved.Any other suggestions?
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: Help on gmail SMTP connection

Post by highjo »

I've tried the localhost smtp and it's working.i just have a little attachements problem .I've already read a post on it.does somebody tried to send a email from his localhost application?Trying to figure out if it from my configuration the problem is.Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Help on gmail SMTP connection

Post by Chris Corbyn »

What's the problem? If the mail is not arriving it's because you're sending from localhost, presumably from your own home. Dynamic IP addresses are always blacklisted.
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: Help on gmail SMTP connection

Post by highjo »

First of al i would like to thank you the support.I've finally send the mail to my locahost admin account sing XAMPP Mercury after i've set the plain text part of my email.There is no probleme anymore there.But i'm still wondering why i can use the gmail smtp server.Theorically i shoul not be a problem and then i can see those function in swift setUsername() and setPassword();If there is an authentication issue somewhere to an smtp (i' mean from my host online) what hould i do? :(
Thanks
Post Reply