So I have a contact form that uses phpmailer. It sends an email from one Gmail account to another. But I can't seem to get the receiving email to receive any emails.
The script is hosted on a cpanel (RivalHost) and the domain is on GoDaddy. I asked RivalHost if they were blocking SMTP connections or blocking ports 587 or 465, and they said they weren't. So I have no idea what's causing the issue. The script works perfectly fine on my localhost, just not on cpanel
Heres the mailing script:
<?php
$result="";
if(isset($_POST['submit'])){
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host='smtp.gmail.com';
$mail->Port=465;
$mail->SMTPAuth=true;
$mail->SMPTSecure='ssl';
$mail->Username='sendingemail#gmail.com';
$mail->Password='*********';
$mail->setFrom('sendingemail#gmail.com');
$mail->addAddress('receivingemail#gmail.com');
$mail->addReplyTo($_POST['email'],$_POST['name']);
$mail->isHTML(true);
$mail->Subject='Contact: '.$_POST['subject'];
$mail->Body='Message: '.$_POST['msg'].'</h1>';
if(!$mail->send()){
$result='something went wrong';
echo $result;
} else {
$result="thank you";
echo $result;
}
}
?>
I was also told to check my MX records, but wasn't really sure what to change them to, or if I needed to change them at all:
MX 0 ********.com 3599 RBL
Solution 1:
PHPMailer uses exeptions. You can put your code in try/catch block to get exceptions caused emails not to be sent.
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
//Email information comes here
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
Solotion 2:
Are you also using the CSF firewall? If so, check to see if the "SMTP_BLOCK" setting is enabled. If STMP_BLOCK is enable contact to hosting to disable it.
Add this to your settings:
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
Related
Can someone tell me why phpMailer not working on my webhost. I test my website locally with XAMMP and email sending with phpMailer is working correctly but when I upload my files to my webhost server it doesn't work. my script do not tell me any errors. neither my server error_log message is appended.
Is there any configuration that I may need to do on my server?
This is how am using phpMailer:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//mailer function
function sendmail($mailFrom,$orgName,$toMail,$replyTo,$mailSugject,$mailBody){
//Load composer's autoloader
require 'mailer/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Recipients
$mail->setFrom($mailFrom, $orgName);
$mail->addAddress($toMail);// Add a recipient
$mail->addReplyTo($replyTo, $orgName);
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $mailSugject;
$mail->Body = $mailBody;
$mail->send();
return array('Mailer' => 1,'msg' => 'Message has been sent');
} catch (Exception $e) {
return array('Mailer' => $mail->ErrorInfo ,'msg' => 'Message could not be sent');
}
}
I have mail function in php which looks like:
<?php
$admin_email = "admin#gmail.com";
$email ="myemail#gmail.com";
$subject = "hellow ord";
$comment = "cmt";
try{
$th=mail($admin_email, $subject, $comment, "From:" . $email);
if($th)
echo "gg";
else
echo "error_message";
}
catch(Exception $e)
{
echo $e->message();
}
?>
I have Wamp server to run it. I have configured hMailServer by seeing a post on Stack Overflow but when I run the above code I get:
Warning: mail(): SMTP server response: 530 SMTP authentication is required...
Do I need to set up anything before using mail function?
The PHP mail() function doesn't support smtp authentication, which generally results in an error when connecting to public servers like the one you are trying to connect to. With hmailserver installed, you still need to use a library like PHPMailer which you can autheticate with.
Follow the steps here to setup phpmailer with gmail:
http://blog.techwheels.net/tag/wamp-server-and-phpmailer-and-gmail/
I've successfully sent email using gmail on (wamp/hmailserver/phpmailer).
<?php
require_once ("class.phpmailer.php");
$sendphpmail = new PHPMailer();
$sendphpmail->IsSMTP();
$sendphpmail->SMTPAuth = true;
$sendphpmail->SMTPSecure = "ssl";
$sendphpmail->Host = "smtp.gmail.com";
$sendphpmail->Port = 465;
$sendphpmail->Username = "your#gmail.com";
$sendphpmail->Password = "gmail_password";
$sendphpmail->SetFrom('your#gmail.com','blahblah');
$sendphpmail->FromName = "From";
$sendphpmail->AddAddress("recipient#gmail.com"); //don't send to yourself.
$sendphpmail->Subject = "Hello!";
$sendphpmail->Body = "<H3>Check out www.google.com</H3>";
$sendphpmail->IsHTML (true);
if (!$sendphpmail->Send())
{
echo "Error: $sendphpmail->ErrorInfo";
}
else
{
echo "Message Sent!";
}
?>
Hope this helps others as well. Took me a few hours of googling and compiling answers from places while facing this issue.
i am using PHPmailer to send emails
here is code that i have used:
$mail = new PHPMailer();
$subject = "test";
$to = "test_patel#yahoo.com"
$mail->SetFrom("PDSociety#aol.com","Punjab Dental Society");
$mail->AddReplyTo("PDSociety#aol.com", "Punjab Dental Society");
$mail->Subject = $subject;
$mail->MsgHTML($str);
$mail->AddAddress($to, "Punjab Dental Society");
if(!$mail->Send())
{
$err = "Mailer Error: " . $mail->ErrorInfo;
//echo $err;
} else {
$msg = "Message sent!";
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
if i change email address from yahoo to gmail or hotmail, still email are not sent.
i checked by echoing error, but no errors.
can anyone explain what is the issue ?
After trying various ways, i found following code working with almost all email providers
$to['email'] = "recipients email address";
$to['name'] = "name";
$subject = "email subject";
$str = "<p>Hello, World</p>";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'Specify main and backup server here';
$mail->Port = 465;
$mail->Username = 'xyz#domainname.com';
$mail->Password = 'email account password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'From Email Address';
$mail->FromName = "Any Name";
$mail->AddReplyTo('xyz#domainname.com', 'any name');
$mail->AddAddress($to['email'],$to['name']);
$mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $str;
if(!$mail->Send()) {
$err = 'Message could not be sent.';
$err .= 'Mailer Error: ' . $mail->ErrorInfo;
}
$mail->ClearAddresses();
variable values needs to be changed accordingly.
Hope these helps people having issues with PHPmailer
PHPMailer is only involved in submitting the message to your own mail server, and you're not having any problem there. After that, your mail server takes on the responsibility of sending it on, so you will find the answer in your mail server's logs.
There is no simple way to ensure messages end up in the inbox and not spam - if there was, spammers would be using it and filtering would be useless. Make sure your DNS resolves backwards and forwards, that you have valid SPF records, that you sign your messages with DKIM (especially important for Yahoo) and most importantly, that you don't send messages that your recipients think are spam.
Try this :
$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->AddAddress($to['email'],$to['name']);
$mail->FromName = '';
$mail->Subject = $subject;
$mail->MsgHTML($message);
$send = true;
return $mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
$e->getMessage(); //Boring error messages from anything else!
}
It will help you if any exception error.
Have you looked on the post here: Using PHPMailer Results in many blocked emails? The asker solved the issue by changing the email subject:
Well I solved the issue; the code above was not the problem and works
great.
In my subject, I used a phrase regarding "verify your account
information" and that got it blocked on a few ISP's.
So the lesson is, your subject matters. I was looking at my php code
and my body content before I realized this.
The content of the email and its subject can make ISPs ban it. You could try taking the content of one of your received emails from your inbox and see if that goes through.
I'm working on a registration page on a website where I need to implement a email verification.
I have installed PHPMailer-master on the server. The first part of the code is written directly in the registration php:
include('send_mail.php');
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> thank you for joining us! Please verify your email and get started using your account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, a verification email has ben sent to you. Please activate this to get started.";
And the code in send_mail.php:
<?php
function Send_Mail($to,$subject,$body)
{
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';
$from = "*mywebsite*";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "*my webhost's mailserver*"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "*username*"; // SMTP username
$mail->Password = "*password*"; // SMTP password
$mail->SetFrom($from, '*mywebsite?*');
$mail->AddReplyTo($from,'*mywebsite?*');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
I'm fairly sure the information in send_mail.php is correct. I'm using the information I found on my configure mail client page on cPanel ("Secure SSL/TLS Settings").
When I try to register, the website goes white somehow. The account gets created on phpmyadmin. No error-log in public_html.
Any ideas?
UPDATE: Sorry for late response.
I've tried using the PHPMailer Test Page. When I try SMTP, the page just loads forever. When using Sendmail, I get the error "Could not execute: /usr/sbin/sendmail -t -i". When I use the QMAIL, it says the email was sent but I never recieve it.
BUT... when I use Mail, I recieve it successfully. Is "Mail" secure enough to use? What are the differences? And how should I implement it into the php code of the register page? I can't find any specific part in the PHPMailer Test Page code for the "mail" function.
Code looks like this now:
$email = $_POST['email'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo('myemail');
$mail->AddAddress($email);
$mail->SetFrom('myemail');
$mail->AddReplyTo('myemail');
$mail->Subject = 'subject';
$mail->Message = 'message comes here';
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
Error message: "Message body empty"
I came across a bizarre issue with PHPmailer (version 5.1) that I'm trying to workaround. I've seen quite a bit of good feedback on here, so I thought I would give it a try. I've found that when I attempt to create a customized confirmation message with a conditional statement based on $mail->send(), I receive duplicate emails. I can duplicate it with the generic testemail.php script that comes with the PHPMailer download. Here's the code:
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.domain.com"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "password"; // SMTP server password
$mail->IsSendmail();
$mail->From = "example_from#domain.com";
$mail->FromName = "First Last";
$to = "example#domain.com";
$mail->AddAddress($to);
$mail->Subject = "PHP Mailer test";
$message = "This is a test. \n";
$mail->Body = $message;
$mail->Send();
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
The above code echoes the "Message has been sent" confirmation, but then sends two emails. If I comment out the $mail->send() line, I still receive the "message has been sent" confirmation and only get one message. If I remove the conditional statement and leave the $mail->send() line commented out, no email is sent.
Why does adding a conditional statement cause an email to be sent without calling the $mail->send() method?What is the proper way of adding a customized confirmation message?
When you put $mail->Send() in your conditional, you're actually calling it again, sending another message, and checking if that second message was sent.
If you just keep
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
and get rid of the original, unconditional Send call, you should be fine.
Alternatively, if it's clearer for you or if you need to do some processing elsewhere that depends on whether the message was successfully sent, you could do the essentially equivalent:
$status = $mail->Send();
if ($status) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}