Thursday, June 26, 2008

PhpMailer Settings to Send email using PHP with Gmail



To work with this just follow this 3 simple step

1. Create a new file and named it any name you like
example: send.php
2. Paste the following code

include "path to your class.phpmailer.php"; // something like this include include "PHPMailer_v2.0.0/class.phpmailer.php";

$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "password"; // your SMTP password or your gmail password
$from = "webmaster@example.com"; // Reply to this email
$to="recepient@domain.com"; // Recipients email ID
$name="Jersey Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, 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";
}
?>

3. Troubleshooting

If ever a warning message occurs occurs something like this
"Warning: fsockopen()[a href:="function.fsockopen">functionopen]: unable
to connect to ssl://smtp.gmail.com:465(Unable to find the socket transport "ssl" -did you forget to enable it when you configured PHP?)
in ....

To fix this:
Go to your php settings and enable php_openssl

that's it..

16 comments:

Anonymous said...

Thanks to your phpmail example I saved some time :) Thank you :)
Regards.

jersey said...

@Polskie

No problem i just want to help..

Stormtech said...

Thanks for the script.
On a Linux host the Warning: fsockopen() cannot be fixed by uncommenting the dll, but in my case the remedy was in the firewall.

Anonymous said...

I got am error "SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host."
will you please take me out of this problem
Thanks

sohan said...

^^
i got the same error!

Yash said...

Hello to all i am using a signup form, it has a function to send email confirmation to user. but mailer is not working. can't send mails to user after signup.

i am using google apps as mail.domain.com
----------------------------------
here is the link of my register.php mailer code
http://www.google.com/support/forum/p/Google+Apps/thread?tid=7861b03695cbce5d&hl=en
---------------------------


please tell me friend. is there any mailer file..
i am very frustrated i tried very much... i am new to php mailer.
please help me...

Raj said...

I am getting error as unable to connect to the SMTP host....i m using wamp server to run my php code.
Guys i am very much frustrated plz do help me...i have exactly followed wat's written dere in above...!!

Pushpendra Kuntal said...

i am getting error that :-

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.

please suggest me what should i do for this.

Pushpendra Kuntal said...

i am getting error that :-

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.

please suggest me what should i do for this.

Pushpendra Kuntal said...

i am getting error that :-

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.

please suggest me what should i do for this.

Pushpendra Kuntal said...
This comment has been removed by the author.
hemal said...

i try but it is not work

Naveed said...

Thank you so much, its working perfectly :)

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.