Issue with Moving phpmailer Page from Localhost to Server - php

I have been developing a page that will automatically send emails from a Gmail account to mail accounts of various origin for specific users of my site. I have this working perfectly on Localhost. The mails are received by the users and also deposited in the "Sent Items" of the Gmail account.
However, as soon as I migrate this to the server we are using it no longer works. The code is as follows :-
$mail = new PHPMailer();
try
{
$mail->CharSet = "UTF-8";
// telling the class to use SMTP
$mail->IsSMTP();
// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPDebug = 0;
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the servier
$mail->SMTPSecure = "tls";
// sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";
// set the SMTP port for the GMAIL server
$mail->Port = 587;
// GMAIL username
$mail->Username = "xxxxxxx#gmail.com";
// GMAIL password
$mail->Password = "P4ssw0rd";
//Set reply-to email this is your own email, not the gmail account
//used for sending emails
$mail->SetFrom($from);
$mail->FromName = "XXX";
$mail->AddReplyTo('xxxxxxx#gmail.com' , 'XXX');
// Mail Subject
$mail->Subject = $subject;
//Main message
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
$mail->Send();
}
catch (phpmailerException $e)
{
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
echo $e->getMessage(); //Boring error messages from anything else!
}
Setting the Debug to 1 on localhost produces the expected errors if something goes wrong. However, nothing seems to happen on the Server. Please help.

Related

How do I delay sending mails with PHPMailer for multiple days?

I'm trying to create a mailing system for our library. People receive an e-mail when they requested a book. However, I want them to receive an e-mail one day before the hand-in date as well.
So I'm not looking for a sleep script code, I'm looking for a code that can maybe store the data on the server until the hand-in date arrived.
I'm using Joomla 3.5.1 for our website, this is the code for the PHPMailer:
jimport('joomla.mail.mail');
try{
$mail = new PHPMailer();
$body = $mailmsgadmin;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "example.host.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 = "ssl"; // sets the prefix to the servier
$mail->Host = "example.host.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "email"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->SetFrom('email', 'First Last');
$mail->AddReplyTo($mailuseremail, $mailusername);
$mail->Subject = $mailsubject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("Users e-mail","Users name");
if(!$mail->Send()) {
JError::raiseWarning( 'SOME_ERROR_CODE', $mail->ErrorInfo);
}
} catch (Exception $e) {JError::raiseWarning( 'SOME_ERROR_CODE', $e->getMessage());}
Instead of trying to get PHPMailer delay the sending, add your email details to a database table. And then write a cron job that checks the table for emails that need to be sent.

Phpmailer not in Hotmail Inbox or Spam?

So i thought that if you send email to hotmail it would at least arrive in spam but in this case it does not. So have i done something wrong in this code below? I tested the same code with Gmail and it works Perfectly. Sure i think its related to Hotmail Filters.
Things to note, I have setup the email address on my domain and i have an SPF Record setup.
What could be wrong? And how can i check it?
Current PHP:
<?php
require 'PHPmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = true;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtpout.secureserver.net"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "username"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->setFrom('email', 'Immortal Insurance');
$mail->AddAddress('sendtoemail');
$mail->AddAddress('sendtoemail');
$mail->Body = 'This is a test Email';
$mail->AddAttachment('tosend/policy.pdf');
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
?>

PHPMailer not sending, but no errors

