Stumbler
Stumbler

Reputation: 2146

PHPMailer: Dynamic gmail forwarding

I do not know if what I want to do is possible (but finding out that it isn't would be useful in itself).

I cannot use my company's gmail account "[email protected]" directly with PHPMailer. I can, however, use an intermediary gmail account "[email protected]" which can have "less secure apps" enabled, which permits SMTP verification.

However I do not want to have the emails be sent from this [email protected] account (wouldn't look particularly professional) - but rather the company's gmail account.

I can send the emails from the intermediary account to [email protected]; either through the editing of the PHPMailer parameters, or by automatically forwarding emails from [email protected] to the company account.

The problem lies in how [email protected] can then successfully email the email (or at least appear to be the sender), as originally intended.

The code so far

$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host        = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug   = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth    = TRUE; // enable SMTP authentication
$Mail->SMTPSecure  = "tls"; //Secure conection
$Mail->Port        = 587; // set the SMTP port to gmail's port
$Mail->Username    = '[email protected]'; // gmail account username
$Mail->Password    = 'a_password'; // gmail account password
$Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 =   low)
$Mail->CharSet     = 'UTF-8';
$Mail->Encoding    = '8bit';
$Mail->Subject     = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From        = '[email protected]'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName    = 'Testing Again';
$Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line

$Mail->addAddress($personEmail); // To: the PERSON WE WANT TO EMAIL
$Mail->isHTML( TRUE );
$Mail->Body    = ' Good news '.$personName.'! The email sent correctly!';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();

if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}

So the issue is: not having the email sent to $personEmail from [email protected] (that's trivial) but rather how to send the email from [email protected] to [email protected] such that [email protected] forwards the message to $personEmail

Upvotes: 0

Views: 1707

Answers (2)

Synchro
Synchro

Reputation: 37730

What you're describing is really relaying, which is usually configured in the mail server config (not the messages), but you don't have access to anything like that in gmail.

You can set allowed aliases in gmail, but I would guess that these are not allowed to overlap with existing gmail account names as that would be a major security hole. Why not enable "less secure apps" on the main account? It's not as if it is actually any less secure - if anything it's better, because the setup to use OAuth2 is so deeply complex and unpleasant...

That said, rather than trying to do all this forgery, you may be interested in this PR and associated docs. It's fairly likely the xoauth branch will get merged into master and released without any further changes as PHPMailer 5.2.11, and it would very helpful if you could give it a try.

Upvotes: 0

Oh boy do I try
Oh boy do I try

Reputation: 43

PHPMailer is made for sending.

What you want to do is forward an email. This implies receiving the email and then sending it through.

What you need is some kind of IMAP client in php, that will allow you to read the emails on [email protected] (and maybe [email protected]). Then save their body and title and pass it to PHPMailer. You can then use PHPMailer to send the emails with [email protected].

Upvotes: 0

Related Questions