I am trying to send mail via SMTP using PHPMailer class. My problem is , for the first attempt , the mail sender is working oerfect , but for all subsequent attempts it's throwing error :
SMTP -> NOTICE:
EOF caught while checking if connected
My email sending code is :
function sendEmail($toAddress,$fromAddress,$toName,$fromName,$subject,$emailContent,$content_type = false, $attach_path="", $cc = '', $cc_name="")
{
require_once('phpmailer/class.phpmailer.php');
if (empty($content_type)) $content_type = false;
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = MY_SMTP_AUTH; // turn on SMTP authentication
$mail->Host = MY_SMTP_HOST_NAME;
if (!empty($this->smtpEncryptionMode))
{
$mail->SMTPSecure= $this->smtpEncryptionMode;
}
if (!empty ($this->smtpPort))
{
$mail->Port = MY_SMTP_PORT;
}
else $mail->Port = 25;
$mail->Username = $this->smtpUserName;
$mail->Password = $this->smtpUserPassword;
$mail->From =$fromAddress;
$mail->FromName = $fromName;
if(is_array($toAddress))
{
foreach($toAddress as $to)
{
$mail->AddAddress($to, "" );
}
}
else
{
$mail->AddAddress($toAddress, $toName );
}
$mail->AddReplyTo($fromAddress, $fromName );
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 80; // set word wrap to 80 characters
$mail->IsHTML($content_type); // set email format to basic
$mail->Subject = $subject;
$mail->Body = $emailContent;
//Here it sets other parameters e.g attachment path etc.
$mail->SMTPDebug = true;
$result = $mail->Send();
if($result == false ) { $result = $mail->ErrorInfo;echo $result; }// Switch this on when debugging.
return $result;
Why is it throwing the error for all successive attempts?
From , what I can infer from class.smtp.php is that it is failing inside a function Connected() which actually checks the socket status of the smtp_connection instance, and there it is getting EOF.
I guess the connection itself isnot getting established...But what is going right in the first instance then?
Is the function in a while loop and if so you need to close the class. Might be to easy try this.
$result = $mail->Send();
$mail->close();
you can't send to mail instances at once for example you call test_mail() and then emailer() function test_mail uses another connection while the other function uses phpmailer and is connected to port 465. Now emailer will throw an EOF error because the connection on the first function is not yet exited.
/*function test_mail(){
$to = 'emil.nrqz#gmail.com';
$subject = 'Test email using PHP';
$message = 'This is a test email message'; $headers = 'From: webmaster#example.com' . "\r\n" . 'Reply-To: webmaster#example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers, '-fwebmaster#example.com');
}*/
function emailer($sendby,$subject,$body,$sendto,$cc){
require("../obj/PHPMailer-master/class.phpmailer.php");
$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->Host = "relay-hosting.secureserver.net";
$mail->Port = 25; // or 587 or 465
/*
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = "smtpout.secureserver.net";
$mail->Port = 465; // or 587 or 465
*/
//var_dump($mail);exit();
$mail->IsHTML(true); // sends email as html
$mail->Username = MAILER_USERNAME; // mailserver username
$mail->Password = MAILER_PASSWORD; // mailserver password
$mail->SetFrom("info#file-bird.com"); // Seen as message from
$mail->Subject = $subject; // Subject of the message
$mail->Body = $body; // email body - can be html
$mail->AddAddress($sendto); // where email will be sent
$mail->addCC($cc);
if(!$mail->Send()){
return "Mailer Error: "; // . $mail->ErrorInfo
}
else{
return "Message has been sent";
$mail->close();
}
}
Related
I am using PHPMailer for sending an email. Emails are getting proper but whenever I am opening email which I was sent using PHP Mailer I am getting a warning message.
Note: If I remove anchor tag from $phpMailerText then I am not getting any warning.If I add anchor tag then I am getting warning.Would you help me in this?
require 'mail/PHPMailerAutoload.php';
$to = $email;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Headers
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-mailer: smtp.gmail.com" . "\r\n" // this will identify the real sender
. "Precedence: bulk" . "\r\n" // this will say it is bulk sender
. "List-Unsubscribe:abc#gmail.com\r\n" // this will reveal the OPT-OUT address
. "Reply-To: $to\n"
. "To: $to\n"
. "From: $to\n";
//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 = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "abc#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "****";
//Set who the message is to be sent from
$mail->setFrom('abc#gmail.com', 'code');
//Set an alternative reply-to address
$mail->addReplyTo('abc#gmail.com', 'code');
//Set who the message is to be sent to
$mail->addAddress($to, 'Customer');
//Set the subject line
$mail->Subject = 'code';
$phpMailerText="<!DOCTYPE HTML><html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
$mail->msgHTML($phpMailerText);
//Replace the plain text body with one created manually
$mail->AltBody = ' ';
//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else {
}
Try the following code and do the other steps.
Create reverse dns record
Configure SPF records
$to = $email;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Headers
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-mailer: YOUR_SITE_DOMAIN Server" . "\r\n" // this will identify the real sender
. "Precedence: bulk" . "\r\n" // this will say it is bulk sender
. "List-Unsubscribe:info#YOUR_SITE_DOMAIN\r\n" // this will reveal the OPT-OUT address
. "Reply-To: $email\n"
. "To: $email\n"
. "From: $email\n";
$mail->addCustomHeader( $headers );
//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 = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "me#companydomain.com";
//Password to use for SMTP authentication
$mail->Password = "****";
// Because html is being used
$mail->isHTML(true);
//Set who the message is to be sent from
$mail->setFrom('me#companydomain.com', 'code');
//Set an alternative reply-to address
$mail->addReplyTo('me#companydomain.com', 'code');
//Set who the message is to be sent to
$mail->addAddress($to, 'Customer');
//Set the subject line
$mail->Subject = 'code';
$phpMailerText="<!DOCTYPE HTML><html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
$mail->msgHTML($phpMailerText);
//Replace the plain text body with one created manually
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else {
}
Please use following code and tell me what happends.
$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("email#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am using mail() function to sent mails when an event happening. But it is not working as I expected. I tried to get the return of the function also. Some one please suggst what may be the issue.
$msg = "Your password has been changed.is'".$data['password']."'";
$to = $data['email'];
$subject = "password changed";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info#hia.com';
$send = mail($to, $subject, $msg, $headers);
if($send){
echo "successful";
}
else{
echo "error";
}
Problems arising from the php setting. Closing function for security by some service provider. You must send mail with SMTP.
Example several SMTP class on github;
hujuice/smtp - snipworks/php-smtp - PHPMailer/PHPMailer
Example code a PHPMailer according to your code;
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#hia.com'; // SMTP username
$mail->Password = 'yourmailpassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('info#hia.com', 'Mailer');
$mail->addAddress($data['email']); // Name is optional
$mail->addReplyTo('info#hia.com', 'Information');
$mail->addCC('cc#hia.com');
$mail->addBCC('bcc#hia.com');
$mail->CharSet = 'ISO-8859-1';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'password changed';
$mail->Body = 'Your password has been changed.is "'.$data['password'].'"';
if(!$mail->send()) {
echo 'error';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'successful';
}
Don't use simple mail(), i prefer for PHPmailer function.
Here is a Example.
First - download phpmailer in your project directory.
second - create send_mail.php in your directory(you can change you name).
send_mail.php file code.
require_once "PHPMailer/PHPMailerAutoload.php";
$mail = new PHPMailer(true);
if (!isset($_POST['send'])) {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the enquiry form!";
die;
}
$to="abc#xyz.com";
$senderName = "xyz";
//send the mail
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$zcode = $_POST["zcode"];
$email = $_POST["email"];
$phone = $_POST["phone"];
//Enable SMTP debugging.
$mail->SMTPDebug = 0;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "abc#xyz.com";
$mail->Password = "########";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = $to;
$mail->FromName = $senderName;
$mail->addAddress($to, $senderName);
$mail->addReplyTo($email, $name);
$mail->isHTML(true);
$mail->Subject = "bfuiebfiaif";
$mail->Body = "as per your needs";
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
die;
}
header('Location: index.php');
die;
i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");
This is my php code to send a confirmation email to a new user and the problem is, i am not receiving it(i have tried different ones, no luck)
function send_email($info)
{
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('noreply#localhost' => 'localhost'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
I get no errors
Install phpmailer.
Here is a sample on how to send mail with PHP mailer
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
If your destination address is #hotmail.com it may never income because they have a strong anti-spam management.
I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?