I created a php application at dan.creativeloafing.com. This just takes form data and builds an html page with it, then emails the contents of that page to dan#creativeloafing.com. A couple days ago, it stopped working. I have been trying to figure this out ever since. I was using the mail() php function and have switched it over to the PHPMailer Library. This is supposedly sending the emails and I get confirmation, but nobody ever recieves the email and I get no bounceback or any errors. This is the jist of the code:
//PHPMailer
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPSecure = 'tsl'; // Enable encryption, 'ssl' also accepted
$mail->Username = 'dan#omgsurvey.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPDebug = 0;
$mail->WordWrap = 50;
$mail->From = 'dan#omgsurvey.com';
$mail->FromName = 'DAN Application';
$mail->addAddress('dan#creativeloafing.com'); // Name is optional
$mail->addReplyTo($repEmail);
$mail->addCC('david.miller#creativeloafing.com');
$mail->isHTML(true);
$mail->Subject = "New DAN Request: ".$campaignName;
$mail->msgHTML(file_get_contents('./tmp/DAN_REQUEST_'.$specialString.$randomNumber.'.html'));
if(!$mail->send()) {
echo '<br />Proposal could not be sent.<br />';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Proposal has been sent';
}
The script always reaches 'Proposal has been sent.' too. This is driving me crazy!
So what was happening here is that godaddy was blocking emails that included my domain name in them. I am not sure if this was a spam issue, but they are currently looking into it. I have gotten the emails to send using a simple mail() function and by removing any references to omgsurvey.com in the email. Silly, this mail was only ever sent to two email addresses!
Isn't:
$mail->SMTPSecure = 'tsl';
supposed to be:
$mail->SMTPSecure = 'tls';
Use try...catch and PHPMailer(true); https://github.com/Synchro/PHPMailer/blob/master/examples/exceptions.phps
//Create a new PHPMailer instance
//Passing true to the constructor enables the use of exceptions for error handling
$mail = new PHPMailer(true);
try {
mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPSecure = 'tsl'; // Enable encryption, 'ssl' also accepted
$mail->Username = 'dan#omgsurvey.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPDebug = 0;
$mail->WordWrap = 50;
$mail->From = 'dan#omgsurvey.com';
$mail->FromName = 'DAN Application';
$mail->addAddress('dan#creativeloafing.com'); // Name is optional
$mail->addReplyTo($repEmail);
$mail->addCC('david.miller#creativeloafing.com');
$mail->isHTML(true);
$mail->Subject = "New DAN Request: ".$campaignName;
$mail->msgHTML(file_get_contents('./tmp/DAN_REQUEST_'.$specialString.$randomNumber.'.html'));
$mail->send();
echo 'Proposal has been sent';
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thinking send() should be Send()
if(!$mail->Send()) {

Before it works but now it does not work .SMTP Error: Could not connect to SMTP host

<?php
require_once('././php_mailer/class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = 'tls://mail.sipradi.com.np'; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = 'tls://mail.sipradi.com.np'; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = 'user_name'; // SMTP account username
$mail->Password = 'pass'; // SMTP account password
$mail->AddAddress('something#sipradi.com.np', 'Full Name');
$mail->SetFrom('user_name', '<noreply#sipradi.com.np>');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($message);
if($mail->Send()){
echo "Message Sent OK<p></p>\n"; die;
}else{
echo 'fail'; die;`
`
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
check password and user name
check another settings: port, etc.
check header info
etc.
I think the problem is the port setting.
From the documentation $Host
$Host
SMTP hosts.
Either a single hostname or multiple semicolon-delimited hostnames. You can also specify a different port for each host by using this format: [hostname:port] (e.g. "smtp1.example.com:25;smtp2.example.com"). Hosts will be tried in order.
and $SMTPSecure
$SMTPSecure
The secure connection prefix.
Options: "", "ssl" or "tls"
This means, that your SMTP server
$mail->Host = 'tls://mail.sipradi.com.np'; // SMTP server
should be
$mail->Host = 'mail.sipradi.com.np'; // SMTP server
If you want to use TLS
$mail->SMTPSecure = 'tls';
and last but not least, you might also try a different port 587, either by setting
$mail->Port = 587;
or in the setting for the SMPT server
$mail->Host = 'mail.sipradi.com.np:587'; // SMTP server

how to use php mail on fedora

I'm running fedora core 9 with php 5.2.6. I want to send a simple email from a form using the php mail command.
Do I need to configure my system to be an smtp server? If so do I have to load something like pear?
I've read that pear is installed by default. however when I go to /usr/lib/php/pear , the folder is empty.
When I go to install pear with yum install php-pear none of the mirrors are available.
Does anyone have any suggesions on what mail service to use? If it's installed by default and where I can figure out how to configure.
Thanks!
Joe
This the code I'm using:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$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 = "joestestemail#gmail.com"; // GMAIL username
$mail->Password = "joeiscool"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('joestestemail#gmail.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('joe12345#mailcatch.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
I would strongly recommend using a library like PHPMailer to send emails.
You can use it with the server's own mail service or with any other server on the internet (your ISPs, Gmail, etc).
For a better idea, check this example from their website:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Edit:
Following up on your comment..
For a simple GMail example, try this:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$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 = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('name#yourdomain.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Edit2:
The reason why you're getting this error is because you have an extra ' on your string:
Replace
$mail->Host = "'smtp.gmail.com";
with:
$mail->Host = "smtp.gmail.com";
The easiest way to get mail sending up and running is to install and configure postfix. You can use:
yum install postfix
And check out the documentation here:
http://www.postfix.org/docs.html
You can use the LAMPStack from BitNami. It comes with an easy installer that installs everything you need and you'll be running in minutes.

Categories