PHP mail send to Multiple recipients - php

I have some PHP code that I'm using to send email to a specific e-mail address. However, I'd like to include a couple more e-mail addresses in the PHP for when it sends it.
when i tried it showing the following
ERROR:Mailer Error: You must provide at least one recipient email address.
code
include "class.phpmailer.php"; // include the class file name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "mail.authsmtp.com";
$mail->Port = "25"; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxx";
$mail->Password = "xxxxx";
$mail->SetFrom("cpn#xxxxxxx.com");
$mail->Subject = $sub1;
$mail->Body = $text_mail;
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
any one guide me how to do it

Change this line
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
to this:
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
You can run that function as many times as you like until you've got all the addresses you need.
See Here for more info

It seems that you are miss using the AddAdress method. You should pass every mail separatly like that :
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
See PHPMailer AddAddress() for more details.

If you want to send email to multiple address, You need to call the AddAddress() function for each and every Email address. First parameter is EMAIL address, Second one is Recipient name and it is optional.
$mail->AddAddress("xxxxxxx#gmail.com", "XXXXXXXX");
$mail->AddAddress("aral#xxxxxx.com", "Aral");

Try this
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");

Take a look here PHPMailer AddAddress()
and keep an eye of your line:
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");

Instead of:
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
You should use
$mail->AddAddress('xxxxxxx#gmail.com', '1');
$mail->AddAddress('aral#xxxxxx.com', '2');
$mail->AddAddress('akhader#xxxxxx.com', '3');
Or use CC copies:
$mail->AddCC('xxxxxxx#gmail.com', '1');
$mail->AddCC('aral#xxxxxx.com', '2');
$mail->AddCC('akhader#xxxxxx.com', '3');

Try this.... This may help you
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("chauhanamrish32#gmail.com,amrinderpuri1990#gmail.com,amrinder_puri#yahoo.com,amrindersinghpuri13#gmail.com", $subject,$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mail.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>

Related

How to addCC and addReplyTo for multiple email addresses?

I'd like to use this send.php script for a contact form. Right now when an email is sent, a user inputs their name, email, and the email of the person to send the email to.
The email is sent from #Email_Placeholder and both #Email_Placeholder, $email, and #email_2 gets a copy of the email.
I'd like to add the option for an addReplyTo and addCC so that both $email and $email_2 receives an email when either of the 3 parties makes a reply to the original email thread.
What is the correct syntax so that $email, and $email_2 are added to the addReplyTo and addCC parameters so that they receive every email in the original thread?
//Lines to edit. Goal add $email and $email_2 to addReplyTo and addCC
$mail->addReplyTo('#Email_Placeholder', 'Information');
$mail->addCC('#Email_Placeholder'); //should be changed to the name of the app owner's email
Send.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}
if(empty($email)){
header("location: index.php?noemail");
exit();
}
if(empty($message)){
header("location: index.php?noemail");
exit();
}
/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/
//added line; 'honeypot'
if(empty($_POST['phone2'])){
//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 1;
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");
$mail->setFrom('#Email_Placeholder', 'Greeting');
// Add a recipient : used to also send to name
//This sends an email to both $email and $email_2
$mail->addAddress($email);
$mail->addAddress($email_2);
//This makes it so that #Email_Placeholder receives a copy of the emails
$mail->addReplyTo('#Email_Placeholder', 'Information');
$mail->addCC('email#gmail.com');
/*$mail->addBCC('bcc#example.com');*/
//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you?
<br>
<b> Please Notice: </b><br>
<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 .
"</p>";
$mail->isHTML(true);
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com");
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>

I can not receive from php mail function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I have a very simple contact form and a very simple php mail script. But when I tried to send it to the specified email address, I did not receive the test mail.
Below are my codes:
HTML Form
<div class="form" >
<form action="mail.php" method="post">
Email: <input type="text" name="email" size="38"><br>
詢問主旨:<input type="text" name="subject" size="36"><br>
<div class="queryTitle">
詢問內容
</div>
<br>
<textarea name = "message" rows="6" cols="37">
</textarea>
<br>
<input name = "submitted" type="submit" value="傳送">
</form>
</div>
Php script:
<?php
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
$from = "From: ".$_REQUEST['email']."\r\n"; //Site name
// Change this to your email address you want to form sent to
$to = "verymeanguy2#gmail.com";
$subject = $_REQUEST['subject'];
$message = $from." ".$_REQUEST['message'];
mail($to,$subject,$message,$from);
}
}
?>
Could it be that Gmail blocked my mail? If so, how can I devise a script that can send mails to the popular mails?
Thanks in advance!
Jason
PS: I am hosting mine on heliohost's free Stevie server, if that accounts for something.
lolka_bolka: yes, mail() was called and adding an echo in front of mail function prints 1. And it's not in spam. Len_D: it did print anything, so I think mail was called and returned true. Anthony: how do I ensure that?
I just want to report back my testing. It seems that Gmail blocks Yahoo mail address sender for some reason. When the header is a Gmail address or even a bogus made-up address, Gmail can receive no problem. Yahoo mail on the other hand can receive mail with no problem at all. Anyone can shed a light on that?
When mail returns true (or 1), it means it did correctly what it tried to do. But doesn't always mean it sent mail at all.
the mail() funciton uses "sendmail" to actually send the email.
If sendmail is configured to send mail, then that's what will happen (it everything went ok).
However, by defult, sendmail stores mail in the server. It "simulates" a real sendmail.
From my personal experience, I recommend PHPMailer. It's easy to use, it's much easier to check for errors (check if the email was sent, and if not, get infomation about the problem).
Example:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->From = "email#domain.com";
$mail->FromName = "auto";
$mail->AddAddress("example#example.net", "Name of example user"); //send to....
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Subject = "=?UTF-8?B?".base64_encode(stripslashes($asunto))."=?=";
$mail->Body = stripslashes($mensaje);
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
return true;
} else {
echo "Message has been sent";
return false;
}
Hope it helped :)

