php and send mail - php

I search to send mail with php script.
$mail = new PHPmailer();
$mail->IsSMTP();
$mail->Host='mail.mydomaine.com';
$mail->From='xxx#mydomaine.com';
$mail->AddAddress('xxx#yahoo.fr');
$mail->AddReplyTo('xxx#mydomaine.com');
$mail->Subject='test';
$mail->Body='example for mail';
if i make From address yyy#mydomaine.com it is work but if i change it to example yyy#gmail.com or yahoo.fr it do not work. this the error message
SMTP Error: The following recipients failed: xxx#yahoo.fr
SMTP server error: 5.7.1 : Relay access denied

If you want to use yyy#gmail.com or yyy#yahoo.com as from address, You need to configure respective mail server with authentication(mail account). For example, if you want to configure gmail configure like this..
$mailObj->Host = 'smtp.gmail.com';
$mailObj->Port = '465';
$mailObj->Username = 'yyyy#gmail.com';
$mailObj->Password = 'passwordofaboveaccount';
Now
$mailObj->From='xxx#gmail.com';
will work

you can add the mail for site....
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->Username = 'yyy#gmail.com';//your mail is valuable
$mail->Password = 'password';//your mail pass
if you need to change the mail to yahoo then simply change the host name with the yahoo smtp.......

How could you ever send email with someone elses email address? You can ofcourse only send from your own domain, if this PHP code is on your server.

do you have the following, yes?
$mailObj->SMTPAuth = TRUE;
$mailObj->SMTPSecure = "ssl";

Related

PHPMailer sets the From same as the smtp username

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.

Sending SMTP Email without Authentication

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');

Send email with false email on PHP

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.

phpMailer: Could not instantiate mail function

I created a form that uses phpMailer to email the results to the website owner. Of course, before I add the owner's email address I use mine to test that the form works. When I use my email the form sends perfectly, however, when I use the owner's address it throws the error "could not instantiate mail function" and won't send the form. The error only occurs with email addresses associated with the owner's domain. Any idea why this could be happening?
If I type this into the command line it works fine:
echo 'test' | mail -s 'test' me#example.com
edit: I wasn't initially using SMTP, but it's now configured as shown below. The error message is now "SMTP Error: The following recipients have failed xxx#somedomain.com" and the end result is still the same. It can e-mail to a number of gmail test addresses but has issue with the owner's email#hisdomain.com. Further, with SMTPDebug it's now producing the error "RCPT TO command failed: 550 No Such User Here" The owner's e-mail, however, works without issue when e-mailed through gmail, outlook, etc.
phpMailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = "error_log";
$mail->Host = "mail.mydomain.com";
$mail->SMTPAuth = true;
$mail->Username = "admin#mydomain.com";
$mail->Password = "XXXXXXXXXXXXXXX";
$mail->CharSet = 'UTF-8';
$mail->AddReplyTo($emailAddress);
$mail->SetFrom("admin#mydomain.com");
$mail->AddAddress($toAddress,$toName);
$mail->Subject = $emailSubject;
$mail->isHTML(true);
$mail->Body = $emailBody;
$mail->AltBody = strip_tags($emailBody);
// Attempt to send the e-mail
if (!$mail->send()) {
//error handling
}
There are couple of things you should try and check with this particular error message:
Make sure you can use regular php mail() function. Create a blank page and use the php mail() to send a test email. If that works, maybe its your SMTP that's having issues with the particular user domain. Setup gmail SMTP or a different SMTP to send emails:
$mail->IsSMTP();
$mail->Host = "smtp.domain.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Can you share your phpMailer source for us to view?
Set $mail->SMTPDebug = 2; so you can see what the server has to say, and read the troubleshooting guide.
You're using authentication without encryption, which is not a good combination and many servers won't allow that. Add this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You've based your code on an old example, so you're probably using an old version of PHPMailer too; get the latest from github.

PHPmailer gmail 'from' is gmail account

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.

Categories