It's taken me days to get the right settings so I thought I would post a php script that works on Bluehost. In initial tests using isSMTP is faster than isMAIL.
<?php
require_once '../includes/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "box1311.bluehost.com"; // specify bluehost as outgoing server
$mail->SMTPSecure = "tls"; // sets the prefix to the server do not use ssl
$mail->SMTPDebug = 3; // comment out if you don't need debug info
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "USER#EXAMPLE.COM"; // SMTP username (your email account)
$mail->Password = "PASSWORD"; // SMTP password
$mail->Port = 25;
$mail->From = 'USER#EXAMPLE.COM';
$mail->FromName = "USER#EXAMPLE.COM";
$mail->AddAddress('CLIENT#gmail.com');
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = 'test message';
$body = '<!DOCTYPE html>
<html><header>
</header>
<body lang=EN-US>
<div style="text-align:center">
<h2>this is a test</h2>
</div>
</body>
</html>';
$mail->Body = $body;
$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;
}else{
echo '<h1>message sent</h1>';
}
?>
This code work for me.
include "phpmailer/class.phpmailer.php";
include "phpmailer/class.smtp.php";
$email_user = "email#host.com";
$email_password = "pass123";
$the_subject = "Title";
$from_name = "Sender";
$phpmailer = new PHPMailer();
// ---------- datos de la cuenta de correo -----------------------------
$phpmailer->Username = $email_user;
$phpmailer->Password = $email_password;
//---------------------------------------------------------------------
$phpmailer->SMTPSecure = 'tls';
$phpmailer->Host = "box6171.bluehost.com";
$phpmailer->Port = 26;
//$phpmailer->SMTPDebug = 2;
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;
$phpmailer->setFrom($phpmailer->Username,$from_name);
$phpmailer->AddAddress("to#host.com");
$phpmailer->Subject = $the_subject;
$phpmailer->Body .="<h1 style='color:#3498db;'>Attachment:</h1>";
$phpmailer->Body .= "<h3>".$attach1."</h3>";
$phpmailer->AddAttachment($attach, "attach1");
$phpmailer->AddBCC("hidecopy#host.com", "bcc1");
$phpmailer->IsHTML(true);
$enviado = $phpmailer->Send();
if($enviado) {
echo 'email send successful';
}
2022 update
$phpmailer->Host = [your fully qualified domain]
$phpmailer->Port = 465
$phpmailer->SMTPSecure = 'ssl'
Important that $phpmailer->From and $phpmailer->Username must be the same.
Related
I designed my website and hosted on my amazon ec2 instance and I bought my domain in godaddy (www.mydomain.com).Now I want a mail configuration in my contact form page in website.. Below its my code , I don't know where am I mistake the code?
<?php
if(isset($_REQUEST['submit']))
{
try
{
$name = $_POST['name'];
echo "<script type='text/javascript'>alert('$name')
</script>";
$email = $_POST['email'];
echo "<script type='text/javascript'>alert('$email')
</script>";
$subject = $_POST['subject'];
echo "<script type='text/javascript'>alert('$subject')
</script>";
$message = $_POST['message'];
echo "<script type='text/javascript'>alert('$message')
</script>";
$response ="";
$body = <<<EOD
<div style='font-size:18px'>
<b> Name </b> : $name <br />
<b> Email address </b> : $email <br />
<b>Message </b> : $message <br />
</div>
EOD;
$to = "XXXXX#gmail.com";
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.phpmailer.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.smtp.php');
$mail = new PHPMailer(true);
//$mail->Host = "relay-hosting.secureserver.net"; // your SMTP Server
// echo $res;
$mail->IsSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPDebug=true;
$mail->SMTPAuth = true; // Auth Type
$mail->Port = 25;
$mail->IsSendmail();
//$mail->SMTPSecure = "ssl";
$mail->Username = "support#mydomain.com";
$mail->Password = "******";
$mail->Sender = "supportexample#mydomain.com";
$mail->From = "supportexample#mydomain.com";
$mail->AddReplyTo($email);
$mail->FromName = "Example";
$mail->AddAddress($to);
//$mail->AddAddress("desired recipient no.2 optional");
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body=$body;
$mail->WordWrap = 50;
$mail->Send();
echo "<script type='text/javascript'>alert('Mail Send Successfully')
</script>";
}
catch (phpmailerException $e) {
echo "<script type='text/javascript'>alert('Failed')
</script>";
echo $e->errorMessage();
}
}
?>
It gives an error
Could not execute: /var/qmail/bin/sendmail
Try This One. This Might Helpful. You Have To Use Different Email For From Mail.
Instead Of Using
$mail->From = "support#mydomain.com"
You Have To Use Another Email Here :
$mail->From = "supportexample#mydomain.com";
require_once('phpmailer/class.phpmailer.php');
require_once('phpmailer/class.smtp.php');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com"; // SMTP HOST
$mail->SMTPAuth = true;
$mail->Username = "support#mydomain.com"; // SMTP username
$mail->Password = "********"; // SMTP password
$mail->SMTPSecure = "ssl"; // Enable TLS encryption, `ssl` also accepted
$mail->Port = "465"; // TCP port to connect to 587
$mail->From = "supportexample#mydomain.com";
$mail->FromName = "Domain Example";
$mail->addAddress("XXXX#gmail.com");
$mail->addReplyTo("supportexample#mydomain.com");
$mail->isHTML(true);
$mail->Subject = "Hello Test";
$mail->Body = "Test Message Working With Us";
if($mail->send()){
return true;
}
else{
return $mail->ErrorInfo;
}
i have a problem with my PHPmailer function.
I want my registration form to send a mail to every new registered member.
I get this error code. SMTP Error: Could not authenticate.
I have contacted my webhost, and they say that i have the right username,password, smtp server etc.
include "class.smtp.php";
include "class.phpmailer.php";
$Host = "mail.smtp.com"; // SMTP servers
$Username = "support#mymail.com"; // SMTP password
$Password = "mymailpassword";
$From = "support#mymail.com";
$FromName = "My name";
$ToEmail = $mail;
$ToName = $username;
$header = str_replace('%homepage_url%', $hp_url, $_language->module['mail_subject']);
$Message = str_replace(Array('%nickname%', '%username%', '%password%', '%activationlink%', '%pagetitle%', '%homepage_url%'), Array(stripslashes($nickname), stripslashes($username), stripslashes($pwd1), stripslashes($validatelink), $hp_title, $hp_url), $_language->module['mail_text']);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($ToEmail , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $header;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($Message);
$mail->CharSet="UTF-8";
if(!$mail->Send()){
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
} else {
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
}
I'm trying to send a email using gmail's smtp (see below code) but I'm getting "Username and Password not accepted" error.
I've tried:
this link
allow less secure apps
ssl is enabled
enable imap/pop on gmail's settings
a different login
none of them works.
Here's the PHP code:
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
Update: Here's the full error message (with DebugMode = 4)
After testing I got this to work by changing the host to include the tls prefix.
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->WordWrap = 900;
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
// Define o remetente
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
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");
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?