tags:

views:

95

answers:

5

Hi

Trying to send an e-mail via SMTP, its not working, any help?

//SMTP info
$host = "smtp.gmail.com";
$username = "[email protected]";
$password = "password";
$from = "Taylor";//From email address
$to = "[email protected]";//Change this to your inbox


$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
// HTML email
//$headers .= "MIME-Version: 1.0" . "\r\n";
//$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));

//Send to you
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
    echo("<h1>** Whoops! Error!. **</h1> <p>Please call us instead.</p>");
    echo("<p>" . $mail->getMessage() . "</p>");
} else {
    echo("<h1>Thank you for your inquiry!</h1> <p>Thank you for signing up for our event. Your request for our guestlist has been received. Please check your email for the confirmation.</p> ");
}
+2  A: 

Please check your PHP logs and see if there are any errors reported. The syntax looks right.

Scooterville
How do i do that :S cheers mate appreciate it
View your PHP environment variables, then find the log location and open it with a text viewer. Scroll to the end, some errors should be logged there. If you're in Windows in may be in C:\PHP5\Logs\ and in Linux it may be in /var/log/.
Scooterville
+1  A: 

Your $from might be your problem. That isn't exactly a valid email address. Try either changing it to a real address or omitting altogether.

Chris Kloberdanz
Thanks, i tried that, but didnt work
A: 

The google documentation says that ports 465 or 587 should be used for connections to the SMTP server. It seems that Mail_smtp uses 25 by default. Try an explicit value of 465 or 587 with the 'port' parameter

$smtp = Mail::factory(
    'smtp',
    array('host' => $host,
          'port' => 465,     // explicit port 465 or 587
          'auth' => true,
          'username' => $username,
          'password' => $password),
);
Inshallah
Thankyou, but it didnt work :S
Also have a look at the following site, it has an example that should work with smtp.gmail.com under the "Sending using authenticated SMTP" heading (and oddly enough, it does use port 25): http://www.phpmaniac.net/wiki/index.php/Pear_Mail
Inshallah
A: 

It looks to my like your $from is not a valid email address. You could try replacing this with something more along the lines of "[email protected]".

However, I have had little success with the standard PHP SMTP library, so I always use the open source phpmailer instead. It's just as easy, and sends as both HTML and plain text, produces very helpful error messages if something goes wrong, etc.

Good luck.

nomulous
Hey, i tried that, i got an error every time saying the from address was invalid, it was a googlemail adress
A: 

Note that many ISPs require you to use their own SMTP server and block other ones.

ceejayoz