I'm setting up a PHP contact form on a site. I'm using the Swift Mailer library to send the mail, and my domain email is through Google Apps. Is it possible to use Google Apps for company email and use sendmail/SMTP on my VPS to send email from the contact page? The problem I'm having is that I can't dynamically generate the from address, Google's servers force that to be the email address that the email is going through. Thanks in advance.
Asked
Active
Viewed 1,607 times
2 Answers
0
I use PHPMailer with this function...
function email($to, $subject, $body){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "email@domain.com";
$mail->Password = "password";
$mail->SetFrom("anything@domain.com", "Any Thing");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
$mail->Send();
unset($mail);
}

Dejan Marjanović
- 19,244
- 7
- 52
- 66
-
Thanks for your reply. I don't think this will solve my issue though. Swift Mailer is working perfectly, the issue is that Google won't allow dynamically setting the 'from' email address when sending through their SMTP server. When the site admin gets the form email, hitting reply would send the email right back to him, not to the person that filled out the site's contact form. Does this make sense? – Lenwood Dec 18 '10 at 17:01
-
1Oh sorry, you need to add ReplyTo address in your email. – Dejan Marjanović Dec 18 '10 at 17:03
-
Google ignores the reply-to info though, they won't allow it to be set dynamically. No matter what I place in that field, the from address is always my Google Apps email address. I presume they do this to avoid having their servers be used to submit spam. – Lenwood Dec 19 '10 at 15:30
0
After doing some reading, I realized that I didn't need a mail library at all. I'm using PHP's mail() function to accomplish exactly what I wanted, sending form mail through sendmail and having Google Apps handle all domain email. Here's the relevant code that's working for me.
// Define message variables if(get_magic_quotes_gpc()){ $name = stripslashes($_POST['name']); $email = stripslashes($_POST['email']); $body = stripslashes($_POST['body']); }else{ $name = $_POST['name']; $email = $_POST['email']; $body = $_POST['body']; } $subject = "Website Contact Form"; $recipient = "web@somesite.com"; $content = "NAME: $name, $email\nCOMMENT: $body\n"; $mailheader = "MIME-Version: 1.0\r\n"; $mailheader .= "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "Bcc: another.email@address.com" . "\r\n"; mail($recipient, $subject, $content, $mailheader) or die("Failure"); header("Location:/thankyou.php"); }
This is working perfectly for me. Hope it helps someone else.

Lenwood
- 1,371
- 16
- 36