Configuring PHPMailer on Siteground shared hosting plan - php

I am stuck trying to make PHPMailer to work with my website hosted on Siteground.
I installed it by composer via PuTTY, everything looking fine so far:
But when I try to test it, I can't get it to work properly.
Here is a sample of my testing code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = '**HOST**';
$mail->SMTPAuth = true;
$mail->Username = '**MAILACCOUNT**';
$mail->Password = '**PASSWORD';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('**MAILACCOUNT**', 'Mailer');
$mail->addAddress('**TESTMAILRECIPIENT', 'test');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Which is a modified version of the template PHPMailer provides on their github.
All of my credentials are good (host, mail account, password). I tried with different ports (25, 587) and as per Siteground the good port for SMTP is 465. But nothing work. The try catch block does not even catch any error, sometimes depending of what I try my webpage stays blank, or return a HTMLERROR 500.
I tried to reach out their technical support but they don't seem to have a clue about anything. They sent me the link to the PHPMailer github, telling me to follow the tutorial.
There is absolutely no documentation about using this PHP library with their services. I have been searching for 2 days now. It seems they have changed their interface, as many - if not all - of their tutorials are for an older version of their cPanel.
Maybe it could have something to do with SPF and DKIM protocols? DKIM is active on my account, SPF I have absolutely no clue what to do with this.
Here is my generated SPF record, if ever you need to know:
v=spf1 +a +mx +ip4:**MYIP** include:_spf.mailspamprotection.com ~all
I tried many online tutorials and adapted them to my needs with Siteground, with no success.
Is there anybody used to their system and could help me getting this to work?
I use a shared hosted plan, if it helps. They use Linux servers.
Thank you very much!

Just in case someone is looking for the same thing - for me what worked is the next:
prefixing the host with "ssl://"
in PHPMailer instance setting:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
Also the port I used was 465.
Very strange combination of settings, but at least it worked in my case.

Related

How to configure phpmailer to send mails with proxies?

How can i use proxies to send smtp mails using PHPmailer?
The proxy format is: proxy/port/user/password
I am using following code to send email:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'C:/xampp/php/pear/PHPMailer-master/src/PHPMailer.php';
require 'C:/xampp/php/pear/PHPMailer-master/src/SMTP.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "test#gmail.com";
$mail->Password = "password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->From = "test#gmail.com";
$mail->FromName = "Name";
$mail->addAddress("address#yahoo.com", "name");
$mail->isHTML(true);
$mail->Subject = "Mail Subject";
$mail->Body = "Something";
$mail->AltBody = "This is the plain text version of the email content";
try {
$mail->send();
echo "Message has been sent successfully";
} catch (Exception $e) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
I tried with socat had no success since socat cannot be installed on my server,i also tried with curl but had no success.
I'm also aware that phpmailer has no explicit SMTP proxy support.
Also guide me if there is a substitute for PHPMailer that can use proxy, in case proxies can't be configured for PHPMailer.
I suspect this is the same question as was asked on GitHub recently.
The short answer is that you can't. Not because PHPMailer doesn't support it, but because proxying SMTP doesn't really exist in any practical sense because of its disconnected, async, store-and-forward nature. It's completely unlike HTTP in this respect. You have two alternatives:
Use a TCP proxy. You specify the proxy host IP as your mail server, and it forwards the traffic. This is completely transparent to PHPMailer, and so does not require any specific support, but you are likely to run into TLS issues.
Use an SMTP relay. Send to a "nearby" mail server (usually referred to as a "smarthost") which then deals with onward delivery for you. This is probably what you want. Again, PHPMailer doesn't care about your relaying arrangements here as you simply deliver to the smarthost as usual, so no specific support is required.
The reason an "SMTP proxy" can't really work is because there is no guarantee that an SMTP server can make an onward delivery. It may require literally days of waiting and retrying to deliver a message, and this means that the proxy needs to implement everything an SMTP server does, turning it into an SMTP relay.
There are SMTP proxies, but they are typically only ever used in high-availability environments, such as in SMTP front-end load balancers, where immediate onward delivery is essentially guaranteed to be possible, so the proxy needs no local storage or queuing abilities. That's probably not what you're looking for.
If you want more than that you need to say exactly what it is you're trying to achieve, why, and how.

Troubleshooting PHPMailer with Bluehost

