PHP mail function not working in cpanel using G Suit - cpanel

I want to submit my form on email but PHP mail function not working in my hostgator cloud server, i am using G Suit on this server. Please help

You can use PHPMailer instead of server mailing as send emails from php not work in most host providers so PHPMailer solve your problem as you can send email over SMTP from php
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//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 = "name#gmail.com";
$mail->Password = "super_secret_password";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "name#gmail.com";
$mail->FromName = "Full Name";
$mail->addAddress("name#example.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent successfully";
}
?>
PHPMailer docs https://github.com/PHPMailer/PHPMailer/wiki/Tutorial

Related

Different smarthosts for different domains with different credentials

Hello I have two (or maybe more later) domains:
domain1
domain2
I want to configure Exim (cPanel) to use SendGrid's or Mailgun SMTP servers, currently I'm trying with this config in Exim:
**Section: TRANSPORTSTART**
domain1_smtp:
driver = smtp
hosts = smtp.mailgun.org
hosts_require_auth = smtp.mailgun.org
hosts_require_tls = smtp.mailgun.org
domain2_smtp:
driver = smtp
hosts = smtp.mailgun.org
hosts_require_auth = smtp.mailgun.org
hosts_require_tls = smtp.mailgun.org
**Section: AUTH**
domain1_login:
driver = plaintext
public_name = LOGIN
client_send = : postmaster#mg.domain1.com : password
domain2_login:
driver = plaintext
public_name = LOGIN1
client_send = : postmaster#mg.domain2.com : password
**Section: PREROUTER**
send_via_domain1:
driver = manualroute
domains = ! +local_domains
senders = *#domain1.cm
transport = domain1_smtp
route_list = "* smtp.mailgun.org::2525 byname"
host_find_failed = defer
send_via_domain2:
driver = manualroute
domains = ! +local_domains
senders = *#domain2.com
transport = domain2_smtp
route_list = "* smtp.mailgun.org::2525 byname"
host_find_failed = defer
When I'm sending email from user#domain1.com I'm getting messages delivered by postmaster#mg.domain1.com and when I'm sending from user#domain2.com I'm getting messages delivered also from postmaster#mg.domain1.com.
I want to have smarthost for every domain with different credentials. Thanks
I have these setup (VPS + WHM/cPanel + Exim + Mailgun) and after doing some online research, I've found a few helpful websites regarding this topic and managed to come out with the correct configuration. Below are the solutions that I'm currently using on my VPS and hope it will help you as well. It should solve your "via" problem and might solve the intermittent "550 5.7.1 Relaying denied" error from Mailgun as well:
Go to the "Exim Configuration Editor" in WHM. Choose "Advanced Editor" and insert the configuration below:
Section: AUTH
mailgun_login:
driver = plaintext
public_name = LOGIN
hide client_send = ": ${extract{login}{${lookup{$sender_address_domain}lsearch{/etc/exim_mailgun}{$value}fail}}} : ${extract{password}{${lookup{$sender_address_domain}lsearch{/etc/exim_mailgun}{$value}fail}}}"
Section: ROUTERSTART
mailgun:
driver = manualroute
domains = ! +local_domains
transport = mailgun_transport
route_list = "* smtp.mailgun.org::587 byname"
host_find_failed = defer
no_more
Section: TRANSPORTSTART
mailgun_transport:
driver = smtp
hosts = smtp.mailgun.org
hosts_require_auth = smtp.mailgun.org
hosts_require_tls = smtp.mailgun.org
Then create a file named /etc/exim_mailgun and insert the content similar to the structure below (Replace it with your Mailgun's domain login credentials that was verified):
domain1.com: username=postmaster#mg.domain1.com password=abcdefghi
domain2.com: username=postmaster#mg.domain2.com password=jklmnopqr

PHPMailer does not provide ErrorInfo

I have a problem with PHP Mailer, which is not providing the $mail->ErrorInfo when an error occured.
I tested with the original example from [http://phpmailer.worxware.com/?pg=tutorial#1] as below.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "from#example.com";
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I've set the host to my server, modified "from" and "AddAddress" to correct addresses and I've received the test mail as expected. But whhen I change the recipient address to blxxxa#blablaxxxx.de, just to check how the errors will be handled I don't get the error.
$mail->AddAddress("blxxxa#blablaxxxx.de");
I still receive "Message has been sent". Any Idea? Maybe server settings?
Try putting your $mail->Send() into a try-catch block.
try{
// ... Your Setup ...
$mail->Send();
}
catch (phpmailerException $e) {
echo $e->errorMessage(); //PHPMailer error messages
}
catch (Exception $e) {
echo $e->getMessage(); // other error messages
}
if you look at the code of the phpmailer library ( phpmailer library on github search for the public function send() code block )
you'll see that phpmailer throws exceptions in case of failure.
You have some good examples here : http://www.merocode.com/sending-emails-using-phpmailer-via-smtp/
good luck.
ErrorInfo will not contain an error message unless an error happens. It sounds like your mail server is accepting the message without complaining (as would be expected if it's a relay or on localhost), so you need to check your mail server logs and your bounce mailbox since the problem is further upstream from you and thus not visible to PHPMailer.
In short, you're not doing anything wrong, you're just looking in the wrong place.
Provide profer hostname, username and password. for example,
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.example.com"; // SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password";
$mail->From = "from#example.com";
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
In my case PhpMailer causes a warning at $mail->send();
Warning: stream_socket_enable_crypto(): Peer certificate CN=mail.xxx.com' did not match expected CN=mail.yyy.com'
in class.smtp.php on line 368
So imho PhpMailer misses some exception handing here.
In such a case you have the following possibilities:
Create an issue in the ticket system https://github.com/PHPMailer/PHPMailer/issues and hope for an update
add a "#" to the function call #$mail->send(); - which hides the warning, but does not help you further
Work with the PHP buffer to read out the warning.
Update:
In PHPMailer 6.x the warning was removed - but $mail->ErrorInfo does not contain any useful information. All I get is "SMTP connect() failed". Not really an improvement...

Not able to send mail using YiiMailer

I am trying to send a mail using YiiMailer Extension, when I used Port 465 getting error "fwrite(): send of 16 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host" and when I used port 587 its fails to send a mail.
Code:
In controller:
$mail = new YiiMailer();
$mail->setData(array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form', 'mail' => $mail));
$mail->setFrom('abc#gmail.com', 'John Doe');
$mail->setTo($_POST['UserLogin']['email']);
$mail->setSubject('Reser Password');
$mail->setBody('Simple message');
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "*******#gmail.com";
$mail->Password = "********";
if ($mail->send()) {
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
Yii::log("Mail sent");
} else {
Yii::app()->user->setFlash('error','Error while sending email: '.$mail->getError());
Yii::log("Mail Error");
}
}
In main.php
'import'=>array(
'application.models.*',
'application.components.*',
'ext.YiiMailer.YiiMailer',
),
Please help me to come out of this problem and I tried other extension too like Emailer, PHPMailer but there i got some SMTP error "smtp unable to connect to the remote server"
Thanks in advance
I am guessing you do not have php_openssl enabled in your server's php.ini. Check phpinfo() to make sure you have it enabled.
Also you may need to add the lines
$mail->SMTPSecure='ssl';
$mail->Mailer='smtps';
This works for me!
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure= 'tls';
$mail->Username = "*******#gmail.com";
$mail->Password = "********";

When smtp mail returns a "password not accepted from server"

You get this error from phpmailer.
"Password not accepted from server: 535 Incorrect authentication data"
What causes this?
You must make sure that the Username given for the log-on is the same as the SetFrom email address.
This will return an error:
$mail->Username = "myemailaddress#gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->SetFrom('mysetfromaddress#gmail.com', 'Pagelinks');
This should not.
$mail->Username = "mysetfromaddress#gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->SetFrom('mysetfromaddress#gmail.com', 'Pagelinks');
When the Username and SetFrom do not match the authentication fails (according to an article found on Google).

error send email with phpmailer in server domain but succed in localhost

i have detail error with this problem,
SMTP -> FROM SERVER:220-server.modulindo.com ESMTP Exim 4.77 #2 Wed, 11 Jul 2012 10:57:22 +0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
SMTP -> FROM SERVER: 250-server.modulindo.com Hello mail.modulindo.com [202.67.9.42] 250-SIZE 52428800 250-PIPELINING 250-AUTH PLAIN LOGIN 250 HELP
SMTP -> ERROR: Password not accepted from server: 535 Incorrect authentication data
SMTP -> FROM SERVER:250 Reset OK
please help me guys!?
i have a problem with phpmailer. i send email with phpmailer in localhost is succeed, but when i upload it in my server domain, there was an error happend. the error is..
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
this is my script..
....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "wasis85#gmail.com";
$mail->Password = "password";
$mail->From = "wasis85#gmail.com";
$mail->FromName = "Wasis Lukito";
$mail->AddAddress($ema[$ari_no],"wasis");
$mail->AddCC("wasisl85#yahoo.com");
$mail->AddReplyTo("wasisl85#yahoo.com","Wasis Lukito");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Penolakan Data BPLPSE";
$mail->Body = "Alasan di tolak ";
$mail->AltBody = "This research is supported by Google.com";
...
i solved same problem with comment (or cancel) this line
// $mail->IsSMTP();
this because from some server i had same error: SMTP Error: Could not authenticate (also Password is incorrect...etc)
The script seems to be fine. I believe you have to check and make sure if your server supports SMTP or it has been properly configured for SMTP or not.