I have a list of mail servers that I would like to send an email to depending on the recipient address. For example, I have this array:
<?php
$_hosts = array("example.com" => "mx1.example.com", "domain.com" => "mx1.domain.com");
?>
So I would like when mailing user#example.com, to connect to mx1.example.com and drop the email there. I'm using PHPMailer to send an email to user#example.com using the following code:
$mail = new
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = '587';
$mail->SMTPSecure = 'tls';
...
$mail->addAddress("user#example.com");
This code works perfectly, except that it sends the message to the mail server at example.com. How can I instruct PHPMailer to send this email to mx1.example.com instead?
Do whatever you need to look up the address in your array, then set Host to the corresponding server you want to send through. If you leave it set to localhost it will always send through there.
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 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.
$mail->Host = "smtp-mail.outlook.com";
$mail->Port = 25;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "h*****#outlook.com";
$mail->Password = "********";
$mail->setFrom("h****#gmail.com", "Z***** Hao");
$mail->addReplyTo("a********#yahoo.com", "Z****** Hao");
$mail->addAddress("h******#qq.com", "Z**** Hao");
Above is my code try to use PHPMailer sending email through the outlook smtp server,and my qq mailbox received an email from my outlook account,but I thought it would be sent from my gmail account since I used the setFrom() method,and also how can the $mail->addReplyTo statement play a part in it?
And as a beginner I write it down by referencing an example on github page of PHPMailer project,it's the Link.
As currently configured, you're doing this:
Host, Port, SMTPSecure, SMTPAuth: Configure the mail server you want to use to send out the e-mail. In this case it's the Outlook SMTP server.
Username and Password: Credentials you're using to login to the Outlook SMTP server.
setFrom sets the From header in the email message. You're currently setting this to your Gmail address, so most of the time, the receiving party sees this address in his/her email client in the from field. But: this header isn't always respected by the sending SMTP server. It could be replaced by the email address belonging to your credentials to prevent spam.
addReplyTo allows you to set a different reply-to address. If not set, the client will suggest to sent an email to the address as specified in the From field. This allows you to overwrite it.
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.
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";