0

I am working on this basic website which contains a contact us form. This contact form is supposed to send mail to a predefined mail address with user feedback. As I am using gmail as smtp, this does not let users to send email with other email address and replaces user email address with default user email address. What should I do to use my email address as a contact mail address, while the users mail address as sender mail address?

For example:

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'sakib.cse11.cuet@gmail.com';                 // SMTP username
$mail->Password = 'mypassword';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('sakibdterror@gmail.com', 'Mailer');
$mail->addAddress('sakib.cse11.cuet@gmail.com', 'Sakib Rahman');     // Add a recipient
               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');

// $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here adasfffasfs 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';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

enter image description here

Whatever the from address, the mail is always sent from the username address(For example I have set mail->setForm ='sakibdterro@gmail.com", but google replaces it with the default username "sakib.cse11.cuet@gmail.com")

Sakibur Rahman
  • 834
  • 3
  • 10
  • 26
  • Post your (relevant) source code. – Funk Forty Niner Jun 16 '17 at 00:24
  • I did @Fred-ii- . Thanks for the suggestion. – Sakibur Rahman Jun 16 '17 at 00:30
  • I have difficulty understanding what you mean by: *"this does not let users to send email with other email address and replaces user email address with default user email address."* - Can you elaborate on that and how it fails? – Funk Forty Niner Jun 16 '17 at 00:33
  • Usually, a "From" sender can be taken from the form's email input, if that is what you are asking. Given the (unknown form's input), i.e.: `` you would then use `$from_email = $_POST['from_email'];` then use `$mail->setFrom($from_email, 'Sender mail');` type of thing; again... "if" that is what you are asking. – Funk Forty Niner Jun 16 '17 at 00:37
  • No, that is not what I am asking. Please take a look at edited post @Fred-ii-. Whatever I set as from_email address, it is replaced by the default username for smtp. – Sakibur Rahman Jun 16 '17 at 00:40
  • OooOOoohhhh I know what this is about now. You have no control over that. You have an Gmail alias happening here and it's replacing it with your "original" Gmail created account. – Funk Forty Niner Jun 16 '17 at 00:42
  • Yes, what should I do? Is there any alternatives? – Sakibur Rahman Jun 16 '17 at 00:44
  • I'm afraid not. – Funk Forty Niner Jun 16 '17 at 00:44
  • So, how the contact us pages are generally handled? Using other smtp services or any third party services like "MailChimp"? – Sakibur Rahman Jun 16 '17 at 00:47
  • You could but then they'll ask you for your email address and it will use that as a default; you would need to use an alternate email address and I am afraid that you will be back to "square one". – Funk Forty Niner Jun 16 '17 at 00:48
  • So what is the solution? I mean there's absolutely millions of examples with contact us pages in websites. What do they do? – Sakibur Rahman Jun 16 '17 at 00:51
  • If you are asking of how to send an email on behalf of other user, it is not possible by design. What other millions do, is they add sender's email from the form to the email body. – Alex Blex Jun 16 '17 at 00:56
  • Possible duplicate of [How to change from-address when using gmail smtp server](https://stackoverflow.com/questions/1332510/how-to-change-from-address-when-using-gmail-smtp-server) – Synchro Jun 16 '17 at 07:05

1 Answers1

2

Please search before posting: This has been answered on here many times before, and the contact form example provided with PHPMailer shows you exactly how to do it.

Gmail does not let you set arbitrary from addresses, though you can set up fixed aliases in your gmail settings. The reason it does not allow it is because it's forgery, and will cause your messages to fail SPF checks.

The right way to do this is to put your own address as the from address, and add the submitter's address as a reply-to. This way you're not forging anything, and when you reply to a message, it will go to who you expect:

$mail->setFrom('sakib.cse11.cuet@gmail.com');
$mail->addReplyTo($_POST['from_email']);
Synchro
  • 35,538
  • 15
  • 81
  • 104