I am going to use PHP for sending emails. My PHP is supposed to be on a shared host. I have found codes to send email via PHP but I'm worried about writing direct password into the PHP code. Does PHP or GMail SMTP server provide any way to avoid using direct password? maybe something similar to API authentication?
I never have actually tried to just send emails true a SMTP server such google, since all this services need authentication. There is some great projects in Github such this, this allow to send emails via SMTP using secure ports, authentication, SSL, etc.. (remember that php files are server side, so users cant access to this information. so your pass it is secure.)
and if you visit their repo you will find their sample:
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->AddAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->AddAddress('ellen#example.com'); // Name is optional
$mail->AddReplyTo('info#example.com', 'Information');
$mail->AddCC('cc#example.com');
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->AddAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->AddAttachment('/tmp/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';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
But if you dont want to go and hard code the passwords on the php controller or what ever you gotta use to process the emails, you can always use the mail() function on php to send emails.
easy sample find on the documentation:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
This function is a SMTP way to send emails, but the authentication it is always required but in this case it is made with the server, so all headers has to be set to be able send a proper email.
Note some server providers don't aloud sending emails true this function, so in the end you will need to create or use a php SMTP email class to process the emails, and this file have to have the password for authentication reason.
PHP is excuted server side so your password should not be exposed to the public. If by what you mean by shared hosting is you are literally sharing a server with someone, then your password could be stolen for your Gmail account as Gmail smtp requires a password. However, if your server host provides you with a outgoing smtp server, then you most certainly don't need to worry about your passwords as you would not need one.
Related
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 want to know how do I get a SMTP server to use PHPMailer to send automatic emails with my created email (email#mydomain.com).
My webhost doesn't provide one. I brought my domain in OnlyDomains and my webhost is 000Webhost.
<?php
require './PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply#mydomain.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = 'noreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->addAddress('sdfsdf#hotmail.com'); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'swag';
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
You should see if your web host allows you to use sendmail on their system. STMP is not needed with sendmail. Chances are they do since it is a standard part of most any Linux/Unix setup. And since you say that your web host is 000WebHost it seems like they do support the use of sendmail:
You can use PHP's mail() function to send mail for your visitors…
But they also seem to offer SMTP in their premium package.
Past that, I would recommend not worrying about SMTP & sticking to sendmail for now. Looking at the documentation for PHPMailer it seems that the library can use sendmail:
The PHP mail() function usually sends via a local mail server,
typically fronted by a sendmail binary on Linux, BSD and OS X
platforms, however, Windows usually doesn't include a local mail
server; PHPMailer's integrated SMTP implementation allows email
sending on Windows platforms without a local mail server.
Then looking in the examples/ folder in the PHPMailer repository shows there is a sendmail example:
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//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__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "myusername#gmail.com";
$mail->Password = "gmail_password";
$mail->From = 'donotreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->AddAddress('Toreceiver#test.com', 'Receiver'); // Add a recipient
$mail->IsHTML(true);
$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;
$mail->Body = "This is in <b>Blod Text</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
It is sending mail from myusername#gmail.com but I want it to send with 'donotreply#mydomain.com' as set in $mail->From. Any help will be highly appreciated.
Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.
My solution was to add
$mail->AddReplyTo($FromEmail, $FromName);
This way at least if you reply to the message it will be delivered as described
There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.
Use:
$mail->SetFrom('donotreply#mydomain.com', 'Admin');
Instead of $mail->From and $mail->FromName.
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
settings > Accounts and import > Send mail as:
set another email which you want to use as from:
if you are using zoho then you can follow:
settings > Mail tab > Send mail as > add from address
then verify that email.
For GSuite users...
What Jyohul said, is the correct way of doing it. You can add an alias in Google Admin Console, under "Users". Click on the user's name, and then click on "user information". In there you can add Aliases. Once the aliases are added, you can then do what Jyohul said, which I added a needed step...:
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
Go to your Gmail, then click the gear on the top right, then:
settings > Accounts > Send mail as: then click "Add another email address".
Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.
$mail->SMTPDebug = 2;
You will the identify the error. Also make sure your SMTP Server credentials are correct. Like host, username and password.
Mine worked correctly
I am using the class PHPMailer to send Mails via SMTP:
<?php
require 'php_mailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.dfgdfgdfg.de'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'dfgdfg'; // SMTP username
$mail->Password = 'dfgsdfgdsfg'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'community#fdgdfg.de';
$mail->FromName = 'dfgdfgdg';
$mail->AddAddress('interview#dfgdfg.de', 'Udo'); // Add a recipient
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'HTML-Mail mit Logo';
$mail->Body = 'Nachfolgend das <b>Logo</b>';
$mail->AltBody = 'Aktiviere HTML, damit das Logo angezeigt wird';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
My Questions:
Whats the best way to send to a lot of Mails (same Mailtext, only the appellation is different (Hello $NAME)?
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Thanks!
Alex
You are setting PHPMailer to interact with SMTP, so I guess that it will wait for it to complete. This is not optimal, because as you say you will block the PHP script until SMTP responds.
It would be better to send through your localhost: set PHPMailer to use sendmail, which will usually be a wrapper to a local exim4 or postfix, which will then handle the mailing for you. This is much better, also because the local server will handle any possible temporary error, and retry later. PHP won't.
You may also want to explore other options, like Mandrill or Sendgrid to do the job, especially if you do lot of mailing or bulk mailing.
Whats the best way to send to a lot of Mails (same Mailtext, only the
appellation is different (Hello $NAME)?
You can do something like, Set the name too.
// rest of code first
$mail->AddAddress("you#example.com")
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AddBCC($row[0]);
}
$mail->Send();//Sends the email
You can have special string 'name_here' in the body and place $name with str_replace function
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Yes according to my knowledge you will have to wait.
How to do a str_replace ? Assume that your email body is as follows
$body = " Dear %first_name%,
other stuff goes here....... ";
$body = str_replace("%first_name%", $first_name, $body);
above will replace %first_name% with the name($first_name) you provide.
I'm trying to get the basic example working for PHPMailer.
All I done was upload the whole PHPMailer_5.2.2 folder, configured the page below as per the code you see and I keep getting Mailer Error: Message body empty, but I can clearly see the contents.html file has html in it and isn't empty. This is the example file I'm using from the PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php
I tried using the settings I have in Outlook for Gmail that works, I know my username and password, the SMTP port is 587 and it's set to TLS, I tried replacing SSL with TLS in the code below, I still get same error.
I also tried the following code, which has been suggested:
changed this:
$mail->MsgHTML($body);
to this
$mail->body = $body;
I still got same error, is there something else I need to configure? It's my first time using PHPMailer, I can get the standard php mail working, but I want to try this because the page I'm going to be emailing has lots of html and I don't want to have to go through all the html and enter character escapes so someone recommending using this.
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = preg_replace('/[\]/','',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxx#gmail.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('xxx#gmail.com', 'First Last');
$mail->AddReplyTo("xxx","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "xxx.net";
$mail->AddAddress($address, "John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I had the same problem and I solved just changing:
This:
$mail->body = 'Hello, this is my message.';
Into this:
$mail->Body = 'Hello, this is my message.';
body into Body what a little mistake!
PHPMailer->MsgHTML() tries to do something clever with fixing non-absolute URLs, but for me too it just returns empty.
$mail->IsHTML(true);
$mail->Body=$body;
First I had to enable the ssl extension, (which I have obtained from another post on this site). To do that, open php.ini and enable php_openssl.dll by changing
;extension=php_openssl.dll
to
extension=php_openssl.dll
Now, enable tls and use port 587. Then comment out the preg_replace.
Finally, I was able to make it show up in my gmail "Sent" file, although I was expecting to see it in my "Inbox".
This site is awesome! Thanks.
When setting $mail->Body equal to variable the body is empty.
When I attach a string to the body everything works fine.
The solution is to strval() a variable containing Your HTML code.
$message;//Your HTML message code
$mail->Body = strval($message);