So as stated in the title I wanna created a function different from mail to send mail(confusing ik)
So using mail () it sends the email from my server regardless of the headers I put so I need to create a function to send the actual email from a set email
Let's say I have an email
flameforgedinc#gmail.com
Lets say I have a bunch of emails in a mailing list
Now I have some code that's gonna use this function to email each one
So this code use's cMail ($to, $sub, $msg, $from);
And the email will appear to the user
From: flameforgedinc#gmail.com
And I actually want it to come from my email
If I use mail then it comes from my server and displays altervista00000 and I don't want the plus my server limits the mail() function to activation emails and I need to be able to send newsletters
Any ideas or workarounds??
My name is Pavel Tashev. I will try to help ;)
The easiest and more correct way from technical point of view is to use your own Mail server which hosts your email account and sends your emails. The purpose of PHP in that cases will be to tell the mail server to send email X to a list of emails Y, from an email account Z.
This will solve all your problems:
max allowed emails per hour;
sender name;
The good news is that you already have a Gmail account which is hosted by Google and you don't need to build your own mail server. You can use Google's mail server. Also, for the email sending I would advice you to use PHPMailer (url: https://github.com/PHPMailer/PHPMailer).
Here is how we can do all of this. Follow these steps:
Integrate PHPMailer in your project. If you use composer, that will be a straightforward process. If you don't, simply download the code and include this file PHPMailerAutoload.php in your project. Just follow the instructions on Github. It is really easy.
So, when you are ready with the installation of PHPMailer you must tell it to connect to your mail server. For Gmail and your email account this would look like this:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'flameforgedinc#gmail.com';
$mail->Password = 'PUT-YOUR-PASSWORD-HERE';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('flameforgedinc#gmail.com', 'YOU CAN PUT YOUR NAMES HERE IF YOU WANT');
The final step is to set up the recipient and the content of the email:
$mail->addAddress('some.user#example.net', 'Joe User');
$mail->addAddress('seconrd.user#example.com', 'The name is optional');
$mail->addReplyTo('flameforgedinc#gmail.com', 'YOU CAN PUT YOUR NAMES HERE IF YOU WANT');
$mail->isHTML(true);
$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';
now you will have to send the email:
!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I hope that will help.
Related
I have a website where I give the opportunity to users to subscribe to a newsletter.
Once a user inputs their email, I want to send them a confirmation email which contains a link with a token. As usual, once the user clicks the link, their email is confirmed and added to my subscribers' database.
I have successfully implemented this with PHPMailer using the following steps:
Create an email account on my host ( for example, the address noreply#myhost.com )
On my PHP code, when I want to send an email from that email addres, do an SMTP login using 'localhost' as domain name and as username and password the ones I used on the email account creation on step 1.
Send the email.
When the user clicks the 'Subscribe to our newsletter' button, he sees a loading icon and gets a message telling him to check his inbox. Step 2 (the SMTP login) takes the most time to execute from PHP.
Is there any way to remain logged in the SMTP account and just send out emails when users request to subscribe to the newsletter so as to reduce the loading time (and possibly the server load)?
Sounds like you're overkilling. No need for logging in each time. All you need for sending email using PHPMailer is
$mail = new PHPMailer;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('ellen#example.com');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
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
My company is currently trying to streamline our process for submitting forms. I have read that what we are trying to do can be done with PHP or PHPMailer but I seem to have hit a roadblock.
What we are trying to do is open a fillable PDF in browser, complete it, and then click a button at the bottom to email it to a designated recipient.
I currently have a PHP script that allows me to email the blank document using PHPMailer and our server email service.
I have tried using the "AddStringAttachment" feature of PHPMailer:
<?php
require("../PHPMailer_5.2.3/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxx.xxx.org"; // SMTP server
$mail->From = "xxx#xxx.org";
$mail->AddAddress("xxx#xxx.org");
$mail->Subject = "Attachment Test";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddStringAttachment($string,'filename.pdf');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
...but I was unable to find a way to define the string in a way that would send the completed form. Currently, if I put any data into the "$string" field the email fails.
Is this even possible? Or am I just spinning my wheels?
The problem might be with your mail server. Many servers don't like sending corrupted PDFs, which would be any PDF that doesn't look like a normal PDF - say, a string of English text.
Are you able to send any other kind of attachment, before I get into how to generate a PDF in PHP?
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.