Asked  7 Months ago    Answers:  5   Viewed   46 times

I use gmail smtp for contact form in my site.(PHPMailer script https://github.com/PHPMailer/PHPMailer?)
my code is:

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "main@gmail.com";
$mail->Password = "xxxxxxxxxx";
$mail->SetFrom("another@gmail.com");
$mail->addReplyTo("another@gmail.com");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
$mail->AddAddress("main@gmail.com");
 if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>

It works but i have two problems:

  1. I set $mail->SetFrom("another@gmail.com");
    but in my gmail show from: main@gmail.com

  2. I set $mail->addReplyTo("another@gmail.com");
    but in my gmail when i click replay button email replayed to main@gmail.com
    my code is

 Answers

21

I found my answer. in your Gmail go to

setting ->accounts ->Send mail as

click Add another email address you own in new window enter new email address (example if your gmail is yourmail@gmail.com you must enter your.mail@gmail.com)or(if your gmail address have dot you must change position of dot. example if your gmail is yo.urmail@gmail.com you must enter yourma.il@gmail.com)
don't forget uncheck Treat as an alias.
click next step.
enter image description here

go back to setting ->accounts ->Send mail as
make a new email as defult
check Reply from the same address the message was sent to
all done!
i change code use new codes.
enter image description here
now show from my site

enter image description here
now when you click replay botton show replay to user email
enter image description here

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "yourmail@gmail.com"; 
$mail->Password = "xxxxxxxxx";
$mail->addReplyTo("useremail@gmail.com","user");
$mail->SetFrom("useremail@gmail.com","My Site");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
$mail->AddAddress("yourmail@gmail.com");
 if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>
Wednesday, March 31, 2021
 
nasty
answered 7 Months ago
79

I got the issue some of hosting provider not supporting external host for mail. you have to ask your hosting support weather they allow external host or not.

I used imap settings of my hosting provider and now it is solved hope this will help someone!!

Wednesday, March 31, 2021
 
binoculars
answered 7 Months ago
59

You edited your question several times, each time changing its meaning, that should be avoided in future as it generates tones of comments. Instead next time pls, read error messages and fix them before asking. What you did wrong (in order of appearance)

  1. Don't use assigning methods without values just for future like $mail->addAddress('');, $mail->addCC(''), etc, it will cause errors.
  2. Use proper SMTP server address.
  3. Use proper credentials. Of course login and password are case sensitive.
  4. Turn ON 'Less secure applications' on your Gmail account if still have problem with credentials errors. It can be done at the security tab as shown at screenshot

Turn on less secure apps in your account

Saturday, May 29, 2021
 
weegee
answered 5 Months ago
44

Your vps doesn't have rDNS configured and the HELO points to a different ip. inconsistencies in HELO and missing rDNS trigger many spamfilters.

Received: from ryanteck.org.uk ([95.154.250.75])

Host 75.250.154.95.in-addr.arpa. not found: 3(NXDOMAIN)

ryanteck.org.uk has address 95.154.250.165

Set a HELO Hostname whose A-Record points back to your VPS and configure the rDNS (PTR) to the same hostname.

Example:

  • Create a new A-Record 'vps.ryanteck.org.uk 95.154.250.75'
  • Set the HELO to 'vps.ryanteck.org.uk'
  • Set the reverse DNS (PTR) for 75.250.154.95 to vps.ryanteck.org.uk

Also your senderdomains have somewhat strange "TXT" records:

dig txt mc-serverlist.co.uk +short 
"95.154.250.165"
dig txt ryanteck.org.uk +short
"95.154.250.165"

you may want to change these into correctly formatted SPF records, eg.

v=spf1 ip4:95.154.250.165 ip4:95.154.250.75 ~all
Saturday, August 7, 2021
 
Lance
answered 3 Months ago
56

Why would you not expect it to use HTML if you call IsHTML(true)? That's how you tell PHPMailer to treat your message body as HTML! If you don't want HTML as the content type, call IsHTML(false), or just don't call it at all since plain text is the default.

If you want both HTML and plain text, call msgHTML($html) instead and it will also handle the HTML->text conversion for you.

As Chris said, call IsHTML before setting Body.

And as Dagon said, if you put HTML in $message, it will send HTML...

Wednesday, August 18, 2021
 
Tyler Rinker
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :