Viewed   509 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

4

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.

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.

now show from my site


now when you click replay botton show replay to user email

<?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, August 31, 2022
 
fauphi
 
2

I have shared two different mail methods for your requirement..

First case;

// Pear Mail Library
require_once "Mail.php";

$from = '<from.gmail.com>';
$to = '<to.yahoo.com>';
$subject = 'Hi!';
$body = "Hi,nnHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

Second case; (http://ctrlq.org/code/19589-send-mail-php)

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'amit@gmail.com';                   // SMTP username
$mail->Password = 'digitalinspiration';               // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;                                    //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('amit@gmail.com', 'Amit Agarwal');     //Set who the message is to be sent from
$mail->addReplyTo('labnol@gmail.com', 'First Last');  //Set an alternative reply-to address
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/usr/labnol/file.doc');         // Add attachments
$mail->addAttachment('/images/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

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

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

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

echo 'Message has been sent';

Try it and hope it will solve your issues !

Friday, November 11, 2022
 
4

Gmail re-writes the headers on messages sent through smtp.gmail.com, replacing the FROM address with the gmail address associated with the gmail account that you are using to send the message through. For more info, and a possible workaround, see: http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server

Tuesday, October 18, 2022
4

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

Thursday, November 17, 2022
11

The error was in the myorigin parameter : it has to be MY_GSUITE_DOMAIN.COM to have the right sender and not the host.

So google error was a bit misleading here.

Also dont forget to lower less secure apps in the gsuite account : https://support.google.com/a/answer/6260879?hl=fr

For those like me who want to send from gsuite email accounts with php, dont forget to :

  • specify the sender, either in php.ini or directly into the php script (in php mail, or anything else)
  • define the right place of sendmail command in php.ini

... and debug using /var/log/mail.log or similar for your distro.

The best tutorial I found so far on the postfix / gsuite set-up is this one : https://www.linode.com/docs/email/postfix/configure-postfix-to-send-mail-using-gmail-and-google-apps-on-debian-or-ubuntu/

(I skipped the app password part)

Saturday, December 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :