Help with sending a second email


#1

Hi, I’m trying to have this code send the same identical email that goes to “$youremail”, to also go to the “E-Mail [email]” address. Any guidance with this, will be appreciated: Here’s the current code:

<?php
{
	$youremail = 'wcform@real....com';

	$body = "UploadForm-M:
	Name:  $_POST[name]
	E-Mail: $_POST[email]
	Message: $_POST[message]
	Video: $_POST[videolink]";

	if( $_POST['email'] && !preg_match( "/[\r\n]/", $_POST['email']) ) {
	  $headers = "From: $_POST[email]";
	} else {
	  $headers = "From: $youremail";
	}
	mail($youremail, 'Contact Form', $body, $headers );
}
if(empty($error))
{
header('Location: http://real...com');
exit;
}
?>

#2

Use CC/BCC and an email wrapper.


#3

Or if you want to explicitly send it separately, just duplicate the line that actually sends the mail and use the alternate email address, though wrap it in an if() check to make sure there’s something in it, though as @chorn alluded to above, there are plenty of issues with using the in-built mail() function that suggest you should also look at using something else to send the email, while you’re in there.

You may also come into some issues if you try to send your email using the address supplied in the form as the from-address - many email servers will only send emails from a domain that they’re configured for. In my opinion, you should always send emails like this from an address that you own, and maybe use the reply-to header if there’s a need to be able to respond to them directly.


#4

You could try something like this:

$youremail 	= 'wcform@real....com';	
$v_email 	= $_POST['email']

if (mail($youremail, 'Contact Form', $body, $headers )){
	mail($v_email, 'Contact Form', $body, $headers );
}

Or like @chorn suggested use CC or BCC


#5

Thank you for your replies.
how could I add CC or BCC to this code?


#6

First read the RFC at

https://tools.ietf.org/html/rfc5322

or have a look at implementations of email wrappers. (Or use an email wrapper)


#7

Maybe something like this:

$headers	=  	'From: ' . $yourname . ' <' . $youremail . '>' . "\r\n" . 
				'Return-Path: Mail-Error <' . $youremail . '>' . "\r\n" .
				'Bcc: ' . $v_email . "\r\n" .
				'X-Mailer: PHP/' . phpversion() . "\r\n" .
				'X-Priority: 1 (Highest)' . "\r\n" .
				'X-MSMail-Priority: High' . "\r\n" .
				'Importance: High' . "\r\n" .
				'MIME-Version: 1.0' . "\r\n" .
				'Content-type: text/html; charset=iso-8859-1' . "\r\n";

#8

Thanks for all the help/replies. This code below works successfully, the only thing I can’t figure out is that when I open up the first email sent it shows that it was sent from: wcform@real…com, but the second email shows that it was sent from an email address pertaining to the server, how can I have the second email show it’s from wcform@real…com?
Here’s the current code:

<?php
{

	$youremail = 'wcform@real....com';

	$body = "UploadForm-M:
	Name:  $_POST[name]
	E-Mail: $_POST[email]
	Message: $_POST[message]";

	$headers .= "Hello ".$_POST[name]."";
	mail($youremail, 'Real Message', $body, $headers );

	$headers .= "Hello ".$_POST[name]."";
	mail($_POST['email'], 'test', $headers );
}

if(empty($error))
{
header('Location: http://real....com');
exit;
}
?>

#9

Your code specifies the from-address, it’s in your first post. It’s not in this latest post of the working code, presumably you just snipped that bit out for the post. If you’re not specifying a from-address, I guess it’s down to your server what it sends, though you would expect it to be consistent.

Why is this line

$headers .= "Hello ".$_POST[name]."";

in your latest code twice?


#10

Thank you again, got it …