Trying to set up PHPMailer for a site I have hosted on Bluehost and after a full day of researching and troubleshooting I just can not get it working.
I'm a beginner so apologies in advance for the newbie question, but I've been reading everything I can find (including this and this, as well as the PHPMailer docs) to solve this but can't seem to get mine set up correctly. Any guidance, thoughts on what I'm doing wrong or ways to debug this are much appreciated.
This is what I've found regarding Bluehost SMTP.
Secure SSL/TLS Settings (Recommended)
Username Your email address: john#example.com
Password The password for that email account.
Incoming Server mail.example.com*
Incoming Port 993 (IMAP) or 995 (POP3)
Outgoing Server mail.example.com*
Outgoing Port 465 (SMTP)
Authentication Password
*Replace example.com with your domain name.
Below is what I'm using in my file (personal info removed).
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.MYDOMAIN.com';
$mail->SMTPAuth = true;
$mail->Username = 'MYEMAIL#MYDOMAIN.com';
$mail->Password = 'MYEMAILPASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 465;
//Recipients
$mail->setFrom('MYEMAIL#MYDOMAIN.com');
$mail->addAddress('MYEMAIL#MYDOMAIN.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I've installed PHPMailer using Composer so the autoload.php file (and the phpmailer folder) are in a 'vendor' folder located in the same directory as the file that contains the code shown above.
After uploading to my Bluehost server, when I try to display the webpage that has this code in the browser, I get this HTTP ERROR 500 screenshot and of course no email sent.
Something that's covered in many of the examples and the docs is what combinations of encryption and port settings will work.
You have Port = 465 and SMTPSecure = 'tls'; that won't work. Either change Port to 587, or SMTPSecure to 'ssl' (but not both!). As it stands, you're trying to open a connection to a port that expects implicit TLS while using a protocol that expects to need to make it explicit with STARTTLS, and that's not going to work.

new PHPMailer is not running?

Below is the phpmailer code which I am using :
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = as given in the configure mail client window;
$mail->SMTPAuth = true;
$mail->Username =as given in the configure mail client window;
$mail->Password = as given in the configure mail client window;
$mail->SMTPSecure = 'tls' ;
$mail->Port = 465;
$mail->setFrom('my email', 'my name');
$mail->addAddress('email', 'name');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
}
catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
The page keeps loading and the e-mail is not sent,What is happening ? and how should I configure this . I am using godaddy cpanel.
You're using SMTP to localhost (which is the recommended and usually the fastest sending mechanism, as far as your script is concerned), but you've enabled encryption and authentication, so your local mail server will need to present a valid certificate for localhost, which is not going to happen. It's usually unnecessary to use encryption or authentication when sending to localhost, because you can whitelist localhost as the source, and this will make it even faster.
If you set SMTPDebug = 2, you can look at the timestamps in the SMTP conversation and see which part is taking a long time.
Keepalive won't help unless you're sending lots of messages in quick succession.
It may also help to look in your local mail server's logs and see if there's anything interesting in there.
You're also using a very old version of PHPMailer; get the latest, and base your code on the examples provided.
You should have no trouble submitting a few hundred messages per second.
If your problem is that submission is fast, but ultimate delivery is slow, you need to look at your local mail server logs for why that is. You may be getting delivery deferrals.

Sending emails using Amazon AWS SES

I have configured aws's ses service but have not been able to use it. My emails are being sent but they are sent through my hosting server (Godaddy) and I would want to send them only through AWS's SES. Pardon me, if I have made a rookie mistake.
Here's what my code looks like
<?php
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->Host = 'email-smtp.eu-west-1.amazonaws.com';
$mail->Port = 25;
$mail->ssl = true;
$mail->authentication = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->setFrom('vaibhav#zigsaw.in', 'Test Email');
$mail->Body = 'Test Email from Zigsaw';
$mail->AddReplyTo('zigsawconsultancy#gmail.com', 'Candidate');
$mail->addAddress('zigsawconsultancy#gmail.com', 'Recruiter');
$mail->Subject = 'Test Email from Zigsaw';
if(!$mail->send())
{
echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
}
else
{
echo "Email sent!" , PHP_EOL;
}
?>
Disclaimer: My file is hosted on a godaddy server and I am trying to send emails through AWS SES.
GoDaddy blocks outbound SMTP, but you've not told PHPMailer to use SMTP (so most of your settings do nothing), so it's submitting via the mail() function, using a local mail server, which in GoDaddy's case is their server - so that's why it's going that way. Unfortunately there isn't a way of using external SMTP on GoDaddy, though you can send via their secureserver.net relay - but that of course means you can't send using your personal domain because it's forgery and you messages will be spam-filtered or bounced.
If you call:
$mail->isSMTP();
PHPMailer will use SMTP and your other settings, though as I said in my comment, you've just invented a bunch of things that will not work, so rewrite your code based on the examples provided, though this won't work on GoDaddy.
If SES has an HTTP API, so you may be able to use that instead, since it will not be subject to GoDaddy's blocking.

Phpmailer error "Could not instantiate mail function"

I'm using the mail() basic example modified slightly for my user id and I'm getting the error "Mailer Error: Could not instantiate mail function"
if I use the mail function -
mail($to, $subject, $message, $headers);
it works fine, though I'm having trouble sending HTML, which is why I'm trying PHPMailer.
this is the code:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
print ($body ); // to verify that I got the html
$mail->AddReplyTo("reply#example.com","my name");
$mail->SetFrom('from#example.com', 'my name');
$address = "to#example.com";
$mail->AddAddress($address, "her name");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Try using SMTP to send email:-
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Your code looks good, did you forget to install PostFix on your server?
sudo apt-get install postfix
It worked for me ;)
Cheers
This worked for me
$mail->SetFrom("from#domain.co","my name", 0); //notice the third parameter
$mail->AddAddress($address, "her name");
should be changed to
$mail->AddAddress($address);
This worked for my case..
You need to make sure that your from address is a valid email account setup on that server.
If you are sending file attachments and your code works for small attachments but fails for large attachments:
If you get the error "Could not instantiate mail function" error when you try to send large emails and your PHP error log contains the message "Cannot send message: Too big" then your mail transfer agent (sendmail, postfix, exim, etc) is refusing to deliver these emails.
The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication):
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.example.com"; // set the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "johndoe#example.com"; // SMTP account username
$mail->Password = "********"; // SMTP account password
PHPMailer defaults to using PHP mail() function which uses settings from php.ini which normally defaults to use sendmail (or something similar). In the above example we override the default behavior.
The PHPMailer help docs on this specific error helped to get me on the right path.
What we found is that php.ini did not have the sendmail_path defined, so I added that with sendmail_path = /usr/sbin/sendmail -t -i;
In my case, it was the attachment size limit that causes the issue. Check and increase the size limit of mail worked for me.
Seems in my case it was just SERVER REJECTION. Please check your mail server log / smtp connection accessibility.
I had this issue, and after doing some debugging, and searching I realized that the SERVER (Godaddy) can have issues.
I recommend you contact your Web hosting Provider and talk to them about Quota Restrictions on the mail function (They do this to prevent people doing spam bots or mass emailing (spam) ).
They may be able to advise you of their limits, and if you're exceeding them. You can also possibly upgrade limit by going to private server.
After talking with GoDaddy for 15 minutes the tech support was able to resolve this within 20 minutes.
This helped me out a lot, and I wanted to share it so if someone else comes across this they can try this method if all fails, or before they try anything else.
An old thread, but it may help someone like me. I resolved the issue by setting up SMTP server value to a legitimate value in PHP.ini
I had this issue as well. My solution was to disable selinux. I tried allowing 2 different http settings in selinux (something like httpd_allow_email and http_can_connect) and that didn't work, so I just disabled it completely and it started working.
I was having this issue while sending files with regional characters in their names like: VęryRęgióńął file - name.pdf.
The solution was to clear filename before attaching it to the email.
Check if sendmail is enabled, mostly if your server is provided by another company.
For what it's worth I had this issue and had to go into cPanel where I saw the error message
"Attention! Please register your email IDs used in non-smtp mails through cpanel plugin. Unregistered email IDs will not be allowed in non-smtp emails sent through scripts. Go to Mail section and find "Registered Mail IDs" plugin in paper_lantern theme."
Registering the emails in cPanel (Register Mail IDs) and waiting 10 mins got mine to work.
Hope that helps someone.
A lot of people often overlook the best/right way to call phpmailer and to put these:
require_once('../class.phpmailer.php');
or, something like this from the composer installation:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "../../vendor/autoload.php";
on TOP of the page before calling or including anything else. That causes the "Could not instantiate mail function"-error by most folks.
Best solution: put all mail handling in a different file to have it as clean as possible. And always use SMTP.
If that is not working, check your DNS if your allowed to send mail.
My config: IIS + php7.4
I had the same issue as #Salman-A, where small attachments were emailed but large were causing the page to error out.
I have increased file and attachments limits in php.ini, but this has made no difference.
Then I found a configuration in IIS(6.0), and increased file limits in there.
Also here is my mail.php:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../_PHPMailer-master/src/Exception.php';
require '../_PHPMailer-master/src/PHPMailer.php';
try{
$email = new PHPMailer(true);
$email->SetFrom('internal#example.com', 'optional name');
$email->isHTML(true);
$email->Subject = 'my subject';
$email->Body = $emailContent;
$email->AddAddress( $eml_to );
$email->AddAttachment( $file_to_attach );
$email->Send();
}catch(phpmailerException $e){echo $e->errorMessage();}
future.
We nee to change the values of 'SMTP' in php.ini file
php.ini file is located into
EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.ini

Categories