Home > Software design >  How to send email from GoDaddy Linux VPS with gmail SMTP so that sent emails are saved
How to send email from GoDaddy Linux VPS with gmail SMTP so that sent emails are saved

Time:04-26

I have used the following PHP code to send email, but when I login to the gmail account after sending an email, the email that has sent is not recorded in the sent mail section of gmail. But emails are successfully delivered each time. I can use the IsSMTP() function in my laptop and in that case sent mails are recorded, but in GoDaddy VPS I can not use IsSMTP() but to use IsMail().

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\POP3;
require '../PHPMailer/Exception.php';
require '../PHPMailer/PHPMailer.php';
require '../PHPMailer/SMTP.php';
require '../PHPMailer/OAuth.php';
require '../PHPMailer/POP3.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->IsMail();
//$mail->IsSMTP(); // if enabled instead of IsMail() an error "can not connect to SMTP" appears.
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPAuth   = true;
$mail->Username   = '[email protected]';
$mail->Password   = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port       = 465;

//Recipients
$mail->setFrom('[email protected]', 'OFFICE');
$mail->addReplyTo('[email protected]', 'OFFICE');
$mail->addBCC("[email protected]", "OFFICE");//since sent mails are not saved
$mail->isHTML(true);
$mail->addAddress('[email protected]');
$mail->Subject = 'Subject';
$mail->Body    = 'Hello';
$mail->AltBody = 'Hello';
$mail->send();

CodePudding user response:

The problem is that Godaddy (and many other VPS providers) block SMTP ports in order to fight spammers.

When you are using $mail->IsMail(), the e-mails will go via GoDaddy's servers completely bypassing your Gmail account.

Now there is another problem as well, Google protects your @gmail.com addresses using various methods (such as SPF), so even if you are able to send the e-mail, it will be most likely marked as SPAM - if delivered at all. Otherwise anyone could send e-mails as you..

I'd highly suggest purchasing a domain (if you dont have one already) and either:

  1. Hosting it in GoDaddys e-mail service and sending it via their SMTP servers (their SMTP servers are allowed)
  2. Use external service which does not rely on SMTP (such as MailGun and their PHP SDK for example, but do your own research for a suitable one)

There might be some other "hacky" ways to still send these e-mails trough Gmail if you really need to, such as using VPN inside VPS or proxying google SMTP externally on some other port which is not blocked. Beware, these methods still require some external service (most likely paid) and might conflict with GoDaddy's and/or Gmail TOS

  • Related