How to Code phpMailer to prevent sending email if hidden field is filled in contact form

I like phpMailer but when I used a previous mailer it had an anti spam code.
You added a hidden field in the contact form and the mail.php script was coded that if the hidden field was filled in (i.e. only a spam robot would do that) the mail wouldnt send
How would I add that to this script?
This is my mail.php code as follows
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$address = $_REQUEST['address'] ;
$postcode = $_REQUEST['postcode'] ;
$service = $_REQUEST['service'] ;
$height = $_REQUEST['height'] ;
$how = $_REQUEST['how'] ;
$website = $_REQUEST['website'] ;
$mail_intro ="The following enquiry came from your web site:";
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("class.phpmailer.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "franchise#website.co.uk"; // SMTP username
$mail->Password = "passowrd"; // SMTP password
$mail->From = "noreply#website.co.uk";
$mail->FromName = "B";
$mail->AddReplyTo($email,$name);
$mail->AddAddress("franchise#website.co.uk", "B");
$mail->AddBCC("joseph#website.co.uk", "Joseph");
$response="$mail_intro\n<br />Name: $name\n<br />Email: $email\n<br />Phone: $phone\n<br />Address: $address\n<br /> Comments:\n$message\n<br /> Website: $website\n";
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "Enquiry";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $response;
$mail->AltBody = $message;
//Autoresponder
$mailto="$email";
$subject="Franchisee Enquiry";
$body.="Thanks for your enquiry, the message below has been sent. \n";
$body.="If due to an unknown technical error you do not receive a response within 4 hours please phone \n";
$body.="or email us directly at info#website.co.uk where we will be only too happy to help.\n";
$body.="$mail_intro\n";
$body.="Name: $name\n";
$body.="Email: $email\n";
$body.="Phone: $phone\n";
$body.="Postcode: $postcode\n";
$body.="Service Required: $service\n";
$body.="How found: $how\n";
$body.="Comments:\n$message\n";
mail($mailto,$subject,$body,"From: noreply#website.co.uk\n");
if(!$mail->Send())
{
header("Location: http://website.co.uk/error.php");
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
header("Location: http://website.co.uk/thankyou.php");
?>
In your HTML, add:
<input type="text" name="fooBarBaz" style="display: none">
And in your PHP:
if ( !empty($_REQUEST['fooBarBaz']) )
{
// hidden field was filled out, do something about it
}
else
{
$email = $_REQUEST['email'];
...
}

Send email with attachment via PHPMailer

I'm preparing to create a form page for a website that will require many fields for the user to fill out and that will be sent to a specified email.
So far I've created a dummy php email page that gets your Message, 1 attachment, and Recipient Email address using Google's SMTP.
Here's my code for uploadtest.html:
<body>
<h1>Test Upload</h1>
<form action="email.php" method="get">
Message: <input type="text" name="message">
Email: <input type="text" name="email"><br>
Attach File: <input type="file" name="file" id="file">
<input type="submit">
</form>
</body>
uploadtest.html is what the user will see
Here's the code for email.php:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$recipiant = $_GET["email"];
$message = $_GET["message"];
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->SMTPSecure = 'ssl';
$mail->Username = "xxxxx#gmail.com"; // SMTP account username
$mail->Password = "xxxxxxxx"; // SMTP account password
$mail->AddAttachment($_FILES['tmp_name']); //****HERE'S MY MAIN PROBLEM!!!
$mail->SetFrom('cinicraftmatt#gmail.com', 'CiniCraft.com'); // FROM
$mail->AddReplyTo('cinicraftmatt#gmail.com', 'Dom'); // Reply TO
$mail->AddAddress($recipiant, 'Dominik Andrzejczuk'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = $message;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
So from what I can tell here, PHPMailer's AddAttachment() method takes as a parameter the URL of the file DIRECTORY you want attached. And this is where my main problem is.
What would the name of the variable be that would get the location of my file (dir/upload.jpg) that I've uploaded so I could use it as a parameter in the AddAttachment() method?
No, it doesn't take URLs, or directories. It takes a direct path to a FILE.
e.g.
$mailer->AddAttachment(
'/path/to/file/on/your/server.txt',
'name_of_file_in_email',
'base64',
'mime/type'
);
The path is self-explanatory. The name_of_file_in_email allows you to "rename" the file, so that you might loaded a file named "foo.exe" on your server, it can appear as "bar.jpg" in the email the client receives.
Your problem is that you're trying to attach an uploaded file, but using the wrong source. It should be
<input type="file" name="thefile" />
^^^^^^^
$_FILES['thefile']['tmp_name']
^^^^^^^
Note the field name relationship to $_FILES.
This will be send like this through PHPMailer Class
$mail->AddAttachment($_FILES['tmp_name'],$_FILES['name']); //****HERE'S MY MAIN PROBLEM!!! So you would have to give file name also.
please try with this
include "phpMailerClass.php";
$email = new PHPMailer();
$email->From = $to;
$email->FromName = $_POST['name'];
$email->Subject = $subject;
$email->Body = $EmailText;
$email->AddAddress( 'shafiq2626#hotmail.com' );
$email->IsHTML(true);
$file_to_attach = $_FILES['file']['tmp_name'];
$filename=$_FILES['file']['name'];
$email->AddAttachment( $file_to_attach , $filename );
$email->Send();
This is code is working fine.
Your <form> tag should specify the enctype attribute like so :
<form ... enctype="multipart/form-data">
...
</form>
Use the following code:
$objectMailers->AddAttachment("file.extension");
tested on PHP 7 and using PHPMailer 5.3

How to get user mails in my free gmail inbox through contact us form on my website

How to get user mails in my free gmail inbox through contact us form on my website. I do not use email with my website name . i use free gmail. I tried many script but the all need email account on domain.
Does this question have something to do with This One?
Well, so you have a form on your site, your users fill it up and you need an email with that data, right?
Simple example:
<form action="sendMail.php" method="post">
Name: <input type="text" name="name" id="name" /><br />
Email: <input type="text" name="email" id="email" /><br />
Text: <textarea name="text"></textarea><br />
<input type="submit" value="Send" />
</form>
then, the php page wich send the mail:
//php sendThis.php page
<?php
require("class.phpmailer.php");
$name = $_POST['name'];
$email = $_POST['email'];
$text = $name . ', ' . $email . ' has filled the form with the text:<br />' . $_POST['text'];
$from = 'your.email#gmail.com';
$to = 'your.email#gmail.com';
$gmailPass = 'your gmail password';
$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 = $from;
// GMAIL password
$mail->Password = $gmailPass;
$mail->From = $from;
$mail->FromName = $from;
$mail->AddReplyTo($from, $from);
$mail->Subject = 'This is a test!';
$mail->Body = $text;
$mail->MsgHTML($text);
$mail->IsHTML(true);
$mail->AddAddress($to, $to);
if(!$mail->Send()){
echo $mail->ErrorInfo;
}else{
echo 'sent!';
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
EDIT: just tested and works fine. Make sure that the 3 files (class.phpmailer.php, class.pop3.php and class.smtp.php) are in the correct include path
Basically, it involves the PHP mail() function:
<?php
mail(yourGmailAddress, object, message);
?>
As you have already observed, this solution works only if the webserver operates a mail server. This mail server may forbid unknown users. So you need to have an email account on that web/mail server (I believe this is the case). The second step then is to forward mail from your website address to you gmail account. I am 90% certain that it is possible from your gmail configuration. It may also be possible from your website mail configuration. But don't configure both!
Try enabling openssl.
Uncomment the line:
extension=php_openssl.dll
in your php.ini file.
You can also put your email address into "action" attribute of the form element. But that's very unreliable. Like this:
<form method='post' action='mailto:your#email.com?Subject=Hello'>
...
</form>
Users must have email client installed and configured for this to work properly. There are some other drawbacks. You have to do some research to find whether this method is for you or not.
http://www.google.com/search?client=opera&rls=en&q=form+action+mailto&sourceid=opera&ie=utf-8&oe=utf-8

Categories