I am using PHPMailer library to send SMTP email for booking enquiry. (Note: The same problem I was facing in PHP Pear Mail library as well).
I set the email from as below
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Username = 'gmail.owner#gmail.com'; // SMTP username
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true);
$mail->Password = 'xxxx';
$mail->setFrom(mark.antony#example.com, Mark Antony);
$mail->replyTo(mark.antony#example.com, Mark Antony);
$mail->addAddress(gmail.owner#gmail.com, Gmail Owner);
$mail->Subject = $whatever subject;
$mail->Body = $whatever html;
The problem is after sending email in the received mailbox I see the From: is the same as To: (Or same as the gmail/smtp username).
I have fixed the problem on reply to by setting replyTo value.
Is there anyway I can fix this? Or is that how it suppose to be?
Google does not allow you to set arbitrary from addresses, because doing so is usually forgery. If you try to, gmail will ignore it and substitute your Username address, which is what you're seeing.
There is one exception to this: you can set up additional aliases in your gmail settings (which you need to verify), and once you've done that you can use those addresses as from addresses.
If you're using this for a contact form (as it sounds like you are), setting to and from to your own address, and the submitter as a reply-to is the correct way to go, as the PHPMailer contact form example demonstrates.
Related
I am using PHP Mailer library to send mail. Here I set from email address. But that email does not show in the mail.
$mail->SMTPDebug = env("GMAIL_SMTP_DEBUG");
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "mygroup#gmail.com"
$mail->Password = "******";
$mail->SMTPSecure = env("GMAIL_SMTP_SECURE");
$mail->Port = env("GMAIL_SMTP_PORT");
$mail->ContentType = 'text/html; charset=utf-8\r\n';
$mail->WordWrap = 900;
//Recipients
$mail->Sender = "myemail#gmail.com";
$mail->SetFrom("myemail#gmail.com", "My Name",false);
.......
Here I want to receive from address is "myemail#gmail.com". But instead of I am getting "mygroup#gmail.com"
What you're asking for is effectively forgery, and gmail (along with other services) doesn't allow it - it will substitute your account address instead, as you're seeing.
The one thing you can do is set up fixed aliases for your account in your gmail settings, and you can then use those as from addresses, and gmail won't substitute them. Even with this, it still won't let you use arbitrary from addresses on the fly - you have to define them beforehand.
My webhost does not allow mail accounts to be created. However they have provided me with their SMTP gateway and said no credentials are needed when the mails are being sent from their datacenter.
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = GATEWAY_PROVIDED_BY_HOST
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
Invalid address: (From): root#localhost
It seems I need to add a from address. But in the case that they do not allow a mail account to be created, what do I put in the from address?
Try to use a valid sender "from" email address.
Update:
You have to use a sender address which is the mail server responsible for (or a white listed one).
normal "#domain.tld" is enought. the name in front of the at is often not tested.
Also possible: you can only send "to" the domain which is the mail server responsible for or you have to authenticate. best is you contact your server provider.
You need to set a From address:
$mail->setFrom('user#example.com', 'My Name');
This will set the from address that appears in message headers, but it will also used as the MAIL FROM command at the SMTP level (where your error is coming from) as it's automatically copied to the Sender PHPMailer property which applies there. That will avoid the fallback to the root#localhost address you're getting.
The other things you're doing to disable authentication are correct.
Create a Google Account and use it:
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "your#gmail.com";
$mail->Password = "*****";
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = "465";
$mail->setFrom('your#gmail.com', 'Your Name');
$mail->addReplyTo('your#gmail.com', 'Your Name');
I used the following code snippet using phpmailer class inorder to send an email using gmail server when a form is submitted in my web application! Though it works for my personel gmail address, it does not send emails when I replace my gmail address and password with my clients gmail address and password!
How can i correct this probem?
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "sunethperera#gmail.com"; // Your full Gmail address
$mail->Password = "******"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "Pharmacy order confirmaton"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress($_POST["Email"], "Recipient Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
When I send the message from my client's email, I get the error message:
SMTP Error: Could not authenticate.
How can I correct this issue?
Trying using this:
$mail->SMTPSecure = "tls";
Remove the Encoding parameter
And also check you have the correct from address and the password as well.
Update your PHPMailer right now. You are running an old vulnerable version. Read the troubleshooting guide and do what it says. Do not use the submitter's address as the from address - its forgery and will make you fail SPF checks. Gmail does not let you send from arbitrary from addresses. Your auth problem is covered in the guide, but you will need to either enable less secure apps in Gmail, or implement oauth, as described in the docs and examples.
This has all been covered many times on SO - search before you post.
I installed recently PHPMailer, because of not being able to send without it.
Now I have another problem: I want to send an email with an invent one, for example, "no-reply#my-domain.com". I don't seem to be able to do it. when I send with Sendmail, it just won't send, and if I use SMTP with autentication, it sends with my email.
require "../PhpMailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "marcelo43#gmail.com";
$mail->Password = "My Password";
$mail->setFrom('no-reply#my-domain.com','MyDomain Admin');
$mail->addAddress('to#gmail.com','To');
$mail->Subject = "Test";
$mail->msgHTML('My message');
if(!$mail->send())
echo 'Could not send email';
else
echo 'Email succesfully sent';
This just sends an email to "to#gmail.com" with "MyDomain Admin" as the name and "marcelo43#gmail.com" as its email.
What do I need to do to send with the email "no-reply#my-domain.com"?
You can't send email with a false email because Gmail blocks mail from an other domain that its own.
If you want to send an e-mail with an other domain you should use an other SMTP server. You can use an SMTP from OVH for example if you have a mail server. Otherwise, Yahoo will let you send you e-mail I think.
Gmail doesn't let you use arbitrary from addresses at all, even from gmail domains. You can however create aliases within your gmail settings that are allowed as from addresses. If you want to use your own domain via gmail, you will neeed to configure gmail as the MX for your domain. If you do that, you will be able to do what you ask.
BTW, this is mentioned in the PHPMailer docs.
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 "real.business#gmail.com" directly with PHPMailer. I can, however, use an intermediary gmail account "fake.12345.account#gmail.com" which can have "less secure apps" enabled, which permits SMTP verification.
However I do not want to have the emails be sent from this fake.12345.account#gmail.com account (wouldn't look particularly professional) - but rather the company's gmail account.
I can send the emails from the intermediary account to real.business#gmail.com; either through the editing of the PHPMailer parameters, or by automatically forwarding emails from fake.12345.account#gmail.com to the company account.
The problem lies in how real.business#gmail.com 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 = 'fake.12345.account#gmail.com'; // 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 = 'testing.num.101#gmail.com'; //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 fake.12345.account#gmail.com (that's trivial) but rather how to send the email from fake.12345.account#gmail.com to real.business#gmail.com such that real.business#gmail.com forwards the message to $personEmail
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.
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 fake.12345.account#gmail.com (and maybe real.business#gmail.com). Then save their body and title and pass it to PHPMailer. You can then use PHPMailer to send the emails with real.business#gmail.com.