I'm trying to send a mail with PHPmailer and gmail. But when I send a mail its from my gmail account instead of no-reply. How do I set no-reply as from?
Code I have:
$mail = new PHPMailer();
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "myAccount#web.com";
$mail->Password = "password";
$mail->From = "no-reply#web.com";
$mail->AddAddress($to);
$mail->AddReplyTo("no-reply#web.com","no-reply");
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->WordWrap = 150;
$mail->send();
The mail I get receives (headers):
Return-Path: <myAccount#web.com>
Which should be no-reply#web.com
Thanks in advance
There are two options I would recommend trying:
Log in to the Google Apps mail account for myaccount#web.com, go to Settings, then Accounts, then Send mail as, and ensure that no-reply#web.com has been validated. I haven't tested this, but this should allow to send mail from no-reply#web.com, rather than from no-reply#web.com on behalf of myaccount#web.com.
Actually create a user on Google Apps with the username no-reply#web.com, and send email through that account. If myaccount#web.com needs a copy, you could always BCC the address and setup a filter on no-reply#web.com to forward any failures to myaccount#web.com.
You can't do it. Just imagine sending mail prompting to reply with your bank account credentials from an address validation#yourbank.com.
To have no-reply address you must have an access to the mail server in #your.domain (not gmail) and create such account there, then send emails using this account.
Related
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.
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.
This code is work, email is sent
$mail = new PHPMailer();
$mail->setFrom("name1#gmail.com", "Name");
$mail->addAddress($to); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("name1#yahoo.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send())
{
echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
}
But for some reason, if I change From email to name2#yahoo.com
$mail->setFrom("name2#yahoo.com", "Name");
emails does not sends anymore. Phpmailer does not report any error messages.
name2#yahoo.com is valid working email address related to this web server.
Thanks.
This is covered in the PHPMailer troubleshooting guide.
Most service providers now have strict SPF and DMARC configurations (Yahoo especially, since they invented DMARC) that mean that you cannot send from addresses in their domains except through their own mail servers, or any others included in their SPF records.
Your code is sending through your own local server, which is not a Yahoo server, and thus won't work.
The solution is to send through Yahoo's own servers using authentication for your email account, something like:
$mail->isSMTP();
$mail->Host = 'smtp.mail.yahoo.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'me#yahoo.com';
$mail->Password = 'password';
Yahoo's DMARC config won't let you spoof the from address, so you can only use a from address that matches your username - this is probably the cause of the symptom you're seeing.
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.
Is it possible to use Google's mail server for testing purpose,and replace the address of mail server when my own server is ready?
You can just send your mails via smtp.gmail.com (port 465 or 587) as with any email client. Note anyway that you will need a Google email account for this. More details are here: Configuring email clients for using GMail
I suggest you to use phpmailer, this is an example working code with it:
<?php
include_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = 'smtp.gmail.com';
// set the SMTP port
$mail->Port = '465';
// GMAIL username
$mail->Username = 'your.gmail.user#gmail.com';
// GMAIL password
$mail->Password = 'your-gmail-password';
$mail->From = 'email address who send the email';
$mail->FromName = 'yourname';
$mail->AddReplyTo('email to reply', 'name to reply');
$mail->Subject = 'Test Gmail!';
if($is_your_mail_an_html){
$mail->MsgHTML($html_message);
$mail->IsHTML(true);
}else{
$mail->Body = $text_message;
$mail->IsHTML(false);
}
$mail->AddAddress('to address email', 'to name');
if(!$mail->Send()){
echo = $mail->ErrorInfo;
}else{
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
But even without phpmailer, you can use gmail to send emails; Just set the port to 465 and enable the ssl auth.
P.s.: dont try to send nesletter throught gmail; they will block your account for 1 day if you send more than $x email per day ($x is 500 on the google documentation, but my experience say that is around 85!)
Yes google does offer that via smtp.
smtp.google.com
port: 587
You also will need your google username and password to send emails.
You need a php smtp class. PHPMailer has one.
If you run a Windows server you can just do this (if you have access to the php.ini). Otherwise follow Sarfraz suggestion.
<?php
ini_set('sendmail_from','test#test.com');
ini_set('SMTP','smtp.test.net');
mail(...);
?>