Sending email through Gmail SMTP using PHP - php

I have following system setup
Windows XP Service pack 2
WAMP 2.0
PHP 5.3
I've configured my php.ini file with the following:
smtp=smtp.gmail.com
smtp_port=25;
and my PHP code is
<?php
mail('alagar.pandi#gmail.com','test subject','test body');
?>
The error I'm getting is
Warning: mail() [function.mail]: SMTP server response:
530 5.7.0 Must issue a STARTTLS command first.
4sm389277yxd.16 in C:\wamp\www\limosbusesjets\test.php on line 5
Any suggestions?

I have always used PHPMailer for all my mailing needs.
It has built in support for GMail as a server (and it's free)
I think your issue is that you are trying to use PHP's mail settings and not PHPMailer's
Make sure you have the following set:
$mail = new PHPMailer(); //Setup the mailer
$mail->IsSMTP();
//$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; //enable SMTP authentication
$mail->SMTPSecure = "ssl"; //sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; //sets GMAIL as the SMTP server
$mail->Port = 465; //set the SMTP port
$mail->Username = $guser; //GMAIL username
$mail->Password = $gpwd; //GMAIL password
$mail->AddReplyTo($fromAddress,$fromName);
$mail->From = $guser;
$mail->FromName = "Your name";
$mail->Subject = $subject; //E-Mail subject
$mail->AltBody = $bodyAlt; //Text Body
$mail->WordWrap = 50; //set word wrap
$mail->Priority = $priority; //Mail priority
$mail->MsgHTML($ebody);

Mail via Google needs to be run through SSL.
There are a lot of arcticles on subject, you might find this useful: http://deepakssn.blogspot.com/2006/06/gmail-php-send-email-using-php-with.html

Related

Using PHPmailer didn't get mail in webmail

I try to sent mail on submitting the form using the SMTP server. I get mail on my Gmail account but didn't get mail on webmail.
Is it an SMTP server issue or webmail?
What can I do to send mail on webmail?
Have to something remaining config in webmail?
Below I tried code:
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**********'; // SMTP username
$mail->Password = '**********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('info#abc.com','ABC');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'ABC - Register Successfully';
$mail->Body = "
<h4>Dear $name,</h4>
<p>Thank you! Your registration has been successful.</p>
<p>If you need any help or query, please feel free to drop an email using the following email address: <strong>info#abc.com</strong></p>
<p>We appreciate your patience. </p>
<h4>Thanking You,</h4>
";
$mail->send();

How to connect and authenticate on the STMP server

I need to connect to the GMAIL SMTP server with email and password, without sending email and then receiving 200 OK.
I using the code PHPMailer:
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myuser#gmail.com";
$mail->Password = "password";
$mail->SetFrom("");
$mail->Subject = utf8_decode("My subject");
$mail->Body = "hello";
$mail->AddAddress("destination#gmail.com");
$mail->Body = template('emails/reset-password', compact('comentario'));
if ($mail->Send()) {
$mail->ClearAllRecipients();
$mail->ClearAttachments();
}
My script is sending email, I would just like to validate the authentication.
PHPMailer itself isn't much use for this as it's all about the sending. However, the SMTP class that's part of PHPMailer can do this just fine, and there is a script provided with PHPMailer that does exactly as you ask. I'm not going to reproduce the script here as it just creates maintenance for me!
The script connects to an SMTP server, starts TLS, and authenticates, and that's all.

PHPMailer's infamous SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed

I'm trying to get PHPMailer working using a Google Apps SMTP server. I've tried:
Uncommenting openssl in php.ini
Telneting Google's server on port 465 (success)
Telneting my webserver on port 465 (success)
Telnetting Google's server from my server (success)
Checking DNS SPF/MX records (and sanitizing to IPv4)
tls on port 587
webhost confirms they allow outbound SMTP traffic
google captcha's unlock trick
Reading everything I could find on StackOverflow (solutions covered the above)
Can someone provide a solution to the timed out connection?
Here's my code:
require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server
$Mail->SMTPDebug = 1; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "ssl"; //Secure conection
$Mail->Port = 465; // set the SMTP port
$Mail->Username = 'account#googleappsaddress.com'; // SMTP account username
$Mail->Password = 'mypassword'; // SMTP account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Test Email Using Gmail';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'myemail#gmail.com';
$Mail->FromName = 'GMail Test';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->AddAddress( $to ); // To:
$Mail->isHTML( TRUE );
$Mail->Body = $body;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->Send()) {
$error = 'Mail error: '.$Mail->ErrorInfo;
echo($error);
return false;
} else {
$error = 'Message sent!';
return true;
}
It turns out I was a victim of coincidence. When I ssh'ed to my server and telneted to Gmail, it was open - however, due to the openssl not being uncommented, I suspect that is why it initially failed.
However, while attempting to get it working, the host firewall blacklisted Gmail. They're currently looking into why, but the short answer is it is white-listed and working.

