I tried sending email using smtp gmail using phpmailer. My gmail account got suspended saying that there is an unusual activity. Here is the code I have used for sending emails. What's the correct way of sending emails using smtp gmail in phpmailer?
My question is not duplicate. I have already tried this : Send email using the GMail SMTP server from a PHP page
I'm using phpmailer, and my accounts getting suspended.
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#exmaple.org';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'smtp';
public $SMTPAuth = true;
public $Username = 'username#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
Here is my php code :
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom('noreply#example.org');
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
Related
/*send email Using at Index.php page*/
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
//include 'classes/phpmailer/mail.php';
//function Send_Mail($to,$subject,$body)
$mail = new Mail();
//$mail->setFrom(SITEEMAIL);
//$mail->Debugoutput = 'html';
$mail->addAddress($to);
//$mail->setFrom('noreply#domain.com', 'noreply');
//$mail->addReplyTo('replyto#example.com', 'First Last');
$mail->subject($subject);
//$mail->IsHTML(true);
$mail->body($body);
$mail->send();
/*PHP Mailer Code used in mail.php*/
<?php
include('class.phpmailer.php');
class Mail extends PhpMailer
//function Send_Mail($subject,$body)
{
// Set default variables for all new objects
public $From = 'rssbmonthlyreport#gmail.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'isSMTP';
public $SMTPAuth = true;
public $Username = 'abc#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $Port = 587;
public $Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
public $CharSet = 'UTF-8';
public $Encoding = '8bit';
public $ContentType = 'text/html; charset=utf-8\r\n';
//public $From = 'rssbmonthlyreport#gmail.com';
//public $FromName = 'GMail Test';
public $WordWrap = 75;
public function subject($subject)
{
return $this->Subject = $subject;
}
public function body($body)
{
return $this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
I am Using these code above at Index page and this in email.php and email.php was included in config.But it is not working in place of this default mail() function running.....I tried every thing even the latest code of phpmailer its crashing..Anyone has idea so please help
This is wrong:
$Mailer = 'isSMTP';
It should be:
$Mailer = 'SMTP';
A better approach would be to call $this->isSMTP() from an overridden constructor.
I'd also recommend using the built-in html2text method if you're going to forcibly set AltBody like that.
If you're going to override things, read the code.
Here's an improved version:
class Mail extends PhpMailer
//function Send_Mail($subject,$body)
{
// Set default variables for all new objects
public $From = 'rssbmonthlyreport#gmail.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $SMTPAuth = true;
public $Username = 'abc#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $Port = 587;
public $Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
public $CharSet = 'UTF-8';
public $Encoding = '8bit';
//public $From = 'rssbmonthlyreport#gmail.com';
//public $FromName = 'GMail Test';
public $WordWrap = 75;
public function __construct($exceptions = null)
{
parent::__construct($exceptions);
$this->isSMTP();
$this->isHTML();
}
public function subject($subject)
{
return $this->Subject = $subject;
}
public function body($body)
{
return $this->Body = $body;
}
public function send()
{
$this->AltBody = $this->html2text($this->Body);
return parent::send();
}
}
}
which email you send
ex: gmail or hotmail
if ssl not present
hotmail automatically deletes email without even notifying. You have add exception for particular sender.
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#domain.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'isSMTP';
public $SMTPAuth = true;
public $Username = 'email#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'ssl';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
I am Using this Code for Email.php file and it works but it sends Mail in Attachment Not in Normal Form...
Email Attachment Showing in Email
Email Attachment Output
Here is link where i am using it for verification purposes.
http://monthlyreport.ultimatefreehost.in
At index.php I am using Like this
//send email
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom(SITEEMAIL);
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
Your functions should return the values.
Use return $this->Body = $body; at the end of body(), and similarly do it for the other functions as well.
I know this kind of question asked so many times in stackoverflow, but I looked at most of them and the problem still remains.
I'm using phpMailer to send mail from my site using a gmail account.
When I was using this in localhost, everything was fine and mails was sending correctly,
But as long as I uploaded my site in remote server, problem revealed itself.
There's connection timeout(110) error when I'm using port 465 and smtp.gmail.com, when I'm trying to use port 587 and tls there's error too.
Would you please let me know what's wrong?
The host php version is 5.4.
this is my code:
require_once ("../PHPMailer/class.phpmailer.php");
class mailSender
{
protected $address;
protected $username;
protected $password;
protected $cc;
protected $bcc;
protected $body;
protected $mailer;
public function __construct( $host, $port, $usn, $psw, $isSMTP = true )
{
$this->mailer = new PHPMailer(true);
//$this->mailer->Host = "mail.yourdomain.com"; // SMTP server
$this->mailer->Host = $host; //"smtp.gmail.com"; // sets GMAIL as the SMTP server
$this->mailer->Port = $port; //465; // set the SMTP port for the GMAIL server
if( $isSMTP == true )
{
$this->mailer->IsSMTP();
//$this->mailer->SMTPDebug = 1; // enables SMTP debug information (for testing)
$this->mailer->SMTPAuth = true; // enable SMTP authentication
$this->mailer->SMTPSecure = "ssl"; // sets the prefix to the servier
}
$this->mailer->Username = $usn; //"yourusername#gmail.com"; // GMAIL username
$this->mailer->Password = $psw; //"yourpassword"; // GMAIL password
$this->mailer->CharSet = 'UTF-8';
$this->mailer->IsHTML(true);
//set High priority to prevent going to SPAM folder
$this->mailer->Priority = 1;
$this->mailer->AddCustomHeader("X-MSMail-Priority: High");
$this->mailer->AddCustomHeader("Importance: High");
}//function __construct
public function send( $address, $senderName, $replyAddr, $replyName, $fromAddr, $fromName, $subject, $body, $attachment = false, $cc = false, $bcc = false )
{
try {
//$this->mailer->AddReplyTo( $replyAddr, $replyName);
$this->mailer->AddAddress($address, $senderName);
$this->mailer->SetFrom($fromAddr, $fromName);
$this->mailer->AddReplyTo($replyAddr, $replyName);
$this->mailer->Subject = $subject; //'PHPMailer Test Subject via mail(), advanced';
$this->mailer->AltBody = 'در صورتی که قادر به تماشای محتوای ای-میل نیستید از یک نمایشگر ای-میل تحت HTML استفاده کنید!'; // optional - MsgHTML will create an alternate automatically
$this->mailer->MsgHTML($body);
//$this->mailer->MsgHTML(file_get_contents('contents.html'));
//$this->mailer->AddAttachment('images/phpmailer.gif'); // attachment
//$this->mailer->AddAttachment('images/phpmailer_mini.gif'); // attachment
$send = $this->mailer->Send();
if( $send )
{
return true;
}
else
{
return false;
}
}//try send
catch( Exception $e )
{
return false;
}//catch
}//function Send
}//class mailSender
Thanks in Advance
Might be your gmail acct is locked. Try the unlock captcha:
https://accounts.google.com/DisplayUnlockCaptcha.
Also check your account setting called Allow less secure apps here:
https://myaccount.google.com/security#connectedapps
I am using ubuntu on my virtual machine. I would like to send emails using catchmail as described here: http://berk.es/2011/05/29/mailcatcher-for-drupal-and-other-php-applications-the-simple-version/
I am trying to send emails like that:
//Mailer class:
class Mailer extends PHPMailer
{
public $UTF8Encode = false;
public function __construct($param = null)
{
parent::__construct($param);
$this->Mailer = 'sendmail';
$this->Sendmail = 'smtp://localhost:1025';
$this->From = 'xxxx#xxxx.com';
$this->FromName = 'Support';
$this->WordWrap = 50;
$this->CharSet = 'UTF-8';
}
}
....etc....
And:
//Sending emails
$mail = new Mailer();
$mail->Body = "xxxx";
$mail->Subject = "xxx";
$mail->From = 'xxxx#xxxx.org';
$mail->FromName = 'Support';
$mail->WordWrap = 50;
$mail->AddAddress(xxxx#xxxx.com);
And I am getting the error:
Could not execute: smtp://localhost:1025
$this->Mailer = 'sendmail';
$this->Sendmail = 'smtp://localhost:1025';
The problem with this is you're telling PHPMailer to use a command line program called sendmail instead of using smtp. And PHPMailer tries to do something like:
exec("smtp://localhost:1025 --args-and-stuff");
And as you can tell this won't work.
To tell PHPMailer to use smtp you need to do the following:
$this->Mailer = 'smtp';
$this->Host = 'localhost';
$this->Port = 1025;
If your SMTP server needs Authentification you can do that as follows:
$mail->SMTPAuth = true;
$mail->Username = "yourname#yourdomain";
$mail->Password = "yourpassword";
I'm using PHPmailer to send emails and I've created a function that prepares the email and sends it. If I try to use this function more than once in a script it stops the script execution when it trys to send a second email using the same function.
my function:
public static function sendEmail($from, $fromName, $to, $subject, $body){
require("includes/class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->IsSMTP(true);
$mailer->Host = 'ssl://smtp.gmail.com:465';
$mailer->SMTPAuth = true;
$mailer->Username = 'removed';
$mailer->Password = 'removed';
$mailer->From = $from;
$mailer->FromName = $fromName;
$mailer->AddAddress($to);
$mailer->Subject = $subject;
$mailer->Body = $body;
$mailer->WordWrap = 100;
if ($mailer->Send()) {
return true;
} else {
return false;
}
}
Why is this happening? Is it anything to do with creating a new phpmailer object each time?
I think you should use require_once() instead of require