My client is using godaddy to host a php site, and has recently signed up for google apps account for email, hoping it will help stop his automated emails from getting rejected as spam. The code sends emails using the phpmailer class. What changes do I need to make so that the email will be sent out through google instead of through godaddy? Is it a code change? Or do I need to set up mx records somewhere? How can I tell which server is actually sending the email

  • It's a code change. PHPMailer includes class.smtp.php for external mail servers. – Andrew Lott Jan 30 '17 at 10:55
  • Sorry but this question is considered off-topic because it is about programming. Generally we would migrate such questions across to Stack Overflow but in this instance have not because it lacks code and would be closed on the receiving end. Please visit Stack Overflow and visit the help centre before reposting. – Simon Hayter Jan 30 '17 at 13:52

MX Record will not automatically send email from your server. You need to make some CODE change. Check this answer if you want to do it with PHPMailer.

$mail = new PHPMailer;
// Tell PHPMailer to use SMTP
// Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
// Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
// Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
// Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
// Whether to use SMTP authentication
$mail->SMTPAuth = true;
// Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
// Password to use for SMTP authentication
$mail->Password = "yourpassword";

However, that solution has some problems of its own. The best way is to properly setup your mail server using DKIM and other settings, so that your emails don't go to spam.


In the code somewhere, you can set many variables that PHPmailer is going to use. This includes explicitly naming the mail server.

Your code may include something like

$mail->Host = 'localhost';
$mail->Port = 25;
$mail->Username = "yourname@example.com";
$mail->Password = "yourpassword";

There is a tutorial at https://github.com/PHPMailer/PHPMailer/wiki/Tutorial and examples at https://github.com/PHPMailer/PHPMailer/tree/master/examples

Not the answer you're looking for? Browse other questions tagged or ask your own question.