Could not connect smtp host wrong

I don't know what is wrong i am transferring my files to a laptop and i am having trouble sending mail in php mailer where check my phpinfo in the laptop it seems ok in fact same as the one in my desktop..
SMTP smtp.mail.yahoo.com smtp.mail.yahoo.com
smtp_port 25 25
the error is
Could not connect to smtp host wrong
ive opened the openssl extension
and the smtp is as u can see
in case you need here is my code
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host="smtp.mail.yahoo.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "******"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->From = "****";
$mail->FromName = "******";
$mail->AddAddress(******","*******");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Welcome to LilShop";
$mail->Body = ' Message ';
$mail->Send();
please help thanks
Add
$mail->Port=465;
$mail->SMTPSecure = "tls";
and then it should work. Yahoo SMTP requires port and tls. You can also check out the PHPMailer examples here: http://phpmailer.worxware.com/index.php?pg=exampleasmtp

PHP mailer error

I tried to use php mailer but errors as follows.
SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:
SMTP -> FROM SERVER:
SMTP -> ERROR: HELO not accepted from server:
SMTP -> ERROR: AUTH not accepted from server:
SMTP -> NOTICE: EOF caught while checking if connectedSMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
and my code
<?php
require("class.phpmailer.php")
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->Username = "admin#xxxxxxxxxxxx.in";
$mail->Password = "xxxxxxxx";
$mail->From = "admin#xxxxxxxxxxxx.in";
$mail->FromName = "Mailer";
$mail->AddAddress("xxxx#yahoo.co.in", "mine");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Here is the subject"
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send()) {
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
I was getting this due to wrong port for SSL.
SSL = 465
TLS = 587
See:
http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Some servers (especially shared hosting) will block you from using SSL with SMTP, I had the same problem once.
Either change host if you can, try using the default PHP mail() function or send through another mail server that does not require SSL e.g. port 25 not 465.
Something like AuthSMTP would be your best bet for an alternate mail server.
I had the same problems, it seems that we have
to set the SMPTSecure value.
First I changed the port from 465 to 587 and added:
$mail->SMTPSecure = "tls";
and it worked :)
If you are working in your localhost just go to the PHP Extention and enable or check the php_openssl
it will be able to access the SSL ports.
try this code
require 'PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
//$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "admin#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "admin123";
$mail->setFrom('admin3#gmail.com', 'development'); //add sender email address.
$mail->addAddress('admins#gmail.com', "development"); //Set who the message is to be sent to.
//Set the subject line
$mail->Subject = $response->subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = 'Name: '.$data['name'].'<br />Location: '.$data['location'].'<br />Email: '.$data['email'].'<br />Phone:'.$data['phone'].'<br />ailment: '.$data['ailment'].'<br />symptoms: '.$data['symptoms'];
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.gif');
//$mail->SMTPAuth = true;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Had the same issue, Change port No in opencart mail setting to 587 and works fine
May be because of fire wall?
If you can't sign in to Google Talk,
or you're receiving an error that
says, Could not authenticate to
server, check if you have personal
firewall software installed, or if
your computer is behind a proxy server
that requires a username and password.
http://www.google.com/support/talk/bin/answer.py?hl=en&answer=30998
I use the same script for several clients and only run into this problem when deploying to Amazon EC2 cloud providers (such as Openshift).
These are tried and tested settings in phpmailer:
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
'but' Google blocks these services as an 'anti-spam' / political maneuver, and this has caught me out because it works locally and on most hosting providers, there is nothing much you can do when they don't accept outbound messages from your hosts DNS / IP. Accept it and move on by looking for another smtp server to route messages through.
not sure but try $mail->Host = "smtp.gmail.com" =>$mail->Host = "smtp.google.com"

Categories